diff options
| -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 |
