diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-07 14:41:23 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-07 14:41:23 +0000 |
| commit | 52f6dabcc3d14efccdf8e925a8a7d2af9d9b7556 (patch) | |
| tree | 8b5b990e1ef98d4952ebbb4b072435652e2f30d3 | |
| parent | e83db97b5bc19d8f26345af7bf2be1204433e730 (diff) | |
| parent | f63c30d4675142225e18b1c38900247091eab189 (diff) | |
| download | perlweeklychallenge-club-52f6dabcc3d14efccdf8e925a8a7d2af9d9b7556.tar.gz perlweeklychallenge-club-52f6dabcc3d14efccdf8e925a8a7d2af9d9b7556.tar.bz2 perlweeklychallenge-club-52f6dabcc3d14efccdf8e925a8a7d2af9d9b7556.zip | |
Merge pull request #9018 from mattneleigh/pwc242
new file: challenge-242/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-242/mattneleigh/perl/ch-1.pl | 100 | ||||
| -rwxr-xr-x | challenge-242/mattneleigh/perl/ch-2.pl | 103 |
2 files changed, 203 insertions, 0 deletions
diff --git a/challenge-242/mattneleigh/perl/ch-1.pl b/challenge-242/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..acb131c44f --- /dev/null +++ b/challenge-242/mattneleigh/perl/ch-1.pl @@ -0,0 +1,100 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @array_pairs = ( + [ + [ 1, 2, 3 ], + [ 2, 4, 6 ] + ], + [ + [ 1, 2, 3, 3 ], + [ 1, 1, 2, 2 ] + ] +); + +print("\n"); +foreach my $array_pair (@array_pairs){ + printf( + "Input: \@arr1 = (%s)\n" . + " \@arr2 = (%s)\n" . + "Output: (%s)\n\n", + join(", ", @{$array_pair->[0]}), + join(", ", @{$array_pair->[1]}), + join( + ", ", + map( + "[" . join(", ", @{$_}) . "]", + find_missing_numbers_in_array_pair($array_pair) + ) + ) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given two arrays of integers, find those that are present in each array but +# missing in the other +# Takes one argument: +# * A ref to a pair of arrays of integers (e.g. +# [ +# [ 1, 2, 3, 3 ], +# [ 1, 1, 2, 2 ] +# ] +# ) +# Returns: +# * A list of two arrays, each containing the integers from the corresponding +# input array that were missing from the other array. If a missing number +# appeared more than once in its input array, it will only appear once in the +# output array; if no numbers were missing, the output array will be empty +# (e.g. +# ( +# [ 3 ], +# [ ] +# ) +# ) +################################################################################ +sub find_missing_numbers_in_array_pair{ + + my @missing = ([], []); + + # Loop over two pairs of indices + for my $indices ([0, 1], [1, 0]){ + # Make a lookup table so we can easily see + # what's in one of the arrays + my %lookup = map( + { $_ => 1; } + @{$ARG[0][$indices->[0]]} + ); + my %seen = (); + + # Loop over the contents of the 'other' array + foreach my $num (@{$ARG[0][$indices->[1]]}){ + # If this number isn't in the first array and + # it hasn't been seen, store it + push(@{$missing[$indices->[1]]}, $num) + unless($lookup{$num} || $seen{$num}); + + # Mark this number as seen + $seen{$num} = 1; + } + } + + return(@missing); + +} + + + diff --git a/challenge-242/mattneleigh/perl/ch-2.pl b/challenge-242/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..55e9f17fbd --- /dev/null +++ b/challenge-242/mattneleigh/perl/ch-2.pl @@ -0,0 +1,103 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @matrices = ( + [ + [ 1, 1, 0 ], + [ 1, 0, 1 ], + [ 0, 0, 0 ] + ], + [ + [ 1, 1, 0, 0 ], + [ 1, 0, 0, 1 ], + [ 0, 1, 1, 1 ], + [ 1, 0, 1, 0 ] + ] +); + +print("\n"); +foreach my $matrix (@matrices){ + printf( + "Input: \@matrix = (%s)\nOutput: (%s)\n\n", + join( + ", ", + map( + "[" . join(", ", @{$_}) . "]", + @{$matrix} + ) + ), + join( + ", ", + map( + "[" . join(", ", @{$_}) . "]", + @{flip_binary_matrix($matrix)} + ) + ) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Flip a binary matrix (made of ones and zeros only) according to the following +# procedure: +# 1) Invert each member of the matrix, changing 0 to 1 and vice-versa +# 2) Reverse the order of members within each row of the matrix +# Takes one argument: +# * A ref to an array of arrays the represents the matrix (e.g. +# [ +# [ 1, 1, 0 ], +# [ 1, 0, 1 ], +# [ 0, 0, 0 ] +# ] +# ) +# Returns: +# * A ref to a flipped copy of the matrix (e.g. +# [ +# [ 1, 0, 0 ], +# [ 0, 1, 0 ], +# [ 1, 1, 1 ] +# ] +# ) +################################################################################ +sub flip_binary_matrix{ + + return( + # 4: Get a ref to the matrix we've made + [ + # 3: Combine all new rows into a new matrix + # array + map( + [ + # 2: Reverse the order of members within the + # row + reverse( + # 1: Make a copy of each row with the members + # inverted + map( + $_ ? 0 : 1, + @{$_} + ) + ) + ], + @{$ARG[0]} + ) + ] + ); + +} + + + |
