diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2023-03-14 19:16:27 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2023-03-14 19:16:27 +0800 |
| commit | bf995619b6b34ca5b440bb1d7013e87ac6e14e16 (patch) | |
| tree | 8b3d1b18e7be6c79a1989a52b982938cabfbce39 | |
| parent | f0932f64660f5f3ea94798d22731f6710104fd5a (diff) | |
| download | perlweeklychallenge-club-bf995619b6b34ca5b440bb1d7013e87ac6e14e16.tar.gz perlweeklychallenge-club-bf995619b6b34ca5b440bb1d7013e87ac6e14e16.tar.bz2 perlweeklychallenge-club-bf995619b6b34ca5b440bb1d7013e87ac6e14e16.zip | |
Challenge 201, Raku solutions
| -rwxr-xr-x | challenge-201/feng-chang/raku/ch-1.raku | 5 | ||||
| -rwxr-xr-x | challenge-201/feng-chang/raku/ch-2.raku | 46 |
2 files changed, 51 insertions, 0 deletions
diff --git a/challenge-201/feng-chang/raku/ch-1.raku b/challenge-201/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..03195cb99d --- /dev/null +++ b/challenge-201/feng-chang/raku/ch-1.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(*@N where @N.all ~~ UInt); + +put ((0..+@N) (-) @N».UInt).keys.sort; diff --git a/challenge-201/feng-chang/raku/ch-2.raku b/challenge-201/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..daf9522db9 --- /dev/null +++ b/challenge-201/feng-chang/raku/ch-2.raku @@ -0,0 +1,46 @@ +#!/bin/env raku + +proto penny-piles(UInt:D $n, Int:D $lower-bound where $lower-bound > 0 = 1) { * } + +multi penny-piles($n) { penny-piles($n, 1) } + +multi penny-piles($n, $m where $m > $n) { [] } +multi penny-piles($n, $m where $m == $n) { [[$n],] } + +multi penny-piles($n, $lower-bound) { + my @a; + + for $lower-bound .. $n-1 -> $i { + @a.push(|penny-piles($n - $i, $i).map({ [ $i, |$_ ] })); + } + + @a.push([$n]); + + @a +} + +multi MAIN('test') { + use Test; + + is-deeply penny-piles(1, 1), [[1],], 'penny-piles(1, 1) => [[1],]'; + is-deeply penny-piles(2, 2), [[2],], 'penny-piles(2, 2) => [[2],]'; + is-deeply penny-piles(5, 5), [[5],], 'penny-piles(5, 5) => [[5],]'; + + is-deeply penny-piles(1), [[1],], 'penny-piles(1) => [[1],]'; + + is-deeply penny-piles(2, 1), [[1, 1], [2]], 'penny-piles(2, 1) => [[1, 1], [2]]'; + is-deeply penny-piles(2), [[1, 1], [2]], 'penny-piles(2) => [[1, 1], [2]]'; + + is-deeply penny-piles(3, 1), [[1, 1, 1], [1, 2], [3]], 'penny-piles(3, 1) => [[1, 1, 1], [1, 2], [3]]'; + is-deeply penny-piles(3), [[1, 1, 1], [1, 2], [3]], 'penny-piles(3) => [[1, 1, 1], [1, 2], [3]]'; + + is-deeply penny-piles(4), [[1, 1, 1, 1], [1, 1, 2], [1, 3], [2, 2], [4]], 'penny-piles(4) => [[1, 1, 1, 1], [1, 1, 2], [1, 3], [2, 2], [4]]'; + is-deeply penny-piles(5), [[1, 1, 1, 1, 1], [1, 1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 4], [2, 3], [5]], + 'penny-piles(5) => [[1, 1, 1, 1, 1], [1, 1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 4], [2, 3], [5]]'; + + done-testing; +} + +multi MAIN(UInt:D $n) { + put penny-piles($n).elems; +} |
