diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-09-17 09:10:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-17 09:10:52 +0100 |
| commit | d429f2de33ddfa98e751a6849fa08e7227712778 (patch) | |
| tree | b5aa5e6ee0a59e5d5c621806203b4d92706cd980 | |
| parent | 2bcc68460b259cf422d05dd3e4f2dfad8b2a71fa (diff) | |
| parent | f97ddcbc990cdd2e4084608d3f198786385f3ca5 (diff) | |
| download | perlweeklychallenge-club-d429f2de33ddfa98e751a6849fa08e7227712778.tar.gz perlweeklychallenge-club-d429f2de33ddfa98e751a6849fa08e7227712778.tar.bz2 perlweeklychallenge-club-d429f2de33ddfa98e751a6849fa08e7227712778.zip | |
Merge pull request #12692 from packy/master
Challenge 339 solutions by Packy Anderson
| -rw-r--r-- | challenge-339/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-339/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-339/packy-anderson/elixir/ch-1.exs | 30 | ||||
| -rwxr-xr-x | challenge-339/packy-anderson/elixir/ch-2.exs | 34 | ||||
| -rwxr-xr-x | challenge-339/packy-anderson/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-339/packy-anderson/perl/ch-2.pl | 29 | ||||
| -rwxr-xr-x | challenge-339/packy-anderson/python/ch-1.py | 29 | ||||
| -rwxr-xr-x | challenge-339/packy-anderson/python/ch-2.py | 36 | ||||
| -rwxr-xr-x | challenge-339/packy-anderson/raku/ch-1.raku | 28 | ||||
| -rwxr-xr-x | challenge-339/packy-anderson/raku/ch-2.raku | 28 |
10 files changed, 244 insertions, 1 deletions
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..91ddbc0ff1 --- /dev/null +++ b/challenge-339/packy-anderson/python/ch-2.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +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]); |
