diff options
| author | E. Choroba <choroba@matfyz.cz> | 2025-06-02 15:36:42 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2025-06-02 15:36:42 +0200 |
| commit | 3594510a5b9f1dadf3983f0b430fe8b9398f0ea9 (patch) | |
| tree | 4c9056ec1c8cf2316c741355a1248e79bfc3264e | |
| parent | b0159e77cc4e56da3a5a1c86d5769c652bcc887e (diff) | |
| download | perlweeklychallenge-club-3594510a5b9f1dadf3983f0b430fe8b9398f0ea9.tar.gz perlweeklychallenge-club-3594510a5b9f1dadf3983f0b430fe8b9398f0ea9.tar.bz2 perlweeklychallenge-club-3594510a5b9f1dadf3983f0b430fe8b9398f0ea9.zip | |
Add solutions to 324: 2D Array & Total XOR by E. Choroba
| -rwxr-xr-x | challenge-324/e-choroba/perl/ch-1.pl | 20 | ||||
| -rwxr-xr-x | challenge-324/e-choroba/perl/ch-2.pl | 25 |
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'; |
