diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-08-25 13:06:33 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-25 13:06:33 +0100 |
| commit | 6fca17edc8608897dde9c149d92e912117cb82d8 (patch) | |
| tree | a8d9283d39b3dbfea925f820f88bcfe0f3ed87d0 | |
| parent | 2c738f5d45e8107d7447823e0b6b3094d6c97f5a (diff) | |
| parent | ead308f663947f7031f362b9883e5fd5105d0322 (diff) | |
| download | perlweeklychallenge-club-6fca17edc8608897dde9c149d92e912117cb82d8.tar.gz perlweeklychallenge-club-6fca17edc8608897dde9c149d92e912117cb82d8.tar.bz2 perlweeklychallenge-club-6fca17edc8608897dde9c149d92e912117cb82d8.zip | |
Merge pull request #2143 from Scimon/master
Challenge 1
| -rw-r--r-- | challenge-075/simon-proctor/raku/ch-1.raku | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/challenge-075/simon-proctor/raku/ch-1.raku b/challenge-075/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..1ff6b48cfc --- /dev/null +++ b/challenge-075/simon-proctor/raku/ch-1.raku @@ -0,0 +1,31 @@ +#!/usr/bin/env raku + +use v6; + +#| Given an amount and a list of valid coins show all the combinations of change +sub MAIN ( + UInt $amount where * > 0, #= Amount to make change of + *@coins where { .all ~~ Int && .all > 0 }, #= Valid coins +) { + .join(", ").say for make-change( $amount, @coins ); +} + +my %change_cache; + +multi sub make-change( 0, @coins ) { + return [[],] +} + +multi sub make-change( $amount, @coins ) { + with %change_cache{$amount} { return |%change_cache{$amount} } + my @result = []; + for @coins.grep( * <= $amount ) -> $coin { + my @list = make-change( $amount - $coin, @coins.grep( * <= $amount ) ); + for @list -> @change { + @result.push( [$coin, |@change] ); + } + } + @result = @result.map( { $_.sort.reverse.Array } ).unique( with => &[~~] ).sort( { $^a.elems cmp $^b.elems } ); + %change_cache{$amount} = @result; + return @result; +} |
