diff options
| -rw-r--r-- | challenge-338/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-338/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-338/packy-anderson/elixir/ch-1.exs | 40 | ||||
| -rwxr-xr-x | challenge-338/packy-anderson/elixir/ch-2.exs | 30 | ||||
| -rwxr-xr-x | challenge-338/packy-anderson/perl/ch-1.pl | 42 | ||||
| -rwxr-xr-x | challenge-338/packy-anderson/perl/ch-2.pl | 31 | ||||
| -rwxr-xr-x | challenge-338/packy-anderson/python/ch-1.py | 40 | ||||
| -rwxr-xr-x | challenge-338/packy-anderson/python/ch-2.py | 29 | ||||
| -rwxr-xr-x | challenge-338/packy-anderson/raku/ch-1.raku | 38 | ||||
| -rwxr-xr-x | challenge-338/packy-anderson/raku/ch-2.raku | 29 |
10 files changed, 281 insertions, 1 deletions
diff --git a/challenge-338/packy-anderson/README.md b/challenge-338/packy-anderson/README.md index 8dd33a6d4c..28dc9f720c 100644 --- a/challenge-338/packy-anderson/README.md +++ b/challenge-338/packy-anderson/README.md @@ -23,4 +23,4 @@ ## Blog Post -[Perl Weekly Challenge: Small, but Oddly Current](https://packy.dardan.com/b/aR) +[Perl Weekly Challenge: Maxwell's Silver Highest](https://packy.dardan.com/b/b0) diff --git a/challenge-338/packy-anderson/blog.txt b/challenge-338/packy-anderson/blog.txt new file mode 100644 index 0000000000..6b234ec198 --- /dev/null +++ b/challenge-338/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/b0
\ No newline at end of file diff --git a/challenge-338/packy-anderson/elixir/ch-1.exs b/challenge-338/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..013e7a3b9b --- /dev/null +++ b/challenge-338/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,40 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def highest_row(matrix) do + Enum.map(matrix, fn row -> Enum.sum(row) end) |> Enum.max + end + + def solution(matrix) do + m = Enum.map(matrix, fn row -> Enum.join(row, ", ") end) + |> Enum.join(",\n ") + + IO.puts("Input: @ints = (#{m})") + IO.puts("Output: #{highest_row(matrix)}") + end +end + +IO.puts("Example 1:") +PWC.solution([[4, 4, 4, 4], + [10, 0, 0, 0], + [2, 2, 2, 9]]) + +IO.puts("\nExample 2:") +PWC.solution([[1, 5], + [7, 3], + [3, 5]]) + +IO.puts("\nExample 3:") +PWC.solution([[1, 2, 3], + [3, 2, 1]]) + +IO.puts("\nExample 4:") +PWC.solution([[2, 8, 7], + [7, 1, 3], + [1, 9, 5]]) + +IO.puts("\nExample 5:") +PWC.solution([[10, 20, 30], + [5, 5, 5], + [0, 100, 0], + [25, 25, 25]]) diff --git a/challenge-338/packy-anderson/elixir/ch-2.exs b/challenge-338/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..9791a61aa7 --- /dev/null +++ b/challenge-338/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,30 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def max_distance(arr1, arr2) do + {min1, max1} = {Enum.min(arr1), Enum.max(arr1)} + {min2, max2} = {Enum.min(arr2), Enum.max(arr2)} + Enum.max([abs(max1 - min2), abs(max2 - min1)]) + end + + def solution(arr1, arr2) do + IO.puts("Input: @arr1 = (" <> Enum.join(arr1, ", ") <> ")") + IO.puts(" @arr2 = (" <> Enum.join(arr2, ", ") <> ")") + IO.puts("Output: #{max_distance(arr1, arr2)}") + end +end + +IO.puts("Example 1:") +PWC.solution([4, 5, 7], [9, 1, 3, 4]) + +IO.puts("\nExample 2:") +PWC.solution([2, 3, 5, 4], [3, 2, 5, 5, 8, 7]) + +IO.puts("\nExample 3:") +PWC.solution([2, 1, 11, 3], [2, 5, 10, 2]) + +IO.puts("\nExample 4:") +PWC.solution([1, 2, 3], [3, 2, 1]) + +IO.puts("\nExample 5:") +PWC.solution([1, 0, 2, 3], [5, 0]) diff --git a/challenge-338/packy-anderson/perl/ch-1.pl b/challenge-338/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..33392ccdcc --- /dev/null +++ b/challenge-338/packy-anderson/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl +use v5.40; + +use List::AllUtils qw( max sum ); + +sub highestRow($matrix) { + max map { sum @$_ } @$matrix; +} + +sub solution($matrix) { + my $m = join( + ",\n ", + map { '[' . join(', ', @$_) . ']'} @$matrix + ); + say "Input: \@matrix = ($m)"; + say "Output: @{[highestRow($matrix)]}"; +} + +say "Example 1:"; +solution([[4, 4, 4, 4], + [10, 0, 0, 0], + [2, 2, 2, 9]]); + +say "\nExample 2:"; +solution([[1, 5], + [7, 3], + [3, 5]]); + +say "\nExample 3:"; +solution([[1, 2, 3], + [3, 2, 1]]); + +say "\nExample 4:"; +solution([[2, 8, 7], + [7, 1, 3], + [1, 9, 5]]); + +say "\nExample 5:"; +solution([[10, 20, 30], + [5, 5, 5], + [0, 100, 0], + [25, 25, 25]]); diff --git a/challenge-338/packy-anderson/perl/ch-2.pl b/challenge-338/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..66a7934b67 --- /dev/null +++ b/challenge-338/packy-anderson/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl +use v5.40; + +use List::AllUtils qw( min max ); + +sub maxDistance($arr1, $arr2) { + my ($min1, $max1) = (min(@$arr1), max(@$arr1)); + my ($min2, $max2) = (min(@$arr2), max(@$arr2)); + max(abs($max1 - $min2), abs($max2 - $min1)); +} + +sub solution($arr1, $arr2) { + say 'Input: @arr1 = (' . join(', ', @$arr1) . ')'; + say ' @arr2 = (' . join(', ', @$arr2) . ')'; + say 'Output: ' . maxDistance($arr1, $arr2); +} + +say "Example 1:"; +solution([4, 5, 7], [9, 1, 3, 4]); + +say "\nExample 2:"; +solution([2, 3, 5, 4], [3, 2, 5, 5, 8, 7]); + +say "\nExample 3:"; +solution([2, 1, 11, 3], [2, 5, 10, 2]); + +say "\nExample 4:"; +solution([1, 2, 3], [3, 2, 1]); + +say "\nExample 5:"; +solution([1, 0, 2, 3], [5, 0]); diff --git a/challenge-338/packy-anderson/python/ch-1.py b/challenge-338/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..3c5708c1a8 --- /dev/null +++ b/challenge-338/packy-anderson/python/ch-1.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +def highest_row(matrix): + return max( [ sum(row) for row in matrix ] ) + +def int_join(joiner, arr): + return joiner.join(map(lambda i: str(i), arr)) + +def solution(matrix): + m = ",\n ".join( + [ "[" + int_join(", ", row) + "]" for row in matrix ] + ) + print(f'Input: @matrix = ({m})') + print(f'Output: {highest_row(matrix)}') + + +print('Example 1:') +solution([[4, 4, 4, 4], + [10, 0, 0, 0], + [2, 2, 2, 9]]) + +print('\nExample 2:') +solution([[1, 5], + [7, 3], + [3, 5]]) + +print('\nExample 3:') +solution([[1, 2, 3], + [3, 2, 1]]) + +print('\nExample 4:') +solution([[2, 8, 7], + [7, 1, 3], + [1, 9, 5]]) + +print('\nExample 5:') +solution([[10, 20, 30], + [5, 5, 5], + [0, 100, 0], + [25, 25, 25]]) diff --git a/challenge-338/packy-anderson/python/ch-2.py b/challenge-338/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..7b27764bc0 --- /dev/null +++ b/challenge-338/packy-anderson/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +def max_distance(arr1, arr2): + (min1, max1) = (min(arr1), max(arr1)) + (min2, max2) = (min(arr2), max(arr2)) + return max(abs(max1 - min2), abs(max2 - min1)) + +def int_join(joiner, arr): + return joiner.join(map(lambda i: str(i), arr)) + +def solution(arr1, arr2): + print(f'Input: @arr1 = ({int_join(", ", arr1)})') + print(f' @arr2 = ({int_join(", ", arr2)})') + print(f'Output: {max_distance(arr1, arr2)}') + +print('Example 1:') +solution([4, 5, 7], [9, 1, 3, 4]) + +print('\nExample 2:') +solution([2, 3, 5, 4], [3, 2, 5, 5, 8, 7]) + +print('\nExample 3:') +solution([2, 1, 11, 3], [2, 5, 10, 2]) + +print('\nExample 4:') +solution([1, 2, 3], [3, 2, 1]) + +print('\nExample 5:') +solution([1, 0, 2, 3], [5, 0]) diff --git a/challenge-338/packy-anderson/raku/ch-1.raku b/challenge-338/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..4a350c2673 --- /dev/null +++ b/challenge-338/packy-anderson/raku/ch-1.raku @@ -0,0 +1,38 @@ +#!/usr/bin/env raku +use v6; + +sub highestRow(@matrix) { + @matrix.map({ $_.sum }).max +} + +sub solution(@matrix) { + my $m = @matrix.map({ '[' ~ $_.join(', ') ~ ']'}) + .join(",\n "); + say "Input: \@matrix = ($m)"; + say "Output: {highestRow(@matrix)}"; +} + +say "Example 1:"; +solution([[4, 4, 4, 4], + [10, 0, 0, 0], + [2, 2, 2, 9]]); + +say "\nExample 2:"; +solution([[1, 5], + [7, 3], + [3, 5]]); + +say "\nExample 3:"; +solution([[1, 2, 3], + [3, 2, 1]]); + +say "\nExample 4:"; +solution([[2, 8, 7], + [7, 1, 3], + [1, 9, 5]]); + +say "\nExample 5:"; +solution([[10, 20, 30], + [5, 5, 5], + [0, 100, 0], + [25, 25, 25]]); diff --git a/challenge-338/packy-anderson/raku/ch-2.raku b/challenge-338/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..07e769c42c --- /dev/null +++ b/challenge-338/packy-anderson/raku/ch-2.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env raku +use v6; + +sub maxDistance(@arr1, @arr2) { + my ($min1, $max1) = (@arr1.min, @arr1.max); + my ($min2, $max2) = (@arr2.min, @arr2.max); + max(abs($max1 - $min2), abs($max2 - $min1)); +} + +sub solution(@arr1, @arr2) { + say 'Input: @arr1 = (' ~ @arr1.join(', ') ~ ')'; + say ' @arr2 = (' ~ @arr2.join(', ') ~ ')'; + say 'Output: ' ~ maxDistance(@arr1, @arr2); +} + +say "Example 1:"; +solution([4, 5, 7], [9, 1, 3, 4]); + +say "\nExample 2:"; +solution([2, 3, 5, 4], [3, 2, 5, 5, 8, 7]); + +say "\nExample 3:"; +solution([2, 1, 11, 3], [2, 5, 10, 2]); + +say "\nExample 4:"; +solution([1, 2, 3], [3, 2, 1]); + +say "\nExample 5:"; +solution([1, 0, 2, 3], [5, 0]); |
