diff options
| -rw-r--r-- | challenge-324/zapwai/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-324/zapwai/perl/ch-2.pl | 29 |
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-324/zapwai/perl/ch-1.pl b/challenge-324/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..3268d8fa47 --- /dev/null +++ b/challenge-324/zapwai/perl/ch-1.pl @@ -0,0 +1,24 @@ +use v5.40; + +sub proc($r, $c, @ints) { + say "Input: \@ints = @ints, \$r = $r, \$c = $c"; + die "wrong dimensions for this data" if (@ints != $r * $c); + my $cell = 0; + my @o; + for my $row (1 .. $r) { + my @x; + for my $col (1 .. $c) { + push @x, $ints[$cell++]; + } + push @o, \@x; + } + say "Output: "; + say "@$_" foreach (@o); +} + +my @ints = (1, 2, 3, 4); my $r = 2; my $c = 2; +proc($r, $c, @ints); +@ints = (1, 2, 3); $r = 1; $c = 3; +proc($r, $c, @ints); +@ints = (1, 2, 3, 4); $r = 4; $c = 1; +proc($r, $c, @ints); diff --git a/challenge-324/zapwai/perl/ch-2.pl b/challenge-324/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..38aec00ec7 --- /dev/null +++ b/challenge-324/zapwai/perl/ch-2.pl @@ -0,0 +1,29 @@ +use v5.40; +use Data::PowerSet qw( powerset ); + +sub xorsum(@a) { + return $a[0] if (@a == 1); + my $sum = 0; + $sum ^= $_ for (@a); + return $sum; +} + +sub proc(@ints) { + say "Input: \@ints = @ints"; + my $pow = powerset(@ints); + my $tot = 0; + for my $p (@$pow) { + #print "\t\t@$p\t:"; + my $x = xorsum(@$p); + #say "xorsum: $x"; + $tot += $x; + } + say "Output: $tot"; +} + +my @ints = (1,3); +proc(@ints); +@ints = (5,1,6); +proc(@ints); +@ints = (3, 4, 5, 6, 7, 8); +proc(@ints); |
