aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2025-06-05 23:57:21 -0400
committerPacky Anderson <packy@cpan.org>2025-06-05 23:57:21 -0400
commitd57f75b6aacb0e0d480ff8c543beedaca13e7bf5 (patch)
tree3f5ff7b0ba270175cc245a77656ed95055885553
parentcd87a854dd039199fdc61c1a021a7a2b3e6b180e (diff)
downloadperlweeklychallenge-club-d57f75b6aacb0e0d480ff8c543beedaca13e7bf5.tar.gz
perlweeklychallenge-club-d57f75b6aacb0e0d480ff8c543beedaca13e7bf5.tar.bz2
perlweeklychallenge-club-d57f75b6aacb0e0d480ff8c543beedaca13e7bf5.zip
Challenge 324 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-324/packy-anderson/README.md2
-rw-r--r--challenge-324/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-324/packy-anderson/elixir/ch-1.exs56
-rwxr-xr-xchallenge-324/packy-anderson/elixir/ch-2.exs37
-rwxr-xr-xchallenge-324/packy-anderson/perl/ch-1.pl59
-rwxr-xr-xchallenge-324/packy-anderson/perl/ch-2.pl27
-rwxr-xr-xchallenge-324/packy-anderson/python/ch-1.py57
-rwxr-xr-xchallenge-324/packy-anderson/python/ch-2.py28
-rwxr-xr-xchallenge-324/packy-anderson/raku/ch-1.raku59
-rwxr-xr-xchallenge-324/packy-anderson/raku/ch-2.raku28
10 files changed, 353 insertions, 1 deletions
diff --git a/challenge-324/packy-anderson/README.md b/challenge-324/packy-anderson/README.md
index 528307c718..e0b74a44c1 100644
--- a/challenge-324/packy-anderson/README.md
+++ b/challenge-324/packy-anderson/README.md
@@ -23,4 +23,4 @@
## Blog Post
-[Perl Weekly Challenge: Should five percent appear too small...](https://packy.dardan.com/b/Ve)
+[Perl Weekly Challenge: Totally 2bular!](https://packy.dardan.com/b/W3)
diff --git a/challenge-324/packy-anderson/blog.txt b/challenge-324/packy-anderson/blog.txt
new file mode 100644
index 0000000000..eed2b1c1d3
--- /dev/null
+++ b/challenge-324/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/W3 \ No newline at end of file
diff --git a/challenge-324/packy-anderson/elixir/ch-1.exs b/challenge-324/packy-anderson/elixir/ch-1.exs
new file mode 100755
index 0000000000..31e72e63c5
--- /dev/null
+++ b/challenge-324/packy-anderson/elixir/ch-1.exs
@@ -0,0 +1,56 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def twoDarray(ints, r, c) when length(ints) != r * c do
+ {
+ "Unable to create a two-dimensional array with " <>
+ "#{r} rows and #{c} columns\nfrom a list of " <>
+ "#{length(ints)} integers; there must be #{r * c} " <>
+ "integers.",
+ []
+ }
+ end
+
+ def twoDarray(ints, _, c) do
+ { nil, Enum.chunk_every(ints, c) }
+ end
+
+ # default value for indent
+ def formatMatrix(matrix), do: formatMatrix(matrix, 8)
+
+ def formatMatrix(matrix, indent) do
+ output = for row <- matrix do
+ String.duplicate(" ", indent) <> " [" <>
+ Enum.join(row, ", ") <> "]"
+ end
+ "[\n" <> Enum.join(output, ",\n") <> "\n" <>
+ String.duplicate(" ", indent) <> "]"
+ end
+
+ def solution(ints, r, c) do
+ int_list = "(" <> Enum.join(ints, ", ") <> ")"
+ IO.puts("Input: @ints = #{int_list}, $r = #{r}, $c = #{c}")
+ {err, arr} = twoDarray(ints, r, c)
+ if err do
+ IO.puts("\n#{err}")
+ else
+ IO.inspect(arr)
+ IO.puts("Output: " <> formatMatrix(arr) )
+ end
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([1, 2, 3, 4], 2, 2)
+
+IO.puts("\nExample 2:")
+PWC.solution([1, 2, 3], 1, 3)
+
+IO.puts("\nExample 3:")
+PWC.solution([1, 2, 3, 4], 4, 1)
+
+IO.puts("\nExample 4:")
+PWC.solution([1, 2, 3, 4], 3, 1)
+
+IO.puts("\nExample 5:")
+PWC.solution([1, 2, 3, 4], 3, 2)
diff --git a/challenge-324/packy-anderson/elixir/ch-2.exs b/challenge-324/packy-anderson/elixir/ch-2.exs
new file mode 100755
index 0000000000..77510b96fc
--- /dev/null
+++ b/challenge-324/packy-anderson/elixir/ch-2.exs
@@ -0,0 +1,37 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def powerSet([]), do: []
+
+ def powerSet([i | rest]) do
+ list = powerSet(rest)
+ [[i] | Enum.reduce(list, list, &[[i | &1] | &2])]
+ end
+
+ def totalXOR([], sum), do: sum
+
+ def totalXOR([set | rest], sum) do
+ totalXOR(
+ rest,
+ sum + Enum.reduce(set, 0, &( Bitwise.bxor(&1, &2) ))
+ )
+ end
+
+ def totalXOR(ints) do
+ powerSet(ints) |> totalXOR(0)
+ end
+
+ def solution(ints) do
+ IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")")
+ IO.puts("Output: #{totalXOR(ints)}")
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([1, 3])
+
+IO.puts("\nExample 2:")
+PWC.solution([5, 1, 6])
+
+IO.puts("\nExample 3:")
+PWC.solution([3, 4, 5, 6, 7, 8])
diff --git a/challenge-324/packy-anderson/perl/ch-1.pl b/challenge-324/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..4365249b2a
--- /dev/null
+++ b/challenge-324/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub twoDarray($ints, $r, $c) {
+ if (scalar(@$ints) != $r * $c) {
+ return "Unable to create a two-dimensional array with "
+ . "$r rows and $c columns\nfrom a list of "
+ . "@{[scalar @$ints]} integers; there must be "
+ . "@{[$r * $c]} integers.";
+ }
+ my @arr;
+ while (@$ints) {
+ my @row;
+ push @row, shift @$ints for 1 .. $c ;
+ push @arr, \@row;
+ }
+ return undef, \@arr;
+}
+
+sub formatMatrix($matrix, $indent=8) {
+ my @output;
+ foreach my $row (@$matrix) {
+ my $output_row = q{ } x $indent . " [";
+ $output_row .= join(', ', map { sprintf "%1d", $_ } @$row) . ']';
+ push @output, $output_row;
+ }
+ return "[\n"
+ . join(",\n", @output)
+ . "\n"
+ . q{ } x $indent . "]";
+}
+
+sub solution($ints, $r, $c) {
+ my $int_list = '(' . join(', ', @$ints) . ')';
+ say "Input: \@ints = $int_list, \$r = $r, \$c = $c";
+ my ($err, $arr) = twoDarray($ints, $r, $c);
+ if ($err) {
+ say "";
+ say $err;
+ }
+ else {
+ say "Output: @{[ formatMatrix($arr) ]}";
+ }
+}
+
+say "Example 1:";
+solution([1, 2, 3, 4], 2, 2);
+
+say "\nExample 2:";
+solution([1, 2, 3], 1, 3);
+
+say "\nExample 3:";
+solution([1, 2, 3, 4], 4, 1);
+
+say "\nExample 4:";
+solution([1, 2, 3, 4], 3, 1);
+
+say "\nExample 4:";
+solution([1, 2, 3, 4], 3, 2);
diff --git a/challenge-324/packy-anderson/perl/ch-2.pl b/challenge-324/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..574805622a
--- /dev/null
+++ b/challenge-324/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+use v5.40;
+
+use Algorithm::Combinatorics qw( subsets );
+use List::AllUtils qw( reduce );
+
+sub totalXOR($ints) {
+ my $sum = 0;
+ for my $s ( subsets($ints) ) {
+ $sum += (reduce { $a ^ $b } @$s) // 0;
+ }
+ return $sum;
+}
+
+sub solution($ints) {
+ say 'Input: @ints = (' . join(', ', @$ints) . ')';
+ say 'Output: ' . totalXOR($ints);
+}
+
+say "Example 1:";
+solution([1, 3]);
+
+say "\nExample 2:";
+solution([5, 1, 6]);
+
+say "\nExample 3:";
+solution([3, 4, 5, 6, 7, 8]);
diff --git a/challenge-324/packy-anderson/python/ch-1.py b/challenge-324/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..904bad27a0
--- /dev/null
+++ b/challenge-324/packy-anderson/python/ch-1.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+
+def twoDarray(ints, r, c):
+ if (len(ints) != r * c):
+ return (
+ "Unable to create a two-dimensional array with "
+ + f"{r} rows and {c} columns\nfrom a list of {len(ints)} "
+ + f"integers; there must be {r * c} integers.",
+ []
+ )
+ arr = []
+ while (ints):
+ row = []
+ for i in range(c):
+ row.append( ints.pop(0) )
+ arr.append(row)
+ return None, arr
+
+def formatMatrix(matrix, indent=8):
+ output = []
+ for row in matrix:
+ output_row = ' ' * indent + ' ['
+ output_row += ', '.join(map(lambda i: str(i), row))
+ output_row += ']'
+ output.append(output_row)
+
+ return(
+ "[\n" + ",\n".join(output) + "\n" +
+ ' ' * indent + ']'
+ )
+
+def int_join(joiner, arr):
+ return joiner.join(map(lambda i: str(i), arr))
+
+def solution(ints, r, c):
+ int_list = '(' + int_join(", ", ints) + ')'
+ print(f'Input: @ints = {int_list}, $r = {r}, $c = {c}')
+ err, arr = twoDarray(ints, r, c)
+ if err:
+ print(f'\n{err}')
+ else:
+ print(f'Output: {formatMatrix(arr)}')
+
+print('Example 1:')
+solution([1, 2, 3, 4], 2, 2)
+
+print('\nExample 2:')
+solution([1, 2, 3], 1, 3)
+
+print('\nExample 3:')
+solution([1, 2, 3, 4], 4, 1)
+
+print('\nExample 4:')
+solution([1, 2, 3, 4], 3, 1)
+
+print('\nExample 5:')
+solution([1, 2, 3, 4], 3, 2)
diff --git a/challenge-324/packy-anderson/python/ch-2.py b/challenge-324/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..3ef70423f5
--- /dev/null
+++ b/challenge-324/packy-anderson/python/ch-2.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+
+from functools import reduce
+
+def powerSet(ints):
+ return reduce(lambda r, i: r + [s + [i] for s in r], ints, [[]])
+
+def totalXOR(ints):
+ sum = 0
+ for s in powerSet(ints):
+ sum += reduce(lambda a, b: a ^ b, s, 0)
+ return sum
+
+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: {totalXOR(ints)}')
+
+print('Example 1:')
+solution([1, 3])
+
+print('\nExample 2:')
+solution([5, 1, 6])
+
+print('\nExample 3:')
+solution([3, 4, 5, 6, 7, 8])
diff --git a/challenge-324/packy-anderson/raku/ch-1.raku b/challenge-324/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..876213967c
--- /dev/null
+++ b/challenge-324/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,59 @@
+#!/usr/bin/env raku
+use v6;
+
+sub twoDarray(@ints, $r, $c) {
+ if (@ints.elems != $r * $c) {
+ return ["Unable to create a two-dimensional array with "
+ ~ "$r rows and $c columns\nfrom a list of {@ints.elems} "
+ ~ "integers; there must be {$r * $c} integers."];
+ }
+ my @arr;
+ while (@ints) {
+ my @row;
+ @row.push( @ints.shift ) for 1 .. $c ;
+ @arr.push(@row);
+ }
+ return @arr;
+}
+
+sub formatMatrix(@matrix, $indent=8) {
+ my @output;
+ for @matrix -> @row {
+ my $output_row = q{ } x $indent ~ " [";
+ $output_row ~= @row.map({ sprintf "%1d", $_ })
+ .join(', ') ~ "]";
+ @output.push($output_row);
+ }
+ return "[\n"
+ ~ @output.join(",\n")
+ ~ "\n"
+ ~ q{ } x $indent ~ "]";
+}
+
+sub solution(@ints, $r, $c) {
+ my $ints = '(' ~ @ints.join(', ') ~ ')';
+ say "Input: \@ints = $ints, \$r = $r, \$c = $c";
+ my @arr = twoDarray(@ints, $r, $c);
+ if (@arr[0].isa("Str") ) {
+ say "";
+ say @arr[0];
+ }
+ else {
+ say "Output: {formatMatrix(@arr)}";
+ }
+}
+
+say "Example 1:";
+solution([1, 2, 3, 4], 2, 2);
+
+say "\nExample 2:";
+solution([1, 2, 3], 1, 3);
+
+say "\nExample 3:";
+solution([1, 2, 3, 4], 4, 1);
+
+say "\nExample 4:";
+solution([1, 2, 3, 4], 3, 1);
+
+say "\nExample 4:";
+solution([1, 2, 3, 4], 3, 2);
diff --git a/challenge-324/packy-anderson/raku/ch-2.raku b/challenge-324/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..c608ed7fc6
--- /dev/null
+++ b/challenge-324/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,28 @@
+#!/usr/bin/env raku
+use v6;
+
+sub powerSet(@array) {
+ return @array.combinations: 0 .. @array.elems;
+}
+
+sub totalXOR(@ints) {
+ my $sum = 0;
+ for powerSet(@ints) -> @s {
+ $sum += [+^] @s;
+ }
+ return $sum;
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ ')';
+ say 'Output: ' ~ totalXOR(@ints);
+}
+
+say "Example 1:";
+solution([1, 3]);
+
+say "\nExample 2:";
+solution([5, 1, 6]);
+
+say "\nExample 3:";
+solution([3, 4, 5, 6, 7, 8]);