aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2025-06-27 00:48:06 -0400
committerPacky Anderson <packy@cpan.org>2025-06-27 00:48:06 -0400
commita4eb4accdb68034147efafe8c5c0f09450821afe (patch)
treeff49cb94a5b3b764b5a653685a014b6363a65468
parent4b33a6c5bea8a2c5df93b32bac9078b4d23ff42f (diff)
downloadperlweeklychallenge-club-a4eb4accdb68034147efafe8c5c0f09450821afe.tar.gz
perlweeklychallenge-club-a4eb4accdb68034147efafe8c5c0f09450821afe.tar.bz2
perlweeklychallenge-club-a4eb4accdb68034147efafe8c5c0f09450821afe.zip
Challenge 327 solutions by Packy Anderson
* Raku that maybe looks like Raku, but mostly like Perl * Perl * Python that definitely looks like Perl * Elixir that looks kinda like Elixir 1 blog post
-rw-r--r--challenge-327/packy-anderson/README.md2
-rw-r--r--challenge-327/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-327/packy-anderson/elixir/ch-1.exs38
-rwxr-xr-xchallenge-327/packy-anderson/elixir/ch-2.exs52
-rwxr-xr-xchallenge-327/packy-anderson/perl/ch-1.pl26
-rwxr-xr-xchallenge-327/packy-anderson/perl/ch-2.pl42
-rwxr-xr-xchallenge-327/packy-anderson/python/ch-1.py27
-rwxr-xr-xchallenge-327/packy-anderson/python/ch-2.py40
-rwxr-xr-xchallenge-327/packy-anderson/raku/ch-1.raku26
-rwxr-xr-xchallenge-327/packy-anderson/raku/ch-2.raku40
10 files changed, 293 insertions, 1 deletions
diff --git a/challenge-327/packy-anderson/README.md b/challenge-327/packy-anderson/README.md
index f16c1ab69f..e2ebc8142c 100644
--- a/challenge-327/packy-anderson/README.md
+++ b/challenge-327/packy-anderson/README.md
@@ -23,4 +23,4 @@
## Blog Post
-[Perl Weekly Challenge: Got a date with compression!](https://packy.dardan.com/b/Wn)
+[Perl Weekly Challenge: Missing Absolute Distance](https://packy.dardan.com/b/Wy)
diff --git a/challenge-327/packy-anderson/blog.txt b/challenge-327/packy-anderson/blog.txt
new file mode 100644
index 0000000000..a6c48ba3f0
--- /dev/null
+++ b/challenge-327/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/Wy \ No newline at end of file
diff --git a/challenge-327/packy-anderson/elixir/ch-1.exs b/challenge-327/packy-anderson/elixir/ch-1.exs
new file mode 100755
index 0000000000..edb5d71285
--- /dev/null
+++ b/challenge-327/packy-anderson/elixir/ch-1.exs
@@ -0,0 +1,38 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ defp map_reduce_func(i, {set, missing}) do
+ {i,
+ if not MapSet.member?(set, i) do
+ {set, missing ++ [i]}
+ else
+ {set, missing}
+ end
+ }
+ end
+
+ def missingInts(ints) do
+ {_, {_, missing}} =
+ 1..length(ints)
+ |> Enum.map_reduce(
+ { MapSet.new(ints), [] },
+ &map_reduce_func/2
+ )
+ missing
+ end
+
+ def solution(ints) do
+ IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")")
+ missing = missingInts(ints)
+ IO.puts("Output: (" <> Enum.join(missing, ", ") <> ")")
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([1, 2, 1, 3, 2, 5])
+
+IO.puts("\nExample 2:")
+PWC.solution([1, 1, 1])
+
+IO.puts("\nExample 3:")
+PWC.solution([2, 2, 1])
diff --git a/challenge-327/packy-anderson/elixir/ch-2.exs b/challenge-327/packy-anderson/elixir/ch-2.exs
new file mode 100755
index 0000000000..d9fd74bedc
--- /dev/null
+++ b/challenge-327/packy-anderson/elixir/ch-2.exs
@@ -0,0 +1,52 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ defp map_reduce_func(y, {x, minDiff, madList}) do
+ diff = abs(x - y)
+ {minDiff, madList} = cond do
+ diff > minDiff ->
+ {minDiff, madList} # no change
+ diff == minDiff ->
+ # add [x,y] to the current list
+ {minDiff, madList ++ [ Enum.sort([x, y]) ]}
+ diff < minDiff ->
+ # we found a new min!
+ # make [x,y] the start of new list
+ {diff, [ Enum.sort([x, y]) ]}
+ end
+ {y, {x, minDiff, madList}}
+ end
+
+ def mad([], _, madList), do: madList
+
+ def mad([x | ints], minDiff, madList) do
+ {_, {_, minDiff, madList}} = Enum.map_reduce(
+ ints, {x, minDiff, madList}, &map_reduce_func/2
+ )
+ mad(ints, minDiff, madList)
+ end
+
+ def mad(ints) do
+ # return the list with the pairs sorted as well
+ mad(ints, Enum.max(ints), [])
+ |> Enum.sort(&(List.first(&1) <= List.first(&2)))
+ end
+
+ def solution(ints) do
+ IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")")
+ mad = mad(ints)
+ |> Enum.map(
+ fn x -> "[#{List.first(x)},#{List.last(x)}]" end
+ )
+ IO.puts("Output: " <> Enum.join(mad, ", "))
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([4, 1, 2, 3])
+
+IO.puts("\nExample 2:")
+PWC.solution([1, 3, 7, 11, 15])
+
+IO.puts("\nExample 3:")
+PWC.solution([1, 5, 3, 8])
diff --git a/challenge-327/packy-anderson/perl/ch-1.pl b/challenge-327/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..de2fdb92ff
--- /dev/null
+++ b/challenge-327/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub missingInts(@ints) {
+ my %set = map { $_ => 1} @ints;
+ my @missing;
+ foreach my $i ( 1 .. scalar(@ints) ) {
+ push @missing, $i unless exists $set{$i};
+ }
+ return @missing;
+}
+
+sub solution($ints) {
+ say 'Input: @ints = (' . join(', ', @$ints) . ')';
+ my @missing = missingInts(@$ints);
+ say 'Output: (' . join(', ', @missing) . ')';
+}
+
+say "Example 1:";
+solution([1, 2, 1, 3, 2, 5]);
+
+say "\nExample 2:";
+solution([1, 1, 1]);
+
+say "\nExample 3:";
+solution([2, 2, 1]);
diff --git a/challenge-327/packy-anderson/perl/ch-2.pl b/challenge-327/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..8bc0e48043
--- /dev/null
+++ b/challenge-327/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+use v5.40;
+
+use List::AllUtils qw( max );
+
+sub MAD(@ints) {
+ my @mad;
+ # let's make the biggest int our starting minimum
+ my $min = max @ints;
+ while (my $x = shift @ints) {
+ foreach my $y ( @ints ) {
+ my $diff = abs($x - $y);
+ next if $diff > $min; # too big!
+ if ($diff < $min) { # we found a new min!
+ $min = $diff; # set the new minimum
+ @mad = (); # clear the old list
+ }
+ # make sure we put the numbers on the list
+ # with the smaller integer first
+ push @mad, [sort $x, $y];
+ }
+ }
+ # and return the list with the pairs sorted as well
+ return sort { $a->[0] <=> $b->[0] } @mad;
+}
+
+sub solution($ints) {
+ say 'Input: @ints = (' . join(', ', @$ints) . ')';
+ my @mad = MAD(@$ints);
+ # make them printable
+ @mad = map { qq{[$_->[0],$_->[1]]} } @mad;
+ say 'Output: ' . join(', ', @mad);
+}
+
+say "Example 1:";
+solution([4, 1, 2, 3]);
+
+say "\nExample 2:";
+solution([1, 3, 7, 11, 15]);
+
+say "\nExample 3:";
+solution([1, 5, 3, 8]);
diff --git a/challenge-327/packy-anderson/python/ch-1.py b/challenge-327/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..7cbb8a7025
--- /dev/null
+++ b/challenge-327/packy-anderson/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+
+def missingInts(ints):
+ intSet = set(ints)
+ missing = []
+ for i in range(1, len(ints)+1):
+ if i not in intSet:
+ missing.append(i)
+ return missing
+
+def int_join(joiner, arr):
+ return joiner.join(map(lambda i: str(i), arr))
+
+def solution(ints):
+ print(f'Input: @ints = ({int_join(", ", ints)})')
+ missing = missingInts(ints)
+ print(f'Output: ({int_join(", ", missing)})')
+
+
+print('Example 1:')
+solution([1, 2, 1, 3, 2, 5])
+
+print('\nExample 2:')
+solution([1, 1, 1])
+
+print('\nExample 3:')
+solution([2, 2, 1])
diff --git a/challenge-327/packy-anderson/python/ch-2.py b/challenge-327/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..22216a9eb2
--- /dev/null
+++ b/challenge-327/packy-anderson/python/ch-2.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+
+def MAD(ints):
+ mad = []
+ # let's make the biggest int our starting minimum
+ minDiff = max(ints)
+ x = ints.pop(0) # get the first value for x
+ while ints:
+ for y in ints:
+ diff = abs(x - y)
+ if diff <= minDiff: # not too big!
+ if diff < minDiff: # we found a new min!
+ minDiff = diff # set the new minimum
+ mad = [] # clear the old list
+ # make sure we put the numbers on the list
+ # with the smaller integer first
+ mad.append(sorted([x, y]))
+ x = ints.pop(0) # get the next value for x
+ # and return the list with the pairs sorted as well
+ return sorted(mad, key=lambda x: x[0])
+
+def int_join(joiner, arr):
+ return joiner.join(map(lambda i: str(i), arr))
+
+def solution(ints):
+ print(f'Input: @ints = ({int_join(", ", ints)})')
+ mad = []
+ for x in MAD(ints):
+ mad.append(f'{x}')
+ print(f'Output: {", ".join(mad)}')
+
+
+print('Example 1:')
+solution([4, 1, 2, 3])
+
+print('\nExample 2:')
+solution([1, 3, 7, 11, 15])
+
+print('\nExample 3:')
+solution([1, 5, 3, 8])
diff --git a/challenge-327/packy-anderson/raku/ch-1.raku b/challenge-327/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..6e7debffe8
--- /dev/null
+++ b/challenge-327/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,26 @@
+#!/usr/bin/env raku
+use v6;
+
+sub missingInts(@ints) {
+ my $set = @ints.Set;
+ my @missing;
+ for 1 .. @ints.elems -> $i {
+ @missing.push($i) if $i ∉ $set;
+ }
+ return @missing;
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ ')';
+ my @missing = missingInts(@ints);
+ say 'Output: (' ~ @missing.join(', ') ~ ')';
+}
+
+say "Example 1:";
+solution([1, 2, 1, 3, 2, 5]);
+
+say "\nExample 2:";
+solution([1, 1, 1]);
+
+say "\nExample 3:";
+solution([2, 2, 1]);
diff --git a/challenge-327/packy-anderson/raku/ch-2.raku b/challenge-327/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..60ef631d42
--- /dev/null
+++ b/challenge-327/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,40 @@
+#!/usr/bin/env raku
+use v6;
+
+sub MAD(@ints) {
+ my @mad;
+ # let's make the biggest int our starting minimum
+ my $min = max @ints;
+ while (my $x = @ints.shift) {
+ for @ints -> $y {
+ my $diff = abs($x - $y);
+ next if $diff > $min; # too big!
+ if ($diff < $min) { # we found a new min!
+ $min = $diff; # set the new minimum
+ @mad = (); # clear the old list
+ }
+ # make sure we put the numbers on the list
+ # with the smaller integer first
+ @mad.push([sort $x, $y]);
+ }
+ }
+ # and return the list with the pairs sorted as well
+ return @mad.sort({ $^a[0] cmp $^b[0] });
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ ')';
+ my @mad = MAD(@ints);
+ # make them printable
+ @mad = @mad.map({ qq{[$_[0],$_[1]]} });
+ say 'Output: ' ~ @mad.join(', ');
+}
+
+say "Example 1:";
+solution([4, 1, 2, 3]);
+
+say "\nExample 2:";
+solution([1, 3, 7, 11, 15]);
+
+say "\nExample 3:";
+solution([1, 5, 3, 8]);