diff options
| author | Util <bruce.gray@acm.org> | 2024-04-01 15:01:06 -0500 |
|---|---|---|
| committer | Util <bruce.gray@acm.org> | 2024-04-01 15:01:06 -0500 |
| commit | 7457eaf6a496cee49ab2b0bc4ced88cc5a974e49 (patch) | |
| tree | ee484c639071fcb333633d4665a5a1f5e1014c5c | |
| parent | 6e3cd6e8a2eb65b0ba00b4e15e1abeb689a33e8d (diff) | |
| download | perlweeklychallenge-club-7457eaf6a496cee49ab2b0bc4ced88cc5a974e49.tar.gz perlweeklychallenge-club-7457eaf6a496cee49ab2b0bc4ced88cc5a974e49.tar.bz2 perlweeklychallenge-club-7457eaf6a496cee49ab2b0bc4ced88cc5a974e49.zip | |
Add TWC 263 solutions by Bruce Gray, in Raku only.
| -rw-r--r-- | challenge-263/bruce-gray/raku/ch-1.raku | 13 | ||||
| -rw-r--r-- | challenge-263/bruce-gray/raku/ch-2.raku | 29 |
2 files changed, 42 insertions, 0 deletions
diff --git a/challenge-263/bruce-gray/raku/ch-1.raku b/challenge-263/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..78fd02e2a2 --- /dev/null +++ b/challenge-263/bruce-gray/raku/ch-1.raku @@ -0,0 +1,13 @@ +sub task1 ( UInt $k, @ns ) { + return @ns.sort.grep: :k, $k; +} + + +use Test; plan +constant @tests = + ( 2, (1, 5, 3, 2, 4, 2), (1, 2) ), + ( 6, (1, 2, 4, 3, 5 ), () ), + ( 4, (5, 3, 2, 4, 2, 1), (4,) ), +; +for @tests -> ( $k, @ns, @expected ) { + is-deeply task1($k, @ns), @expected; +} diff --git a/challenge-263/bruce-gray/raku/ch-2.raku b/challenge-263/bruce-gray/raku/ch-2.raku new file mode 100644 index 0000000000..d4e42a564f --- /dev/null +++ b/challenge-263/bruce-gray/raku/ch-2.raku @@ -0,0 +1,29 @@ +# Both solutions work with any number of input arrays, not just 2. + +sub task2_via_classify ( *@AoA ) { # First attempt + @AoA.pairup + .classify( *.key, :as{ .value } ) + .map({ +.key, .value.sum }) + .sort; +} + +sub task2_via_Bag ( *@AoA ) { # Better, IMO + return @AoA.pairup.Bag.map( *.kv.cache ).sort; +} + + +constant @tests = + ( ( (1,1), (2,1), (3,2) ), ( (2,2), (1,3) ), ( (1,4), (2,3), (3,2) ) ), + ( ( (1,2), (2,3), (1,3), (3,2) ), ( (3,1), (1,3) ), ( (1,8), (2,3), (3,3) ) ), + ( ( (1,1), (2,2), (3,3) ), ( (2,3), (2,4) ), ( (1,1), (2,9), (3,3) ) ), +; +constant @subs = + :&task2_via_classify, + :&task2_via_Bag, +; +use Test; plan @tests * @subs; +for @subs -> ( :key($sub_name), :value(&task2) ) { + for @tests -> ( @it1, @it2, @expected ) { + is-deeply task2(@it1, @it2), @expected, "$sub_name : @it1[]"; + } +}
\ No newline at end of file |
