aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2023-11-07 08:37:02 +0000
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2023-11-07 08:37:02 +0000
commitc82ef9090aa1c934c8515ea574f3148388db6659 (patch)
treeb3c764461486a933531e3b20acf1a0dd1d631a69
parenta82dd587d3773d2a36a1fcc4b3c525c031433a4a (diff)
downloadperlweeklychallenge-club-c82ef9090aa1c934c8515ea574f3148388db6659.tar.gz
perlweeklychallenge-club-c82ef9090aa1c934c8515ea574f3148388db6659.tar.bz2
perlweeklychallenge-club-c82ef9090aa1c934c8515ea574f3148388db6659.zip
Week 242 ...
-rw-r--r--challenge-242/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-242/peter-campbell-smith/perl/ch-1.pl44
-rwxr-xr-xchallenge-242/peter-campbell-smith/perl/ch-2.pl50
3 files changed, 95 insertions, 0 deletions
diff --git a/challenge-242/peter-campbell-smith/blog.txt b/challenge-242/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..63e07a2698
--- /dev/null
+++ b/challenge-242/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/242
diff --git a/challenge-242/peter-campbell-smith/perl/ch-1.pl b/challenge-242/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..768eea1e1e
--- /dev/null
+++ b/challenge-242/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+use v5.16; # The Weekly Challenge - 2023-11-06
+use utf8; # Week 242 task 1 - Missing members
+use strict; # Peter Campbell Smith
+use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use List::Uniq 'uniq';
+
+missing_members([1, 2, 3], [2, 4, 6]);
+missing_members([1, 2, 3, 3], [1, 1, 2, 2]);
+missing_members([1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 4, 6, 8, 10]);
+
+# make bigger example
+my ($j, @arr1, @arr2);
+for $j (0 .. 29) {
+ push @arr1, int(rand(15) + 1);
+ push @arr2, int(rand(15) + 1);
+}
+missing_members(\@arr1, \@arr2);
+
+sub missing_members {
+
+ printf('%sInput: ([%s], [%s])%s', "\n", join(', ', @{$_[0]}), join(', ', @{$_[1]}), "\n");
+ printf('Output: ([%s], [%s])%s', mm($_[0], $_[1]), mm($_[1], $_[0]), "\n");
+}
+
+sub mm {
+
+ my (@one, @two, $j);
+
+ # make each array unique
+ @one = uniq(@{$_[0]});
+ @two = uniq(@{$_[1]});
+
+ # delete from @one anything that occurs in @two
+ for $j (@two) {
+ @one = grep { $_ != $j } @one;
+ }
+
+ # and return what's left
+ return join(', ', @one);
+}
+ \ No newline at end of file
diff --git a/challenge-242/peter-campbell-smith/perl/ch-2.pl b/challenge-242/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..cc4aa56913
--- /dev/null
+++ b/challenge-242/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/perl
+
+use v5.16; # The Weekly Challenge - 2023-11-06
+use utf8; # Week 242 task 2 - Flip matrix
+use strict; # Peter Campbell Smith
+use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+flip_matrix([1, 1, 0],
+ [0, 1, 1],
+ [0, 0, 1]);
+
+flip_matrix([1, 1, 0, 0],
+ [1, 0, 0, 1],
+ [0, 1, 1, 1],
+ [1, 0, 1, 0]);
+
+flip_matrix([1, 0, 0, 0, 0],
+ [0, 1, 0, 0, 0],
+ [0, 0, 1, 0, 0],
+ [0, 0, 0, 1, 0],
+ [0, 0, 0, 0, 1]);
+
+sub flip_matrix {
+
+ my ($n, $j, @row, @flipped);
+
+ # reverse and flip
+ $n = @{$_[0]} - 1;
+ for $j (0 .. $n) {
+ @row = @{$_[$j]};
+ $flipped[$j][$_] = 1 - $row[$n - $_] for (0 .. $n);
+ }
+
+ # show input and output
+ print_matrix(qq(\nInput: [), \@_);
+ print_matrix(qq(\nOutput: [), \@flipped)
+}
+
+sub print_matrix {
+
+ my ($legend, $matrix, $j);
+
+ # format rows of matrix
+ ($legend, $matrix) = @_;
+ for $j (0 .. @$matrix - 1) {
+ say qq[$legend] . join(', ', @{$matrix->[$j]}) . qq(]);
+ $legend = ' [';
+ }
+}
+ \ No newline at end of file