diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-09-01 14:41:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-01 14:41:06 +0100 |
| commit | cfda72e9fd99a800a45f48e9b5ef01874305c9f2 (patch) | |
| tree | cc16c40d0f8f831cc795ba2536f16fb2a9c6c660 | |
| parent | f047373050f784baf1ee339c18e9d0b5bae2a348 (diff) | |
| parent | 29c7f940a078835d650c15c7fac2eaa7754f3cfc (diff) | |
| download | perlweeklychallenge-club-cfda72e9fd99a800a45f48e9b5ef01874305c9f2.tar.gz perlweeklychallenge-club-cfda72e9fd99a800a45f48e9b5ef01874305c9f2.tar.bz2 perlweeklychallenge-club-cfda72e9fd99a800a45f48e9b5ef01874305c9f2.zip | |
Merge pull request #12609 from mahnkong/challenge-337
Challenge 337
| -rw-r--r-- | challenge-337/mahnkong/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-337/mahnkong/perl/ch-2.pl | 44 |
2 files changed, 67 insertions, 0 deletions
diff --git a/challenge-337/mahnkong/perl/ch-1.pl b/challenge-337/mahnkong/perl/ch-1.pl new file mode 100644 index 0000000000..d34b702275 --- /dev/null +++ b/challenge-337/mahnkong/perl/ch-1.pl @@ -0,0 +1,23 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run(@nums) { + my @result; + for (my $i = 0; $i <= $#nums; $i++) { + $result[$i] = 0; + for (my $j = 0; $j <= $#nums; $j++) { + next if $i == $j; + $result[$i] += 1 if $nums[$i] >= $nums[$j]; + } + } + + return [ @result ]; +} + +is_deeply(run(6, 5, 4, 8), [2, 1, 0, 3], "Example 1"); +is_deeply(run(7, 7, 7, 7), [3, 3, 3, 3], "Example 2"); +is_deeply(run(5, 4, 3, 2, 1), [4, 3, 2, 1, 0], "Example 3"); +is_deeply(run(-1, 0, 3, -2, 1), [1, 2, 4, 0, 3], "Example 4"); +is_deeply(run(0, 1, 1, 2, 0), [1, 3, 3, 4, 1], "Example 5"); diff --git a/challenge-337/mahnkong/perl/ch-2.pl b/challenge-337/mahnkong/perl/ch-2.pl new file mode 100644 index 0000000000..7e72e7a2aa --- /dev/null +++ b/challenge-337/mahnkong/perl/ch-2.pl @@ -0,0 +1,44 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run($row, $col, $locations) { + my @matrix; + my $result = 0; + + # Setup + foreach (1..$row) { + my @col; + foreach (1..$col) { + push @col, 0; + } + push @matrix, [ @col ]; + } + + foreach my $location (@$locations) { + for (my $i = 0; $i <= $#matrix; $i++) { + if ($i == $location->[0]) { + my $row = $matrix[$i]; + for (my $j = 0; $j <= $#$row; $j++) { + $row->[$j] += 1; + } + } + $matrix[$i]->[$location->[1]] += 1; + } + } + + foreach my $row (@matrix) { + foreach my $item (@$row) { + $result += 1 if $item % 2 != 0; + } + } + + return $result; +} + +is(run(2, 3, [[0,1],[1,1]]), 6, "Example 1"); +is(run(2, 2, [[1,1],[0,0]]), 0, "Example 2"); +is(run(3, 3, [[0,0],[1,2],[2,1]]), 0, "Example 3"); +is(run(1, 5, [[0,2],[0,4]]), 2, "Example 4"); +is(run(4, 2, [[1,0],[3,1],[2,0],[0,1]]), 8, "Example 5"); |
