diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-10-19 21:16:56 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-19 21:16:56 +0100 |
| commit | f71fd04705398dc5bca5d4f78a1aa254d97d5abd (patch) | |
| tree | a3ec193e819608d62c4fe5aada4160560da76c3e | |
| parent | 1b363b218726b8dda3de28481dc697d8d585bd3f (diff) | |
| parent | 31372be9fef1c1277caf802b23e8ab3389898e70 (diff) | |
| download | perlweeklychallenge-club-f71fd04705398dc5bca5d4f78a1aa254d97d5abd.tar.gz perlweeklychallenge-club-f71fd04705398dc5bca5d4f78a1aa254d97d5abd.tar.bz2 perlweeklychallenge-club-f71fd04705398dc5bca5d4f78a1aa254d97d5abd.zip | |
Merge pull request #12871 from wambash/challenge-week-343
solutions week 343
| -rw-r--r-- | challenge-343/wambash/raku/ch-1.raku | 32 | ||||
| -rw-r--r-- | challenge-343/wambash/raku/ch-2.raku | 41 |
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-343/wambash/raku/ch-1.raku b/challenge-343/wambash/raku/ch-1.raku new file mode 100644 index 0000000000..4bdc5aa647 --- /dev/null +++ b/challenge-343/wambash/raku/ch-1.raku @@ -0,0 +1,32 @@ +#!/usr/bin/env raku + +sub zero-friend (+nums) { + nums + andthen .map: *.abs + andthen .min +} + +multi MAIN (Bool :test($)!) { + use Test; + + # Example cases from the challenge + is zero-friend((4, 2, -1, 3, -2)), 1, 'closest to zero are -1 (1 away) and 2 (2 away) → result 1'; + is zero-friend((-5, 5, -3, 3, -1, 1)), 1, 'closest are -1 and 1 → distance 1'; + is zero-friend((7, -3, 0, 2, -8)), 0, 'exact zero present → distance 0'; + is zero-friend((-2, -5, -1, -8)), 1, 'closest are -1 (1 away) and -2 (2 away) → result 1'; + is zero-friend((-2, 2, -4, 4, -1, 1)), 1, 'closest are -1 and 1 → distance 1'; + + # Additional edge cases + is zero-friend((0)), 0, 'single element zero → distance 0'; + is zero-friend((10)), 10, 'single positive number → distance 10'; + is zero-friend((-7)), 7, 'single negative number → distance 7'; + is zero-friend((100, -99)), 99, 'closest is -99 → distance 99'; + + done-testing; +} + + + +multi MAIN (+nums) { + say zero-friend nums; +} diff --git a/challenge-343/wambash/raku/ch-2.raku b/challenge-343/wambash/raku/ch-2.raku new file mode 100644 index 0000000000..663bcd20e8 --- /dev/null +++ b/challenge-343/wambash/raku/ch-2.raku @@ -0,0 +1,41 @@ +#!/usr/bin/env raku + +sub champion-team (+grid) { + my %adepts := ( + grid + andthen .max: *.cache.sum, :p + andthen .Map + ); + + %adepts + andthen .nodemap: { $_[%adepts.keys] }\ + andthen .max: *.value.sum + andthen .key +} + +multi MAIN (Bool :test($)!) { + use Test; + + # Example cases from the challenge + is champion-team([ [0,1,1], [0,0,1], [0,0,0] ]), 0, + 'Team 0 beats Team 1 and 2 → champion 0'; + is champion-team([ [0,1,0,0], [0,0,0,0], [1,1,0,0], [1,1,1,0] ]), 3, + 'Team 3 beats everyone → champion 3'; + is champion-team([ [0,1,0,1], [0,0,1,1], [1,0,0,0], [0,0,1,0] ]), 0, + 'Teams 0 and 1 tie with 2 wins, but 0 beats 1 → champion 0'; + is champion-team([ [0,1,1], [0,0,0], [0,1,0] ]), 0, + 'Team 0 beats both others → champion 0'; + is champion-team([ [0,0,0,0,0], [1,0,0,0,0], [1,1,0,1,1], [1,1,0,0,0], [1,1,0,1,0] ]), 2, + 'Team 2 beats almost everyone → champion 2'; + + # Additional edge cases + is champion-team([ [0] ]), 0, 'single team → champion 0'; + is champion-team([ [0,1], [0,0] ]), 0, 'two teams: team 0 beats team 1 → champion 0'; + is champion-team([ [0,0], [1,0] ]), 1, 'two teams: team 1 beats team 0 → champion 1'; + + done-testing; +} + +multi MAIN (+grid) { + say champion-team grid».comb: / 0|1 /; +} |
