diff options
| author | Simon Proctor <simon.proctor@zoopla.co.uk> | 2020-08-25 11:13:00 +0100 |
|---|---|---|
| committer | Simon Proctor <simon.proctor@zoopla.co.uk> | 2020-08-25 11:13:00 +0100 |
| commit | 25f0d1da2eb93ba4ba6df4b0f615fa0c8145b7c7 (patch) | |
| tree | 94bae3dd7af467e59829d80345352b5a814655cf /challenge-075/simon-proctor | |
| parent | 2c738f5d45e8107d7447823e0b6b3094d6c97f5a (diff) | |
| download | perlweeklychallenge-club-25f0d1da2eb93ba4ba6df4b0f615fa0c8145b7c7.tar.gz perlweeklychallenge-club-25f0d1da2eb93ba4ba6df4b0f615fa0c8145b7c7.tar.bz2 perlweeklychallenge-club-25f0d1da2eb93ba4ba6df4b0f615fa0c8145b7c7.zip | |
Probably overkill but it works and is pretty fast for big numbers with British coins
Diffstat (limited to 'challenge-075/simon-proctor')
| -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..32c4e8d699 --- /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.Array } ).unique( with => &[~~] ); + %change_cache{$amount} = @result; + return @result; +} |
