diff options
| -rwxr-xr-x | challenge-324/perlboy1967/perl/ch1.pl | 35 | ||||
| -rwxr-xr-x | challenge-324/perlboy1967/perl/ch2.pl | 44 |
2 files changed, 79 insertions, 0 deletions
diff --git a/challenge-324/perlboy1967/perl/ch1.pl b/challenge-324/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..9621c376be --- /dev/null +++ b/challenge-324/perlboy1967/perl/ch1.pl @@ -0,0 +1,35 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 324 +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-324#TASK1> + +Author: Niels 'PerlBoy' van Dijke + +Task 1: 2D Array +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers and two integers $r amd $c. + +Write a script to create two dimension array having $r rows and $c columns +using the given array. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); +use Test2::V0 qw(-no_srand); +no warnings qw(experimental::signatures); + +sub to2D ($r,$c,@ints) { + return () if ($r * $c != scalar(@ints)); + return map { [splice(@ints,0,$c)] } 0 .. $#ints/$c; +} + +is([to2D(2,2,1,2,3,4)],[[1,2],[3,4]],'Example 1'); +is([to2D(1,3,1,2,3)],[[1,2,3]],'Example 2'); +is([to2D(4,1,1,2,3,4)],[[1],[2],[3],[4]],'Example 3'); + +done_testing; diff --git a/challenge-324/perlboy1967/perl/ch2.pl b/challenge-324/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..e3ea35aedb --- /dev/null +++ b/challenge-324/perlboy1967/perl/ch2.pl @@ -0,0 +1,44 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 324 +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-324#TASK2> + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Total XOR +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers. + +Write a script to return the sum of total XOR for every subset of given array. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); +use Test2::V0 qw(-no_srand); +no warnings qw(experimental::signatures); + +use Algorithm::Combinatorics qw(combinations); +use List::AllUtils qw(sum); + +sub totalXor (@ints) { + my $sum = sum(@ints); + for my $dim (2 .. scalar(@ints)) { + for my $a (combinations(\@ints,$dim)) { + my $xor = shift (@$a); + $xor ^= $_ for (@$a); + $sum += $xor; + } + } + return $sum; +} + +is(totalXor(1,3),6,'Example 1'); +is(totalXor(5,1,6),28,'Example 2'); +is(totalXor(3,4,5,6,7,8),480,'Example 3'); + +done_testing; |
