diff options
| -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"); |
