diff options
| -rwxr-xr-x | challenge-256/mattneleigh/perl/ch-1.pl | 76 | ||||
| -rwxr-xr-x | challenge-256/mattneleigh/perl/ch-2.pl | 69 |
2 files changed, 145 insertions, 0 deletions
diff --git a/challenge-256/mattneleigh/perl/ch-1.pl b/challenge-256/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..eb6b3a9628 --- /dev/null +++ b/challenge-256/mattneleigh/perl/ch-1.pl @@ -0,0 +1,76 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @word_lists = ( + [ "ab", "de", "ed", "bc" ], + [ "aa", "ba", "cd", "ed" ], + [ "uv", "qp", "st", "vu", "mn", "pq" ] +); + +print("\n"); +foreach my $word_list (@word_lists){ + printf( + "Input: \@words = (%s)\nOutput: %d\n\n", + join( + ", ", + map( + "\"" . $_ . "\"", + @{$word_list} + ) + ), + count_reversed_pairs(@{$word_list}) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a list of distinct words, count the number of reversed pairs in the +# list- that is to say, the pairs of strings in which one string is the reverse +# of the other +# Takes one argument: +# * The list of words to examine (e.g. ( "uv", "qp", "st", "vu", "mn", "pq" ) ) +# Returns: +# * The count of reversed pairs (e.g. 2 ) +################################################################################ +sub count_reversed_pairs{ + + my $pairs = 0; + my @reversed; + my $j; + + # Store reversed copies of every word that $j + # will refer to later in the nested loop below + for $j (1 .. $#ARG){ + $reversed[$j] = join("", reverse(split("", $ARG[$j]))); + } + + # Compare every original string with every + # reversed equivalent + for my $i (0 .. $#ARG - 1){ + for $j ($i + 1 .. $#ARG){ + # Increment the counter if the string at $i + # matches the reversed string at $j + $pairs++ + if($ARG[$i] eq $reversed[$j]); + } + } + + return($pairs); + +} + + + diff --git a/challenge-256/mattneleigh/perl/ch-2.pl b/challenge-256/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..c18b24970d --- /dev/null +++ b/challenge-256/mattneleigh/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @string_lists = ( + [ "abcd", "1234" ], + [ "abc", "12345" ], + [ "abcde", "123" ] +); + +print("\n"); +foreach my $string_list (@string_lists){ + printf( + "Input: \$str1 = \"%s\", \$str2 = \"%s\"\nOutput: \"%s\"\n\n", + $string_list->[0], + $string_list->[1], + merge_from_alternate_strings(@{$string_list}) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given two strings, merge them by combining one character from each, in +# alternating order, starting with the first string; if one string is longer +# than the other, remaining characters from it will be appended at the end +# Takes two arguments: +# * The first string to concatenate (e.g. "abc") +# * The second string to concatenate (e.g. "12345") +# Returns: +# * A string merged as defined above (e.g. "a1b2c345") +################################################################################ +sub merge_from_alternate_strings{ + + my @str1 = split("", $ARG[0]); + my @str2 = split("", $ARG[1]); + my $merged = ""; + + # Concatenate characters from @str1 and @str2 + # while neither is empty + while(@str1 && @str2){ + $merged .= shift(@str1) . shift(@str2); + } + + # At least one array is now empty; if anything + # remains in either, concatenate what's left + if(@str1){ + $merged .= join("", @str1); + } elsif(@str2){ + $merged .= join("", @str2); + } + + return($merged); + +} + + + |
