Skip to content

Instantly share code, notes, and snippets.

@bjulius
Created June 20, 2024 05:47
Show Gist options
  • Save bjulius/7d1e9895205ecb654c805e64c28e1388 to your computer and use it in GitHub Desktop.
Save bjulius/7d1e9895205ecb654c805e64c28e1388 to your computer and use it in GitHub Desktop.
Brian Julius Solution to Omid Challenge 70
let
Source = Table.AddColumn(
Table.FromList({1, 2, 5, 10}, Splitter.SplitByNothing(), {"Denoms"}, null, null),
"Target",
each 11
),
Rscript = R.Execute(
"df <- dataset#(lf)coin_combinations <- function(target, coins) {#(lf) dp <- vector(""list"", target + 1)#(lf) dp[[1]] <- list(numeric(0)) #(lf) for (coin in coins) {#(lf) for (amount in coin:target) {#(lf) if (!is.null(dp[[amount - coin + 1]])) {#(lf) for (combination in dp[[amount - coin + 1]]) {#(lf) dp[[amount + 1]] <- append(dp[[amount + 1]], list(c(combination, coin)))#(lf) }#(lf) }#(lf) }#(lf) }#(lf) #(lf) return(dp[[target + 1]])#(lf)}#(lf)#(lf)target_sum <- max(df$Target)#(lf)coin_denominations <- as.vector(df$Denoms)#(lf)combinations <- coin_combinations(target_sum, coin_denominations)#(lf)max_length <- max(sapply(combinations, length))#(lf)padded_combinations <- lapply(combinations, function(x) {#(lf) length(x) <- max_length#(lf) return(x)#(lf)})#(lf)#(lf)combs_df <- do.call(rbind, lapply(padded_combinations, function(x) as.data.frame(t(x))))#(lf)colnames(combs_df) <- paste0(""Coin"", 1:ncol(combs_df))#(lf)combs_df",
[dataset = Source]
){[Name = "combs_df"]}[Value],
AddIdx = Table.AddIndexColumn(Rscript, "Combo", 1, 1, Int64.Type),
UnpivOther = Table.RemoveColumns(Table.UnpivotOtherColumns(AddIdx, {"Combo"}, "A", "Value"), "A"),
Group = Table.TransformColumns(
Table.Group(UnpivOther, {"Combo", "Value"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
{"Value", each Text.From(_) & "$"}
),
Piv = Table.Pivot(Group, List.Distinct(Group[Value]), "Value", "Count"),
RepNulls = Table.RemoveColumns(
Table.ReplaceValue(Piv, null, 0, Replacer.ReplaceValue, Table.ColumnNames(Piv)),
"Combo"
)
in
RepNulls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment