diff options
| -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]} + ) + ] + ); + +} + + + |
