From f795a520a8025e1b1f0569a8d0743061537a8d9c Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Tue, 16 Sep 2025 23:28:22 -0400 Subject: Challenge 339 solutions by Packy Anderson * Raku that is finally Raku-ish * Perl * Python that kinda looks like Raku * Elixir that is starting to look like Elixir 1 blog post --- challenge-339/packy-anderson/README.md | 2 +- challenge-339/packy-anderson/blog.txt | 1 + challenge-339/packy-anderson/elixir/ch-1.exs | 30 ++++++++++++++++++++++ challenge-339/packy-anderson/elixir/ch-2.exs | 34 +++++++++++++++++++++++++ challenge-339/packy-anderson/perl/ch-1.pl | 28 ++++++++++++++++++++ challenge-339/packy-anderson/perl/ch-2.pl | 29 +++++++++++++++++++++ challenge-339/packy-anderson/python/ch-1.py | 29 +++++++++++++++++++++ challenge-339/packy-anderson/python/ch-2.py | 38 ++++++++++++++++++++++++++++ challenge-339/packy-anderson/raku/ch-1.raku | 28 ++++++++++++++++++++ challenge-339/packy-anderson/raku/ch-2.raku | 28 ++++++++++++++++++++ 10 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 challenge-339/packy-anderson/blog.txt create mode 100755 challenge-339/packy-anderson/elixir/ch-1.exs create mode 100755 challenge-339/packy-anderson/elixir/ch-2.exs create mode 100755 challenge-339/packy-anderson/perl/ch-1.pl create mode 100755 challenge-339/packy-anderson/perl/ch-2.pl create mode 100755 challenge-339/packy-anderson/python/ch-1.py create mode 100755 challenge-339/packy-anderson/python/ch-2.py create mode 100755 challenge-339/packy-anderson/raku/ch-1.raku create mode 100755 challenge-339/packy-anderson/raku/ch-2.raku diff --git a/challenge-339/packy-anderson/README.md b/challenge-339/packy-anderson/README.md index 28dc9f720c..1013ff24f7 100644 --- a/challenge-339/packy-anderson/README.md +++ b/challenge-339/packy-anderson/README.md @@ -23,4 +23,4 @@ ## Blog Post -[Perl Weekly Challenge: Maxwell's Silver Highest](https://packy.dardan.com/b/b0) +[Perl Weekly Challenge: Points? What's the diff?](https://packy.dardan.com/b/bN) diff --git a/challenge-339/packy-anderson/blog.txt b/challenge-339/packy-anderson/blog.txt new file mode 100644 index 0000000000..3094ad33e7 --- /dev/null +++ b/challenge-339/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/bN \ No newline at end of file diff --git a/challenge-339/packy-anderson/elixir/ch-1.exs b/challenge-339/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..97edb4ed54 --- /dev/null +++ b/challenge-339/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,30 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def max_diff(ints) do + ints = Enum.sort(ints, &(abs(&1) <= abs(&2))) + {a, b} = {Enum.at(ints, 0), Enum.at(ints, 1)} + {c, d} = {Enum.at(ints, -2), Enum.at(ints, -1)} + abs((a * b) - (c * d)) + end + + def solution(ints) do + IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")") + IO.puts("Output: #{max_diff(ints)}") + end +end + +IO.puts("Example 1:") +PWC.solution([5, 9, 3, 4, 6]) + +IO.puts("\nExample 2:") +PWC.solution([1, -2, 3, -4]) + +IO.puts("\nExample 3:") +PWC.solution([-3, -1, -2, -4]) + +IO.puts("\nExample 4:") +PWC.solution([10, 2, 0, 5, 1]) + +IO.puts("\nExample 5:") +PWC.solution([7, 8, 9, 10, 10]) diff --git a/challenge-339/packy-anderson/elixir/ch-2.exs b/challenge-339/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..271002bd5a --- /dev/null +++ b/challenge-339/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,34 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def add_max(x, {sum, max}) do + sum = sum + x + max = Enum.max([max, sum]) + { x, {sum, max} } + end + + def peak_point(gain) do + {_, {_, max}} = Enum.map_reduce(gain, {0, 0}, &add_max/2) + max + end + + def solution(gain) do + IO.puts("Input: @gain = (" <> Enum.join(gain, ", ") <> ")") + IO.puts("Output: #{peak_point(gain)}") + end +end + +IO.puts("Example 1:") +PWC.solution([-5, 1, 5, -9, 2]) + +IO.puts("\nExample 2:") +PWC.solution([10, 10, 10, -25]) + +IO.puts("\nExample 3:") +PWC.solution([3, -4, 2, 5, -6, 1]) + +IO.puts("\nExample 4:") +PWC.solution([-1, -2, -3, -4]) + +IO.puts("\nExample 5:") +PWC.solution([-10, 15, 5]) diff --git a/challenge-339/packy-anderson/perl/ch-1.pl b/challenge-339/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..f697645a51 --- /dev/null +++ b/challenge-339/packy-anderson/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use v5.40; + +sub maxDiff(@ints) { + my ($A, $B, $C, $D) = + +(sort { abs($a) <=> abs($b) } @ints)[0, 1, -2, -1]; + abs(($A * $B) - ($C * $D)) +} + +sub solution($ints) { + say 'Input: @ints = (' . join(', ', @$ints) . ')'; + say 'Output: ' . maxDiff(@$ints); +} + +say "Example 1:"; +solution([5, 9, 3, 4, 6]); + +say "\nExample 2:"; +solution([1, -2, 3, -4]); + +say "\nExample 3:"; +solution([-3, -1, -2, -4]); + +say "\nExample 4:"; +solution([10, 2, 0, 5, 1]); + +say "\nExample 5:"; +solution([7, 8, 9, 10, 10]); diff --git a/challenge-339/packy-anderson/perl/ch-2.pl b/challenge-339/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..ef39719bea --- /dev/null +++ b/challenge-339/packy-anderson/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl +use v5.40; + +use List::AllUtils qw( max reductions ); + +sub peakPoint(@gain) { + max reductions { $a + $b } 0, @gain; +} + +sub solution($gain) { + say 'Input: @gain = (' . join(', ', @$gain) . ')'; + say 'Output: ' . peakPoint(@$gain); +} + +say "Example 1:"; +solution([-5, 1, 5, -9, 2]); + +say "\nExample 2:"; +solution([10, 10, 10, -25]); + +say "\nExample 3:"; +solution([3, -4, 2, 5, -6, 1]); + +say "\nExample 4:"; +solution([-1, -2, -3, -4]); + +say "\nExample 5:"; +solution([-10, 15, 5]); + diff --git a/challenge-339/packy-anderson/python/ch-1.py b/challenge-339/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..4338ed01c4 --- /dev/null +++ b/challenge-339/packy-anderson/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +def max_diff(ints): + ints = sorted(ints, key=abs) + a, b = ints[0:2] + c, d = ints[-2:] + return abs((a * b) - (c * d)) + +def int_join(joiner, arr): + return joiner.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @ints = ({int_join(", ", ints)})') + print(f'Output: {max_diff(ints)}') + +print('Example 1:') +solution([5, 9, 3, 4, 6]) + +print('\nExample 2:') +solution([1, -2, 3, -4]) + +print('\nExample 3:') +solution([-3, -1, -2, -4]) + +print('\nExample 4:') +solution([10, 2, 0, 5, 1]) + +print('\nExample 5:') +solution([7, 8, 9, 10, 10]) diff --git a/challenge-339/packy-anderson/python/ch-2.py b/challenge-339/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..0387ea2f4e --- /dev/null +++ b/challenge-339/packy-anderson/python/ch-2.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +from functools import reduce + +from functools import reduce + +def add_max(x, y): + global max_alt + max_alt = max(max_alt, x+y) + return x+y + +def peak_point(gain): + global max_alt + max_alt = 0 # initialize global to 0 + reduce(add_max, gain) + return max_alt + +def int_join(joiner, arr): + return joiner.join(map(lambda i: str(i), arr)) + +def solution(gain): + print(f'Input: @gain = ({int_join(", ", gain)})') + print(f'Output: {peak_point(gain)}') + +print('Example 1:') +solution([-5, 1, 5, -9, 2]) + +print('\nExample 2:') +solution([10, 10, 10, -25]) + +print('\nExample 3:') +solution([3, -4, 2, 5, -6, 1]) + +print('\nExample 4:') +solution([-1, -2, -3, -4]) + +print('\nExample 5:') +solution([-10, 15, 5]) diff --git a/challenge-339/packy-anderson/raku/ch-1.raku b/challenge-339/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..694ca97147 --- /dev/null +++ b/challenge-339/packy-anderson/raku/ch-1.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku +use v6; + +sub maxDiff(@ints is copy) { + my ($a, $b, $c, $d) = + @ints.sort({ abs($^a) cmp abs($^b) })[0, 1, *-2, *-1]; + abs(($a * $b) - ($c * $d)) +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + say 'Output: ' ~ maxDiff(@ints); +} + +say "Example 1:"; +solution([5, 9, 3, 4, 6]); + +say "\nExample 2:"; +solution([1, -2, 3, -4]); + +say "\nExample 3:"; +solution([-3, -1, -2, -4]); + +say "\nExample 4:"; +solution([10, 2, 0, 5, 1]); + +say "\nExample 5:"; +solution([7, 8, 9, 10, 10]); diff --git a/challenge-339/packy-anderson/raku/ch-2.raku b/challenge-339/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..fd804250a4 --- /dev/null +++ b/challenge-339/packy-anderson/raku/ch-2.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku +use v6; + +sub peakPoint(@gain) { + my $max = 0; # we start at 0 + @gain.reduce({ my $s = $^a + $^b; $max = max($max, $s); $s }); + $max; +} + +sub solution(@gain) { + say 'Input: @gain = (' ~ @gain.join(', ') ~ ')'; + say 'Output: ' ~ peakPoint(@gain); +} + +say "Example 1:"; +solution([-5, 1, 5, -9, 2]); + +say "\nExample 2:"; +solution([10, 10, 10, -25]); + +say "\nExample 3:"; +solution([3, -4, 2, 5, -6, 1]); + +say "\nExample 4:"; +solution([-1, -2, -3, -4]); + +say "\nExample 5:"; +solution([-10, 15, 5]); -- cgit From f97ddcbc990cdd2e4084608d3f198786385f3ca5 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Tue, 16 Sep 2025 23:35:54 -0400 Subject: Remove duplicated line. --- challenge-339/packy-anderson/python/ch-2.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/challenge-339/packy-anderson/python/ch-2.py b/challenge-339/packy-anderson/python/ch-2.py index 0387ea2f4e..91ddbc0ff1 100755 --- a/challenge-339/packy-anderson/python/ch-2.py +++ b/challenge-339/packy-anderson/python/ch-2.py @@ -2,8 +2,6 @@ from functools import reduce -from functools import reduce - def add_max(x, y): global max_alt max_alt = max(max_alt, x+y) -- cgit