From c4ee1dff34ffe41fab580a72ffca5a51aa8075ec Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 13 Oct 2025 08:40:26 +0200 Subject: Add solutions to 343: Zero Friend & Champion Team by E. Choroba --- challenge-343/e-choroba/perl/ch-1.pl | 18 +++++++++++++ challenge-343/e-choroba/perl/ch-2.pl | 52 ++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100755 challenge-343/e-choroba/perl/ch-1.pl create mode 100755 challenge-343/e-choroba/perl/ch-2.pl 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'; -- cgit