aboutsummaryrefslogtreecommitdiff
path: root/challenge-324
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-06-03 13:08:02 +0100
committerGitHub <noreply@github.com>2025-06-03 13:08:02 +0100
commit1d38e9576825151e0d8bc88e9c3bed9efc5ab5e1 (patch)
tree2d436883b1785f399fba95e2ed0b518ad27d4192 /challenge-324
parent672679bcbab9afb7de605db23fe762e9cb6e99bc (diff)
parent3594510a5b9f1dadf3983f0b430fe8b9398f0ea9 (diff)
downloadperlweeklychallenge-club-1d38e9576825151e0d8bc88e9c3bed9efc5ab5e1.tar.gz
perlweeklychallenge-club-1d38e9576825151e0d8bc88e9c3bed9efc5ab5e1.tar.bz2
perlweeklychallenge-club-1d38e9576825151e0d8bc88e9c3bed9efc5ab5e1.zip
Merge pull request #12121 from choroba/ech324
Add solutions to 324: 2D Array & Total XOR by E. Choroba
Diffstat (limited to 'challenge-324')
-rwxr-xr-xchallenge-324/e-choroba/perl/ch-1.pl20
-rwxr-xr-xchallenge-324/e-choroba/perl/ch-2.pl25
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-324/e-choroba/perl/ch-1.pl b/challenge-324/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..38848c621d
--- /dev/null
+++ b/challenge-324/e-choroba/perl/ch-1.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+use PDL;
+
+sub array_2d($r, $c, @ints) {
+ pdl(@ints)->reshape($c, $r)->unpdl
+}
+
+use Test2::V0 qw{ is plan };
+plan(3 + 2);
+
+is array_2d(2, 2, 1, 2, 3, 4), [[1, 2], [3, 4]], 'Example 1';
+is array_2d(1, 3, 1, 2, 3), [[1, 2, 3]], 'Example 2';
+is array_2d(4, 1, 1, 2, 3, 4), [[1], [2], [3], [4]], 'Example 3';
+
+is array_2d(2, 3, 1, 2), [[1, 2, 0], [0, 0, 0]], 'Not enough data';
+is array_2d(1, 2, 1, 2, 3), [[1, 2]], 'Too much data';
diff --git a/challenge-324/e-choroba/perl/ch-2.pl b/challenge-324/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..f21906153c
--- /dev/null
+++ b/challenge-324/e-choroba/perl/ch-2.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+use Math::Combinatorics;
+
+sub total_xor(@ints) {
+ my $total = 0;
+ for my $size (1 .. @ints) {
+ my $comb = 'Math::Combinatorics'->new(count => $size, data => \@ints);
+ while (my @combo = $comb->next_combination) {
+ my $xor = 0;
+ $xor ^= $_ for @combo;
+ $total += $xor;
+ }
+ }
+ return $total
+}
+
+use Test::More tests => 3;
+
+is total_xor(1, 3), 6, 'Example 1';
+is total_xor(5, 1, 6), 28, 'Example 2';
+is total_xor(3, 4, 5, 6, 7, 8), 480, 'Example 3';