diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-18 22:51:53 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-18 22:51:53 +0000 |
| commit | 47a72491bde0dcae70de209e0aa56bff3fb03431 (patch) | |
| tree | 67b1c1d53fb445b1ba75c266091e61897449be12 | |
| parent | b40455c574bc569d19a3d104f18fced741ae5c49 (diff) | |
| parent | aaf84f7b7741d9d5bc99c169664a97831a41833e (diff) | |
| download | perlweeklychallenge-club-47a72491bde0dcae70de209e0aa56bff3fb03431.tar.gz perlweeklychallenge-club-47a72491bde0dcae70de209e0aa56bff3fb03431.tar.bz2 perlweeklychallenge-club-47a72491bde0dcae70de209e0aa56bff3fb03431.zip | |
Merge pull request #7745 from mattneleigh/pwc208
new file: challenge-208/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-208/mattneleigh/perl/ch-1.pl | 137 | ||||
| -rwxr-xr-x | challenge-208/mattneleigh/perl/ch-2.pl | 115 |
2 files changed, 252 insertions, 0 deletions
diff --git a/challenge-208/mattneleigh/perl/ch-1.pl b/challenge-208/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..e3995c92b4 --- /dev/null +++ b/challenge-208/mattneleigh/perl/ch-1.pl @@ -0,0 +1,137 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @list_pairs = ( + [ + [ "Perl", "Raku", "Love" ], + [ "Raku", "Perl", "Hate" ] + ], + [ + [ "A", "B", "C" ], + [ "D", "E", "F" ] + ], + [ + [ "A", "B", "C" ], + [ "C", "A", "B" ] + ] +); + +print("\n"); +foreach my $list_pair (@list_pairs){ + printf( + "Input: \@list1 = (%s)\n \@list2 = (%s)\nOutput: (%s)\n\n", + list_to_quoted_string(@{$list_pair->[0]}), + list_to_quoted_string(@{$list_pair->[1]}), + list_to_quoted_string( + common_minimum_index_sum($list_pair->[0], $list_pair->[1]) + ) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Find the matching string(s) between two lists of strings, which also have the +# minimum sum of their respective indices within each list +# Takes two arguments: +# * A ref to the first list of strings to examine +# * A ref to the second list of strings to examine +# Returns: +# * A list of matches among the two lists that have the minimum sum of their +# respective indices within the original lists; this list will be empty if no +# matches are found, or may have multiple members if multiple matches are +# tied for the lowest index sum; the sum itself is not reported +################################################################################ +sub common_minimum_index_sum{ + my $list1 = shift(); + my $list2 = shift(); + + my @matches = (); + + # Compare each member of the first + # list with each member of the second, + # and store each match along with the + # sum of the matching members' indices + for my $i (0 .. $#$list1){ + for my $j (0 .. $#$list2){ + if($list1->[$i] eq $list2->[$j]){ + push(@matches, [ $list1->[$i], $i + $j ]); + } + } + } + + if(scalar(@matches)){ + my $index_sum; + + # There were matches; sort them in + # ascending order by their index sums + @matches = sort({ $a->[1] <=> $b->[1] } @matches); + + # Store the first (lowest) index sum + # and simplifiy the first member of + # the match list + $index_sum = $matches[0][1]; + $matches[0] = $matches[0][0]; + + # Compare the first sum with + # subsequent sums + for my $i (1 .. $#matches){ + if($matches[$i][1] == $index_sum){ + # The sum matched- simplify this + # member of the match list + $matches[$i] = $matches[$i][0]; + } else{ + # It didn't match- delete this and + # all subsequent matches, and break + # out of the loop + splice(@matches, $i); + last; + } + } + } + + return(@matches); + +} + + + +################################################################################ +# Build a quoted, comma-separated string out of the contents of a list +# Takes one argument: +# * The list (e.g. ( 1, 2, 3, 4 ) ) +# Returns: +# * A quoted, comma-separated string containing the contents of the list (e.g. +# ""1", "2", "3", "4"" ) +################################################################################ +sub list_to_quoted_string{ + + return( + # (2) Join the quoted strings together + # with commas + join( + ", ", + # (1) Put quotes around each list member + map( + "\"".$_."\"", + @ARG + ) + ) + ); + +} + + + diff --git a/challenge-208/mattneleigh/perl/ch-2.pl b/challenge-208/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..3a34cd7d87 --- /dev/null +++ b/challenge-208/mattneleigh/perl/ch-2.pl @@ -0,0 +1,115 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_lists = ( + # Given cases + [ 1, 2, 2, 4 ], + [ 1, 2, 3, 4 ], + [ 1, 2, 3, 3 ], + + # Additional test cases + [ 1, 1, 3, 4 ], + [ 1, 1, 2, 3 ], + [ 1, 3, 3, 4 ], + [ 1, 2, 4, 4 ] +); + +print("\n"); +foreach my $list (@integer_lists){ + my $result = find_duplicate_and_missing(@{$list}); + + if(defined($result)){ + # $result is defined- stringify the + # contents pointed to by the returned + # array ref + $result = sprintf("(%s)", join(", ", @{$result})); + } else{ + # $result is undefined- set it to -1 + $result = -1; + } + + printf( + "Input: \@nums = (%s)\nOutput: %s\n\n", + join(", ", @{$list}), + $result + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Find a duplicate and missing integer in a sequence of otherwise-consecutive +# integers in ascending order +# Takes one argument: +# * A list of integers, in ascending order, to examine (e.g. ( 1, 2, 2, 4 ) ) +# Returns: +# * A ref to a list containing the duplicate number, and the missing number +# (e.g. [ 2, 3 ] ) Note that the missing number may be the one before or +# after the duplicate, in ordinal sequence, depending on the contents of the +# supplied list +# - OR - +# * undef if no duplicate was found in the list +################################################################################ +sub find_duplicate_and_missing{ + + for my $i (0 .. ($#ARG - 1)){ + if($ARG[$i] == $ARG[$i + 1]){ + # We have a duplicate... + if($i == 0){ + # ...at the start of the list; return + # the value in cell 0 and either the + # preceeding or succeeding would-be + # value, depending on which looks to + # be missing + return( + [ + $ARG[0], + ( + $ARG[2] == ($ARG[0] + 2) ? + $ARG[0] + 1 + : + $ARG[0] - 1 + ) + ] + ); + } else{ + # ...somewhere in the middle or + # the end of the list; return the + # value in cell $i and either the + # preceeding or succeeding would-be + # value, depending on which looks to + # be missing + return( + [ + $ARG[$i], + ( + $ARG[$i - 1] == ($ARG[$i] - 1) ? + $ARG[$i] + 1 + : + $ARG[$i] - 1 + ) + ] + ); + } + } + } + + # We didn't find a duplicate + return(undef); + +} + + + |
