From c82ef9090aa1c934c8515ea574f3148388db6659 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Tue, 7 Nov 2023 08:37:02 +0000 Subject: Week 242 ... --- challenge-242/peter-campbell-smith/blog.txt | 1 + challenge-242/peter-campbell-smith/perl/ch-1.pl | 44 ++++++++++++++++++++++ challenge-242/peter-campbell-smith/perl/ch-2.pl | 50 +++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 challenge-242/peter-campbell-smith/blog.txt create mode 100755 challenge-242/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-242/peter-campbell-smith/perl/ch-2.pl 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 -- cgit