aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2025-05-16 00:37:33 -0400
committerPacky Anderson <packy@cpan.org>2025-05-16 00:40:00 -0400
commitd2d3f658384be159cedb6d4288524a14cfd1d3ea (patch)
tree331f8b7068857935ce0608436ae27a554983665a
parent8ec526706ce12f3249b2392009f54125bbcdc11f (diff)
downloadperlweeklychallenge-club-d2d3f658384be159cedb6d4288524a14cfd1d3ea.tar.gz
perlweeklychallenge-club-d2d3f658384be159cedb6d4288524a14cfd1d3ea.tar.bz2
perlweeklychallenge-club-d2d3f658384be159cedb6d4288524a14cfd1d3ea.zip
Challenge 321 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-321/packy-anderson/README.md2
-rw-r--r--challenge-321/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-321/packy-anderson/elixir/ch-1.exs51
-rwxr-xr-xchallenge-321/packy-anderson/elixir/ch-2.exs30
-rwxr-xr-xchallenge-321/packy-anderson/perl/ch-1.pl37
-rwxr-xr-xchallenge-321/packy-anderson/perl/ch-2.pl23
-rwxr-xr-xchallenge-321/packy-anderson/python/ch-1.py46
-rwxr-xr-xchallenge-321/packy-anderson/python/ch-2.py28
-rwxr-xr-xchallenge-321/packy-anderson/raku/ch-1.raku37
-rwxr-xr-xchallenge-321/packy-anderson/raku/ch-2.raku26
10 files changed, 280 insertions, 1 deletions
diff --git a/challenge-321/packy-anderson/README.md b/challenge-321/packy-anderson/README.md
index 9ffd00fb24..0d605b134b 100644
--- a/challenge-321/packy-anderson/README.md
+++ b/challenge-321/packy-anderson/README.md
@@ -23,4 +23,4 @@
## Blog Post
-[Perl Weekly Challenge: Happy Mother's Day](https://packy.dardan.com/b/UZ)
+[Perl Weekly Challenge: Comparatively Distinct, but Great^H^H^H^H^HAverage](https://packy.dardan.com/b/Up)
diff --git a/challenge-321/packy-anderson/blog.txt b/challenge-321/packy-anderson/blog.txt
new file mode 100644
index 0000000000..0478708029
--- /dev/null
+++ b/challenge-321/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/Up \ No newline at end of file
diff --git a/challenge-321/packy-anderson/elixir/ch-1.exs b/challenge-321/packy-anderson/elixir/ch-1.exs
new file mode 100755
index 0000000000..590fcb09e5
--- /dev/null
+++ b/challenge-321/packy-anderson/elixir/ch-1.exs
@@ -0,0 +1,51 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def distinctAverages([], step, explain, averages),
+ do: {explain, averages}
+
+ def distinctAverages(nums, step, explain, averages) do
+ {min, nums} = List.pop_at(nums, 0)
+ {max, nums} = List.pop_at(nums, -1)
+ avg = (min + max) / 2
+ # since in Elixir, this will always produce a float,
+ # we need to do a little more work to make sure avg is an
+ # integer when it has no fractional component
+ avg = cond do
+ avg == trunc(avg) -> trunc(avg)
+ true -> avg
+ end
+ explain = explain <> "Step #{step}: Min = #{min}, "
+ explain = explain <> "Max = #{max}, Avg = #{avg}\n"
+ averages = averages |> Map.put(avg, 1)
+ distinctAverages(nums, step+1, explain, averages)
+ end
+
+ def distinctAverages(nums) do
+ {explain, averages} =
+ distinctAverages(Enum.sort(nums), 1, "", %{})
+ count = averages |> Map.keys |> length
+ {
+ count,
+ explain <> "\nThe count of distinct average is #{count}."
+ }
+ end
+
+ def solution(nums) do
+ IO.puts("Input: @nums = (" <> Enum.join(nums, ", ") <> ")")
+ {count, explain} = distinctAverages(nums)
+ IO.puts("Output: #{count}\n\n#{explain}")
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([1, 2, 4, 3, 5, 6])
+
+IO.puts("\nExample 2:")
+PWC.solution([0, 2, 4, 8, 3, 5])
+
+IO.puts("\nExample 3:")
+PWC.solution([7, 3, 1, 0, 5, 9])
+
+IO.puts("\nExample 4:")
+PWC.solution([1, 9, 2, 6, 3, 4])
diff --git a/challenge-321/packy-anderson/elixir/ch-2.exs b/challenge-321/packy-anderson/elixir/ch-2.exs
new file mode 100755
index 0000000000..7da3ddeb8e
--- /dev/null
+++ b/challenge-321/packy-anderson/elixir/ch-2.exs
@@ -0,0 +1,30 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ defp repeatReplace(str) do
+ s = Regex.replace(~r/.\#/, str, "")
+ cond do
+ s != str -> repeatReplace(s)
+ true -> s
+ end
+ end
+
+ def backspaceCompare(str1, str2) do
+ repeatReplace(str1) == repeatReplace(str2)
+ end
+
+ def solution(str1, str2) do
+ IO.puts("Input: $str1 = \"#{str1}\"")
+ IO.puts(" $str2 = \"#{str2}\"")
+ IO.puts("Output: #{ backspaceCompare(str1, str2) }" )
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution("ab#c", "ad#c")
+
+IO.puts("\nExample 2:")
+PWC.solution("ab##", "a#b#")
+
+IO.puts("\nExample 3:")
+PWC.solution("a#b", "c")
diff --git a/challenge-321/packy-anderson/perl/ch-1.pl b/challenge-321/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..3cfd61003f
--- /dev/null
+++ b/challenge-321/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub distinctAverages(@nums) {
+ @nums = sort @nums;
+ my ($explain, $step) = (q{}, 0);
+ my %averages =();
+ while (@nums) {
+ my ($min, $max) = (shift(@nums), pop(@nums));
+ my $avg = ($min + $max) / 2;
+ $step++;
+ $explain .= "Step $step: Min = $min, "
+ . "Max = $max, Avg = $avg\n";
+ $averages{$avg}++;
+ }
+ my $count = scalar(keys %averages);
+ $explain .= "\nThe count of distinct average is $count.";
+ return $count, $explain;
+}
+
+sub solution($nums) {
+ say 'Input: @nums = (' . join(', ', @$nums) . ')';
+ my ($count, $explain) = distinctAverages(@$nums);
+ say "Output: $count\n\n$explain";
+}
+
+say "Example 1:";
+solution([1, 2, 4, 3, 5, 6]);
+
+say "\nExample 2:";
+solution([0, 2, 4, 8, 3, 5]);
+
+say "\nExample 3:";
+solution([7, 3, 1, 0, 5, 9]);
+
+say "\nExample 4:";
+solution([1, 9, 2, 6, 3, 4]);
diff --git a/challenge-321/packy-anderson/perl/ch-2.pl b/challenge-321/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..6c50898042
--- /dev/null
+++ b/challenge-321/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub backspaceCompare($str1, $str2) {
+ while ($str1 =~ s/.\#//) {}
+ while ($str2 =~ s/.\#//) {}
+ return $str1 eq $str2 ? 'true' : 'false';
+}
+
+sub solution($str1, $str2) {
+ say qq{Input: \$str1 = "$str1"};
+ say qq{ \$str2 = "$str2"};
+ say 'Output: ' . backspaceCompare($str1, $str2);
+}
+
+say "Example 1:";
+solution("ab#c", "ad#c");
+
+say "\nExample 2:";
+solution("ab##", "a#b#");
+
+say "\nExample 3:";
+solution("a#b", "c");
diff --git a/challenge-321/packy-anderson/python/ch-1.py b/challenge-321/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..a39a0b68ec
--- /dev/null
+++ b/challenge-321/packy-anderson/python/ch-1.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+
+from collections import Counter
+
+def distinctAverages(nums):
+ nums.sort() # list.sort() works in place
+ explain, step = "", 0
+ averages = Counter()
+ while nums:
+ min, max = nums.pop(0), nums.pop(-1)
+ avg = (min + max) / 2
+ # since in Python, this will always produce a real number,
+ # we need to do a little more work to make sure avg is an
+ # integer when it has no fractional component
+ if avg == int(avg):
+ avg = int(avg)
+ step += 1
+ explain += (
+ f"Step {step}: Min = {min}, Max = {max}, Avg = {avg}\n"
+ )
+ averages[avg] += 1
+ count = len(list(averages))
+ explain += (
+ f"\nThe count of distinct average is {count}."
+ )
+ return count, explain
+
+def int_join(joiner, arr):
+ return joiner.join(map(lambda i: str(i), arr))
+
+def solution(nums):
+ print(f'Input: @nums = ({int_join(", ", nums)})')
+ count, explain = distinctAverages(nums)
+ print(f'Output: {count}\n\n{explain}')
+
+print('Example 1:')
+solution([1, 2, 4, 3, 5, 6])
+
+print('\nExample 2:')
+solution([0, 2, 4, 8, 3, 5])
+
+print('\nExample 3:')
+solution([7, 3, 1, 0, 5, 9])
+
+print('\nExample 4:')
+solution([1, 9, 2, 6, 3, 4]) \ No newline at end of file
diff --git a/challenge-321/packy-anderson/python/ch-2.py b/challenge-321/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..e1f1bc7c82
--- /dev/null
+++ b/challenge-321/packy-anderson/python/ch-2.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+
+import re
+
+def backspaceCompare(str1, str2):
+ s1 = re.sub(r'.\#', "", str1)
+ while s1 != str1:
+ str1 = s1
+ s1 = re.sub(r'.\#', "", str1)
+ s2 = re.sub(r'.\#', "", str2)
+ while s2 != str2:
+ str2 = s2
+ s2 = re.sub(r'.\#', "", str2)
+ return s1 == s2
+
+def solution(str1, str2):
+ print(f'Input: $str1 = "{str1}"')
+ print(f' $str2 = "{str2}"')
+ print(f'Output: { backspaceCompare(str1, str2) }')
+
+print('Example 1:')
+solution("ab#c", "ad#c")
+
+print('\nExample 2:')
+solution("ab##", "a#b#")
+
+print('\nExample 3:')
+solution("a#b", "c")
diff --git a/challenge-321/packy-anderson/raku/ch-1.raku b/challenge-321/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..5abc7a0d4a
--- /dev/null
+++ b/challenge-321/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,37 @@
+#!/usr/bin/env raku
+use v6;
+
+sub distinctAverages(@nums) {
+ @nums = @nums.sort;
+ my ($explain, $step) = (q{}, 0);
+ my %averages = Bag.new;
+ while (@nums) {
+ my ($min, $max) = (@nums.shift, @nums.pop);
+ my $avg = ($min + $max) / 2;
+ $step++;
+ $explain ~= "Step $step: Min = $min, "
+ ~ "Max = $max, Avg = $avg\n";
+ %averages{$avg}++;
+ }
+ $explain ~= "\nThe count of distinct average is "
+ ~ %averages.elems ~ ".";
+ return %averages.elems, $explain;
+}
+
+sub solution(@nums) {
+ say 'Input: @nums = (' ~ @nums.join(', ') ~ ')';
+ my ($count, $explain) = distinctAverages(@nums);
+ say "Output: $count\n\n$explain";
+}
+
+say "Example 1:";
+solution([1, 2, 4, 3, 5, 6]);
+
+say "\nExample 2:";
+solution([0, 2, 4, 8, 3, 5]);
+
+say "\nExample 3:";
+solution([7, 3, 1, 0, 5, 9]);
+
+say "\nExample 4:";
+solution([1, 9, 2, 6, 3, 4]); \ No newline at end of file
diff --git a/challenge-321/packy-anderson/raku/ch-2.raku b/challenge-321/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..5aea8ca321
--- /dev/null
+++ b/challenge-321/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,26 @@
+#!/usr/bin/env raku
+use v6;
+
+sub backspaceCompare($str1, $str2) {
+ # if we pass in these as 'is rw', it expects to be passed
+ # a writable container, which a string literal is not.
+ # it's just easier to copy it to a writable container.
+ my $s1 = $str1; while ($s1 ~~ s/.\#//) {}
+ my $s2 = $str2; while ($s2 ~~ s/.\#//) {}
+ return $s1 eq $s2;
+}
+
+sub solution($str1, $str2) {
+ say qq{Input: \$str1 = "$str1"};
+ say qq{ \$str2 = "$str2"};
+ say 'Output: ' ~ backspaceCompare($str1, $str2);
+}
+
+say "Example 1:";
+solution("ab#c", "ad#c");
+
+say "\nExample 2:";
+solution("ab##", "a#b#");
+
+say "\nExample 3:";
+solution("a#b", "c");