Omid - Challenge 70

data-challenges
advanced-exercises
🔰 For example, the highlighted row means using eleven 1$ coins to make a total of 11$.
Published

March 24, 2026

Illustration for Omid - Challenge 70

Challenge Description

🔰 For example, the highlighted row means using eleven 1$ coins to make a total of 11$.

Solutions

library(tidyverse)
library(readxl)

path = "files/CH-070 coin change problem.xlsx"
test = read_excel(path, range = "H2:K14") %>% arrange(`1$`,`2$`,`5$`,`10$`)

target <- 11
coins <- c(1, 2, 5, 10)

counts <- expand.grid(
  n1 = 0:(target / coins[1]),
  n2 = 0:(target / coins[2]),
  n3 = 0:(target / coins[3]),
  n4 = 0:(target / coins[4])
)

combinations <- counts %>%
  mutate(
    total = n1 * coins[1] + n2 * coins[2] + n3 * coins[3] + n4 * coins[4]
  ) %>%
  filter(total == target) %>%
  select(-total) %>%
  arrange(n1,n2,n3,n4)

all.equal(combinations, test, check.attributes = FALSE)
# [1] TRUE
  • Logic:

    • Reads the workbook ranges needed for the challenge

    • Builds the intermediate columns that drive the final result

  • Strengths:

    • The R solution stays close to the workbook rule and keeps the transformation compact.
  • Areas for Improvement:

    • The code assumes the sheet structure and source ranges remain stable.
  • Gem:

    • The strongest part of the solution is choosing the right intermediate representation before shaping the final output.
import pandas as pd

path = "CH-070 coin change problem.xlsx"
test = pd.read_excel(path, usecols = "H:K", skiprows=1, nrows = 12).sort_values(['1$', '2$', '5$', '10$']).reset_index(drop=True)

target = 11
coins = [1, 2, 5, 10]

counts = pd.DataFrame(
    index=pd.MultiIndex.from_product([range(target + 1)] * 4, names=['n1', 'n2', 'n3', 'n4'])
).reset_index()


combinations = counts.assign(
    total=lambda x: x['n1'] * coins[0] + x['n2'] * coins[1] + x['n3'] * coins[2] + x['n4'] * coins[3]
).query('total == @target').drop('total', axis=1).sort_values(['n1', 'n2', 'n3', 'n4']).reset_index(drop=True)
combinations.columns = test.columns 

print(combinations.equals(test)) # True
  • Logic:

    • Reads the workbook ranges needed for the challenge

    • Builds the intermediate columns that drive the final result

  • Strengths:

    • The Python version follows the same rule in a direct dataframe-oriented implementation.
  • Areas for Improvement:

    • The code assumes the workbook layout remains stable, so any sheet redesign would require small adjustments.
  • Gem:

    • The implementation stays close to the original workbook rule instead of adding unnecessary abstraction.

Difficulty Level

This task is moderate:

  • The business rule is readable, but the workbook still requires careful implementation to reach the expected layout.