aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2025-10-13 08:40:26 +0200
committerE. Choroba <choroba@matfyz.cz>2025-10-13 08:40:26 +0200
commitc4ee1dff34ffe41fab580a72ffca5a51aa8075ec (patch)
tree1f23bc5f4c3bb8646a50fb7ab074979323c9b53d
parent35a27e16cb743acd46c1f315766530dc991ac587 (diff)
downloadperlweeklychallenge-club-c4ee1dff34ffe41fab580a72ffca5a51aa8075ec.tar.gz
perlweeklychallenge-club-c4ee1dff34ffe41fab580a72ffca5a51aa8075ec.tar.bz2
perlweeklychallenge-club-c4ee1dff34ffe41fab580a72ffca5a51aa8075ec.zip
Add solutions to 343: Zero Friend & Champion Team by E. Choroba
-rwxr-xr-xchallenge-343/e-choroba/perl/ch-1.pl18
-rwxr-xr-xchallenge-343/e-choroba/perl/ch-2.pl52
2 files changed, 70 insertions, 0 deletions
diff --git a/challenge-343/e-choroba/perl/ch-1.pl b/challenge-343/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..ea6e3a6536
--- /dev/null
+++ b/challenge-343/e-choroba/perl/ch-1.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub zero_friend(@nums) {
+ my $zf = abs shift @nums;
+ abs $_ < $zf and $zf = abs for @nums;
+ return $zf
+}
+
+use Test::More tests => 5;
+
+is zero_friend(4, 2, -1, 3, -2), 1, 'Example 1';
+is zero_friend(-5, 5, -3, 3, -1, 1), 1, 'Example 2';
+is zero_friend(7, -3, 0, 2, -8), 0, 'Example 3';
+is zero_friend(-2, -5, -1, -8), 1, 'Example 4';
+is zero_friend(-2, 2, -4, 4, -1, 1), 1, 'Example 5';
diff --git a/challenge-343/e-choroba/perl/ch-2.pl b/challenge-343/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..8d1681722d
--- /dev/null
+++ b/challenge-343/e-choroba/perl/ch-2.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub champion_team(@grid) {
+ my $max = 0;
+ my %wins;
+ for my $y (0 .. $#grid) {
+ for my $x (0 .. $#{ $grid[$y] }) {
+ my $w = $grid[$y][$x] ? $y : $x;
+ $max = $w if ++$wins{$w} > $wins{$max};
+ }
+ }
+ return "Team $max"
+}
+
+use Test::More tests => 5;
+
+is champion_team(
+ [0, 1, 1],
+ [0, 0, 1],
+ [0, 0, 0],
+ ), 'Team 0', 'Example 1';
+
+is champion_team(
+ [0, 1, 0, 0],
+ [0, 0, 0, 0],
+ [1, 1, 0, 0],
+ [1, 1, 1, 0],
+ ), 'Team 3', 'Example 2';
+
+is champion_team(
+ [0, 1, 0, 1],
+ [0, 0, 1, 1],
+ [1, 0, 0, 0],
+ [0, 0, 1, 0],
+ ), 'Team 0', 'Example 3';
+
+is champion_team(
+ [0, 1, 1],
+ [0, 0, 0],
+ [0, 1, 0],
+ ), 'Team 0', 'Example 4';
+
+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],
+ ), 'Team 2', 'Example 5';