diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-06 23:25:46 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-06 23:25:46 +0100 |
| commit | e400c589e0601c7d1c643e7c83f3a13eb39336c7 (patch) | |
| tree | 11b01c2a737d54cf9c31a73130b4afb93cd84f0f | |
| parent | 288f6ae04ecb341b5f668fcde869d30cccd84cf8 (diff) | |
| parent | d50496d07d8e2e581617cc51034088410d2502f2 (diff) | |
| download | perlweeklychallenge-club-e400c589e0601c7d1c643e7c83f3a13eb39336c7.tar.gz perlweeklychallenge-club-e400c589e0601c7d1c643e7c83f3a13eb39336c7.tar.bz2 perlweeklychallenge-club-e400c589e0601c7d1c643e7c83f3a13eb39336c7.zip | |
Merge pull request #12133 from mahnkong/challenge-324
Challenge 324
| -rw-r--r-- | challenge-324/mahnkong/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-324/mahnkong/perl/ch-2.pl | 33 |
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"); |
