aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Mahnke <andreas.mahnke@leuphana.de>2025-06-06 15:25:25 +0200
committerAndreas Mahnke <andreas.mahnke@leuphana.de>2025-06-06 15:25:25 +0200
commitd50496d07d8e2e581617cc51034088410d2502f2 (patch)
treebfbdf996e99e9f2501c81afdfc05b80a85172c2e
parentcd87a854dd039199fdc61c1a021a7a2b3e6b180e (diff)
downloadperlweeklychallenge-club-d50496d07d8e2e581617cc51034088410d2502f2.tar.gz
perlweeklychallenge-club-d50496d07d8e2e581617cc51034088410d2502f2.tar.bz2
perlweeklychallenge-club-d50496d07d8e2e581617cc51034088410d2502f2.zip
Challenge 324
-rw-r--r--challenge-324/mahnkong/perl/ch-1.pl23
-rw-r--r--challenge-324/mahnkong/perl/ch-2.pl33
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-324/mahnkong/perl/ch-1.pl b/challenge-324/mahnkong/perl/ch-1.pl
new file mode 100644
index 0000000000..daeffccdce
--- /dev/null
+++ b/challenge-324/mahnkong/perl/ch-1.pl
@@ -0,0 +1,23 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run($ints, $r, $c) {
+ my $result = [];
+ my @row;
+ for (my $i = 0; $i < scalar(@$ints); $i++) {
+ push @row, $ints->[$i];
+ if (scalar(@row) == $c) {
+ push @$result, [@row];
+ last if (scalar(@$result) == $r);
+ @row=();
+ }
+ }
+ return $result;
+}
+
+is_deeply(run([1, 2, 3, 4], 2, 2), [[1, 2], [3, 4]], "Example 1");
+is_deeply(run([1, 2, 3], 1, 3), [[1, 2, 3]], "Example 2");
+is_deeply(run([1, 2, 3, 4], 4, 1), [[1], [2], [3], [4]], "Example 3");
+is_deeply(run([1, 2, 3, 4], 3, 1), [[1], [2], [3]], "Example 4");
diff --git a/challenge-324/mahnkong/perl/ch-2.pl b/challenge-324/mahnkong/perl/ch-2.pl
new file mode 100644
index 0000000000..42358dd62a
--- /dev/null
+++ b/challenge-324/mahnkong/perl/ch-2.pl
@@ -0,0 +1,33 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run(@ints) {
+ my $result = 0;
+ my $n = @ints;
+ my $total = 2 ** $n;
+
+ for my $i (0 .. $total - 1) {
+ my @set;
+ for my $j (0 .. $n - 1) {
+ if ($i & (1 << $j)) {
+ push @set, $ints[$j];
+ }
+ }
+ if (scalar(@set) > 1) {
+ my $intermediate = $set[0];
+ for (my $i = 1; $i <= $#set; $i++) {
+ $intermediate ^= $set[$i];
+ }
+ $result += $intermediate;
+ } elsif (defined $set[0]) {
+ $result += $set[0];
+ }
+ }
+ return $result;
+}
+
+is(run(1, 3), 6, "Example 1");
+is(run(5, 1, 6), 28, "Example 2");
+is(run(3, 4, 5, 6, 7, 8), 480, "Example 3");