diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-05-13 00:37:16 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-13 00:37:16 +0100 |
| commit | 5d841dbd3b14883b2cfd82e38df1e7620c02b6ad (patch) | |
| tree | 265635f68392ac58de56ae744bc2ae79c19de887 | |
| parent | 4cdfbf9c8e945a19f9c6acf95a8791bedd4b5cc5 (diff) | |
| parent | 50496f248ff868ae0c7e3a0c7225bc7bf6e44035 (diff) | |
| download | perlweeklychallenge-club-5d841dbd3b14883b2cfd82e38df1e7620c02b6ad.tar.gz perlweeklychallenge-club-5d841dbd3b14883b2cfd82e38df1e7620c02b6ad.tar.bz2 perlweeklychallenge-club-5d841dbd3b14883b2cfd82e38df1e7620c02b6ad.zip | |
Merge pull request #10083 from Util/c268
Add TWC 268 solutions by Bruce Gray, in Raku only.
| -rw-r--r-- | challenge-268/bruce-gray/raku/ch-1.raku | 50 | ||||
| -rw-r--r-- | challenge-268/bruce-gray/raku/ch-2.raku | 22 |
2 files changed, 72 insertions, 0 deletions
diff --git a/challenge-268/bruce-gray/raku/ch-1.raku b/challenge-268/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..1bd89ca7eb --- /dev/null +++ b/challenge-268/bruce-gray/raku/ch-1.raku @@ -0,0 +1,50 @@ +# Common code +sub adds_to_same { (@^a X+ $^addend).Bag eqv @^b.Bag } + +sub task1_detailed_failure ( @x, @y --> Rat ) { + die if @x.elems != @y.elems; + + my Rat $ret = (@y.sum - @x.sum) / @x.elems; + + unless adds_to_same(@x, $ret.narrow, @y) { + warn "If a answer existed, it would be $ret, but $ret added to `@x`(@x[]) does not result in `@y`(@y[]), so no answer exists."; + return Nil; + } + + return $ret; +} +sub task1_Z_minus ( @x, @y --> Numeric ) { + die if @x.elems != @y.elems; + + my @y-x = (@y.sort Z- @x.sort).squish; + + return +@y-x == 1 ?? @y-x[0] !! Nil; +} +# Credit to roger-bell-west for the .min idea. +sub task1_concise ( @x, @y --> Numeric ) { + return adds_to_same(@x, $_, @y) ?? $_ !! Nil given @y.min - @x.min; +} + + +constant @tests = + ( 2, ( 3, 7, 5 ) , ( 9, 5, 7 ) ), + ( 3, ( 1, 2, 1 ) , ( 5, 4, 4 ) ), + ( 3, ( 2, ) , ( 5, ) ), + ( Nil, ( 0, 3, 7 ) , ( 2, 6, 8 ) ), + ( 0, ( 5, 7, 9 ) , ( 9, 5, 7 ) ), + ( -2, ( -3, -7, -5 ) , ( -9, -5, -7 ) ), + ( -3, ( -1, -2, -1 ) , ( -5, -4, -4 ) ), + ( -3, ( -2, ) , ( -5, ) ), + ( -3, ( 5, ) , ( 2, ) ), +; +constant @subs = + :&task1_detailed_failure, + :&task1_Z_minus, + :&task1_concise, +; +use Test; plan +@subs * +@tests; +for @subs -> ( :key($sub_name), :value(&task1) ) { + for @tests -> ( $expected, @x, @y ) { + is task1(@x, @y), $expected, "$sub_name : @x[] : @y[]"; + } +} diff --git a/challenge-268/bruce-gray/raku/ch-2.raku b/challenge-268/bruce-gray/raku/ch-2.raku new file mode 100644 index 0000000000..5c267e5669 --- /dev/null +++ b/challenge-268/bruce-gray/raku/ch-2.raku @@ -0,0 +1,22 @@ +constant &task2_also_works_with_odd = *.sort(+*).batch(2)ยป.reverse.flat; + +sub task2_does_not_need_flat ( @ns where +@ns %% 2 ) { + return @ns.sort(+*).map: { |($^y, $^x) }; +} + + +constant @tests = + ( ( 2, 5, 3, 4 ) , (3, 2, 5, 4) ), + ( ( 9, 4, 1, 3, 6, 4, 6, 1 ) , (1, 1, 4, 3, 6, 4, 9, 6) ), + ( ( 1, 2, 2, 3 ) , (2, 1, 3, 2) ), +; +constant @subs = + :&task2_also_works_with_odd, + :&task2_does_not_need_flat, +; +use Test; plan +@subs * +@tests; +for @subs -> ( :key($sub_name), :value(&task2) ) { + for @tests -> ( @in, @expected ) { + is-deeply task2(@in), @expected, "$sub_name : @in[]"; + } +} |
