Hello, here is my solution to the Task 1 of the Week 75 of the Weekly Challenge solved in the Raku programming language.

## Coins Sum

You are given a set of coins `@C`

, assuming you have infinite amount of each coin in the set.

Write a script to find how many ways you make sum `$S`

using the coins from the set `@C`

.

My solution is a kind of a two-liner:

my @coins = 1, 2, 4; my $sum = 6; my @wallet; @wallet.append: $_ xx ($sum div $_) for @coins; .say for @wallet.combinations.unique(:as(*.Str)).grep({$sum == [+] $_});

The `@coins`

array keeps the types of available coins. Then, we fill the `@wallet`

so that we have enough real coins to make the `$sum`

by using only a single kind of them (if that is possible at all).

For the given example, the `@wallet`

contains the following collection:

[1 1 1 1 1 1 2 2 2 4]

The next step is to find all possible `unique`

`combinations`

of the coins from the wallet and take only those, whose sum is `$sum`

.

To take only unique combinations, I am calling `.unique(:as(*.Str))`

which treats them as a string. This step can possibly be made more optimal.

To filter the combinations, a `grep`

method is used, where the reduction operator is used to compute the sum: `.grep({$sum == [+] $_})`

.

With the above input, the program prints the following combinations of coins that give the required amount:

(2 4) (1 1 4) (2 2 2) (1 1 2 2) (1 1 1 1 2) (1 1 1 1 1 1)

* * *

→ GitHub repository

→ Navigation to the *Raku challenges* post series