aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-343/packy-anderson/README.md2
-rw-r--r--challenge-343/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-343/packy-anderson/elixir/ch-1.exs27
-rwxr-xr-xchallenge-343/packy-anderson/elixir/ch-2.exs83
-rwxr-xr-xchallenge-343/packy-anderson/perl/ch-1.pl29
-rwxr-xr-xchallenge-343/packy-anderson/perl/ch-2.pl87
-rwxr-xr-xchallenge-343/packy-anderson/python/ch-1.py26
-rwxr-xr-xchallenge-343/packy-anderson/python/ch-2.py76
-rwxr-xr-xchallenge-343/packy-anderson/raku/ch-1.raku26
-rwxr-xr-xchallenge-343/packy-anderson/raku/ch-2.raku85
10 files changed, 441 insertions, 1 deletions
diff --git a/challenge-343/packy-anderson/README.md b/challenge-343/packy-anderson/README.md
index 29039bec47..57c0477cd1 100644
--- a/challenge-343/packy-anderson/README.md
+++ b/challenge-343/packy-anderson/README.md
@@ -23,4 +23,4 @@
## Blog Post
-[Perl Weekly Challenge: Balanced Adagio for Strings](https://packy.dardan.com/b/cQ)
+[Perl Weekly Challenge: Ze-ro the CHAMPIONS!](https://packy.dardan.com/b/cg)
diff --git a/challenge-343/packy-anderson/blog.txt b/challenge-343/packy-anderson/blog.txt
new file mode 100644
index 0000000000..e9da1aff8e
--- /dev/null
+++ b/challenge-343/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/cg \ No newline at end of file
diff --git a/challenge-343/packy-anderson/elixir/ch-1.exs b/challenge-343/packy-anderson/elixir/ch-1.exs
new file mode 100755
index 0000000000..7b0e9f1257
--- /dev/null
+++ b/challenge-343/packy-anderson/elixir/ch-1.exs
@@ -0,0 +1,27 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def zero_friend(nums) do
+ nums |> Enum.map(fn n -> abs(n) end) |> Enum.min
+ end
+
+ def solution(nums) do
+ IO.puts("Input: @nums = (" <> Enum.join(nums, ", ") <> ")")
+ IO.puts("Output: #{zero_friend(nums)}")
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([4, 2, -1, 3, -2])
+
+IO.puts("\nExample 2:")
+PWC.solution([-5, 5, -3, 3, -1, 1])
+
+IO.puts("\nExample 3:")
+PWC.solution([7, -3, 0, 2, -8])
+
+IO.puts("\nExample 4:")
+PWC.solution([-2, -5, -1, -8])
+
+IO.puts("\nExample 5:")
+PWC.solution([-2, 2, -4, 4, -1, 1])
diff --git a/challenge-343/packy-anderson/elixir/ch-2.exs b/challenge-343/packy-anderson/elixir/ch-2.exs
new file mode 100755
index 0000000000..f3a9c1de3d
--- /dev/null
+++ b/challenge-343/packy-anderson/elixir/ch-2.exs
@@ -0,0 +1,83 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def stronger(i, j, grid) do
+ cond do
+ grid |> Enum.at(i) |> Enum.at(j) == 1 -> i
+ grid |> Enum.at(i) |> Enum.at(j) == 0 -> j
+ end
+ end
+
+ def reduce_fn(row, {best, max, team}) do
+ sum = Enum.sum(row)
+ {best, max} = cond do
+ sum > max ->
+ {[team], sum}
+ sum == max ->
+ {best ++ [team], max}
+ true ->
+ {best, max}
+ end
+ {row, {best, max, team+1}}
+ end
+
+ def champion_team(grid) do
+ {_, {best, _, _}} =
+ Enum.map_reduce(grid, {[], 0, 0}, &reduce_fn/2)
+ if length(best) == 1 do
+ best |> List.first
+ else
+ stronger(List.first(best), List.last(best), grid)
+ end
+ end
+
+ def format_matrix(grid) do
+ grid
+ |> Enum.map(fn row -> Enum.join(row, ", ") end)
+ |> Enum.join(",\n ")
+ end
+
+ def solution(grid) do
+ IO.puts("Input: @grid = #{format_matrix(grid)}")
+ IO.puts("Output: #{champion_team(grid)}")
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([
+ [0, 1, 1],
+ [0, 0, 1],
+ [0, 0, 0]
+])
+
+IO.puts("\nExample 2:")
+PWC.solution([
+ [0, 1, 0, 0],
+ [0, 0, 0, 0],
+ [1, 1, 0, 0],
+ [1, 1, 1, 0]
+])
+
+IO.puts("\nExample 3:")
+PWC.solution([
+ [0, 1, 0, 1],
+ [0, 0, 1, 1],
+ [1, 0, 0, 0],
+ [0, 0, 1, 0]
+])
+
+IO.puts("\nExample 4:")
+PWC.solution([
+ [0, 1, 1],
+ [0, 0, 0],
+ [0, 1, 0]
+])
+
+IO.puts("\nExample 5:")
+PWC.solution([
+ [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]
+])
diff --git a/challenge-343/packy-anderson/perl/ch-1.pl b/challenge-343/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..29400d2cc8
--- /dev/null
+++ b/challenge-343/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+use v5.40;
+
+use List::AllUtils qw( min );
+
+sub zeroFriend(@nums) {
+ min map { abs($_) } @nums;
+}
+
+sub solution($nums) {
+ say 'Input: @nums = (' . join(', ', @$nums) . ')';
+ say 'Output: ' . zeroFriend(@$nums);
+}
+
+say "Example 1:";
+solution([4, 2, -1, 3, -2]);
+
+say "\nExample 2:";
+solution([-5, 5, -3, 3, -1, 1]);
+
+say "\nExample 3:";
+solution([7, -3, 0, 2, -8]);
+
+say "\nExample 4:";
+solution([-2, -5, -1, -8]);
+
+say "\nExample 5:";
+solution([-2, 2, -4, 4, -1, 1]);
+
diff --git a/challenge-343/packy-anderson/perl/ch-2.pl b/challenge-343/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..f77f02a201
--- /dev/null
+++ b/challenge-343/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,87 @@
+#!/usr/bin/env perl
+use v5.40;
+
+use List::AllUtils qw( sum );
+
+sub stronger($i, $j, @grid) {
+ return $i if $grid[$i][$j] == 1;
+ return $j if $grid[$i][$j] == 0;
+}
+
+sub championTeam(@grid) {
+ my @best;
+ my $max = 0;
+ foreach my $team ( 0 .. $#grid ) {
+ my $sum = sum(@{$grid[$team]});
+ if ($sum > $max) {
+ @best = ($team);
+ $max = $sum;
+ }
+ elsif ($sum == $max) {
+ push @best, $team;
+ }
+ }
+ if (@best == 1) {
+ return $best[0];
+ }
+ my ($i, $j) = @best;
+ return stronger($i, $j, @grid);
+}
+
+sub formatMatrix($matrix, $indent=15) {
+ my @output;
+ foreach my $row (@$matrix) {
+ my $output_row = q{ } x $indent . " [";
+ $output_row .= join(', ', map { sprintf "%1d", $_ } @$row) . ']';
+ push @output, $output_row;
+ }
+ return "[\n"
+ . join(",\n", @output)
+ . "\n"
+ . q{ } x $indent . "]";
+}
+
+sub solution($grid) {
+ say 'Input: @grid = ' . formatMatrix($grid);
+ say 'Output: Team ' . championTeam(@$grid);
+}
+
+say "Example 1:";
+solution([
+ [0, 1, 1],
+ [0, 0, 1],
+ [0, 0, 0]
+]);
+
+say "\nExample 2:";
+solution([
+ [0, 1, 0, 0],
+ [0, 0, 0, 0],
+ [1, 1, 0, 0],
+ [1, 1, 1, 0]
+]);
+
+say "\nExample 3:";
+solution([
+ [0, 1, 0, 1],
+ [0, 0, 1, 1],
+ [1, 0, 0, 0],
+ [0, 0, 1, 0]
+]);
+
+say "\nExample 4:";
+solution([
+ [0, 1, 1],
+ [0, 0, 0],
+ [0, 1, 0]
+]);
+
+say "\nExample 5:";
+solution([
+ [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]
+]);
+
diff --git a/challenge-343/packy-anderson/python/ch-1.py b/challenge-343/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..74d198e2bb
--- /dev/null
+++ b/challenge-343/packy-anderson/python/ch-1.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+
+def zero_friend(nums):
+ return min([ abs(n) for n in nums])
+
+def int_join(joiner, arr):
+ return joiner.join(map(lambda i: str(i), arr))
+
+def solution(nums):
+ print(f'Input: @nums = ({int_join(", ", nums)})')
+ print(f'Output: {zero_friend(nums)}')
+
+print('Example 1:')
+solution([4, 2, -1, 3, -2])
+
+print('\nExample 2:')
+solution([-5, 5, -3, 3, -1, 1])
+
+print('\nExample 3:')
+solution([7, -3, 0, 2, -8])
+
+print('\nExample 4:')
+solution([-2, -5, -1, -8])
+
+print('\nExample 5:')
+solution([-2, 2, -4, 4, -1, 1])
diff --git a/challenge-343/packy-anderson/python/ch-2.py b/challenge-343/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..f7e32cf00c
--- /dev/null
+++ b/challenge-343/packy-anderson/python/ch-2.py
@@ -0,0 +1,76 @@
+#!/usr/bin/env python
+
+def stronger(i, j, grid):
+ if grid[i][j] == 1: return i
+ if grid[i][j] == 0: return j
+
+def champion_team(grid):
+ best = []
+ my_max = 0
+ for team in range(len(grid)):
+ my_sum = sum(grid[team])
+ if my_sum > my_max:
+ best = [team]
+ my_max = my_sum
+ elif my_sum == my_max:
+ best.append(team)
+ if len(best) == 1:
+ return best[0]
+ i, j = best
+ return stronger(i, j, grid)
+
+def format_matrix(matrix, indent=15):
+ output = []
+ for row in matrix:
+ output_row = ' ' * indent + ' ['
+ output_row += ', '.join(map(lambda i: str(i), row))
+ output_row += ']'
+ output.append(output_row)
+
+ return(
+ "[\n" + ",\n".join(output) + "\n" +
+ ' ' * indent + ']'
+ )
+
+def solution(grid):
+ print(f'Input: @grid = {format_matrix(grid)}')
+ print(f'Output: {champion_team(grid)}')
+
+print('Example 1:')
+solution([
+ [0, 1, 1],
+ [0, 0, 1],
+ [0, 0, 0]
+])
+
+print('\nExample 2:')
+solution([
+ [0, 1, 0, 0],
+ [0, 0, 0, 0],
+ [1, 1, 0, 0],
+ [1, 1, 1, 0]
+])
+
+print('\nExample 3:')
+solution([
+ [0, 1, 0, 1],
+ [0, 0, 1, 1],
+ [1, 0, 0, 0],
+ [0, 0, 1, 0]
+])
+
+print('\nExample 4:')
+solution([
+ [0, 1, 1],
+ [0, 0, 0],
+ [0, 1, 0]
+])
+
+print('\nExample 5:')
+solution([
+ [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]
+])
diff --git a/challenge-343/packy-anderson/raku/ch-1.raku b/challenge-343/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..689399904c
--- /dev/null
+++ b/challenge-343/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,26 @@
+#!/usr/bin/env raku
+use v6;
+
+sub zeroFriend(@nums) {
+ @nums.map({ abs($_) }).min;
+}
+
+sub solution(@nums) {
+ say 'Input: @nums = (' ~ @nums.join(', ') ~ ')';
+ say 'Output: ' ~ zeroFriend(@nums);
+}
+
+say "Example 1:";
+solution([4, 2, -1, 3, -2]);
+
+say "\nExample 2:";
+solution([-5, 5, -3, 3, -1, 1]);
+
+say "\nExample 3:";
+solution([7, -3, 0, 2, -8]);
+
+say "\nExample 4:";
+solution([-2, -5, -1, -8]);
+
+say "\nExample 5:";
+solution([-2, 2, -4, 4, -1, 1]);
diff --git a/challenge-343/packy-anderson/raku/ch-2.raku b/challenge-343/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..11a147d7af
--- /dev/null
+++ b/challenge-343/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,85 @@
+#!/usr/bin/env raku
+use v6;
+
+sub stronger($i, $j, @grid) {
+ return $i if @grid[$i][$j] == 1;
+ return $j if @grid[$i][$j] == 0;
+}
+
+sub championTeam(@grid) {
+ my @best;
+ my $max = 0;
+ for @grid.keys -> $team {
+ my $sum = @grid[$team].sum;
+ if ($sum > $max) {
+ @best = ($team);
+ $max = $sum;
+ }
+ elsif ($sum == $max) {
+ @best.push($team);
+ }
+ }
+ if (@best.elems == 1) {
+ return @best[0];
+ }
+ my ($i, $j) = @best;
+ return stronger($i, $j, @grid);
+}
+
+sub formatMatrix(@matrix, $indent=15) {
+ my @output;
+ for @matrix -> @row {
+ my $output_row = q{ } x $indent ~ " [";
+ $output_row ~= @row.map({ sprintf "%1d", $_ })
+ .join(', ') ~ "]";
+ @output.push($output_row);
+ }
+ return "[\n"
+ ~ @output.join(",\n")
+ ~ "\n"
+ ~ q{ } x $indent ~ "]";
+}
+
+sub solution(@grid) {
+ say 'Input: @grid = ' ~ formatMatrix(@grid);
+ say 'Output: Team ' ~ championTeam(@grid);
+}
+
+say "Example 1:";
+solution([
+ [0, 1, 1],
+ [0, 0, 1],
+ [0, 0, 0]
+]);
+
+say "\nExample 2:";
+solution([
+ [0, 1, 0, 0],
+ [0, 0, 0, 0],
+ [1, 1, 0, 0],
+ [1, 1, 1, 0]
+]);
+
+say "\nExample 3:";
+solution([
+ [0, 1, 0, 1],
+ [0, 0, 1, 1],
+ [1, 0, 0, 0],
+ [0, 0, 1, 0]
+]);
+
+say "\nExample 4:";
+solution([
+ [0, 1, 1],
+ [0, 0, 0],
+ [0, 1, 0]
+]);
+
+say "\nExample 5:";
+solution([
+ [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]
+]);