diff options
| author | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2024-04-07 22:28:33 +0200 |
|---|---|---|
| committer | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2024-04-07 22:28:33 +0200 |
| commit | 0d15af8d561aa1ea1bed9aabca6c502d115adac2 (patch) | |
| tree | 164b668117e17ac513e6f2ea3eba949e26156d96 /challenge-263/barroff/raku | |
| parent | 0495c7534995083a63370f8e93d3834bbe1f054d (diff) | |
| download | perlweeklychallenge-club-0d15af8d561aa1ea1bed9aabca6c502d115adac2.tar.gz perlweeklychallenge-club-0d15af8d561aa1ea1bed9aabca6c502d115adac2.tar.bz2 perlweeklychallenge-club-0d15af8d561aa1ea1bed9aabca6c502d115adac2.zip | |
feat: add solutions for challenge 263 from BarrOff
Diffstat (limited to 'challenge-263/barroff/raku')
| -rw-r--r-- | challenge-263/barroff/raku/ch-1.p6 | 24 | ||||
| -rw-r--r-- | challenge-263/barroff/raku/ch-2.p6 | 25 |
2 files changed, 49 insertions, 0 deletions
diff --git a/challenge-263/barroff/raku/ch-1.p6 b/challenge-263/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..67714a3b8a --- /dev/null +++ b/challenge-263/barroff/raku/ch-1.p6 @@ -0,0 +1,24 @@ +#!/usr/bin/env raku + +use v6.d; + +sub target-index(Int:D $k, @ints --> Positional) { + indices(@ints.sort.join, $k) +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is target-index(2, [1, 5, 3, 2, 4, 2]), (1, 2), + 'works for 2 and (1, 5, 3, 2, 4, 2)'; + is target-index(6, [1, 2, 4, 3, 5]), (), 'works for 6 and (1, 2, 4, 3, 5)'; + is target-index(4, [5, 3, 2, 4, 2, 1]), (4), + 'works for 4 and (5, 3, 2, 4, 2, 1)'; +} + +#| Take user provided number like 10 1 111 24 1000 +multi sub MAIN(Int:D $k, *@ints) { + say target-index($k, @ints); +} diff --git a/challenge-263/barroff/raku/ch-2.p6 b/challenge-263/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..0c55055914 --- /dev/null +++ b/challenge-263/barroff/raku/ch-2.p6 @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +use v6.d; + +sub merge-items(@items1, @items2 --> Positional) { + my %dict; + my @res; + map({ %dict{ $_[0] } += $_[1] }, @items1); + map({ %dict{ $_[0] } += $_[1] }, @items2); + map({ @res.push([$_.Int, %dict{$_}]) }, %dict.keys); + return @res; +} + +#| Run test cases +multi sub MAIN() { + use Test; + plan 3; + + is sort(merge-items([ [1,1], [2,1], [3,2] ], [ [2,2], [1,3] ])), + [ [1,4], [2,3], [3,2] ], 'works for [ [1,4], [2,3], [3,2] ]'; + is sort(merge-items([ [1,2], [2,3], [1,3], [3,2] ], [ [3,1], [1,3] ])), + [ [1,8], [2,3], [3,3] ], 'works for [ [1,8], [2,3], [3,3] ]'; + is sort(merge-items([ [1,1], [2,2], [3,3] ], [ [2,3], [2,4] ])), + [ [1,1], [2,9], [3,3] ], 'works for [ [1,1], [2,9], [3,3] ]'; +} |
