diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-13 01:14:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-13 01:14:16 +0000 |
| commit | 5f9ad3eb76b08a6e2adbae41552dbeb88d122485 (patch) | |
| tree | 5780a2b08ac3b5a8bb0f39b3c958e67c6b9e6c13 | |
| parent | 90ca0d9c3eab8fc6e8561902071089062bd2933b (diff) | |
| parent | 10a2ec6a604cef32d7d37869b32a61eba46cf60d (diff) | |
| download | perlweeklychallenge-club-5f9ad3eb76b08a6e2adbae41552dbeb88d122485.tar.gz perlweeklychallenge-club-5f9ad3eb76b08a6e2adbae41552dbeb88d122485.tar.bz2 perlweeklychallenge-club-5f9ad3eb76b08a6e2adbae41552dbeb88d122485.zip | |
Merge pull request #9575 from mattneleigh/pwc256
Pwc256
| -rwxr-xr-x | challenge-256/mattneleigh/perl/ch-1.pl | 76 | ||||
| -rwxr-xr-x | challenge-256/mattneleigh/perl/ch-2.pl | 77 |
2 files changed, 153 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..8e275c5719 --- /dev/null +++ b/challenge-256/mattneleigh/perl/ch-2.pl @@ -0,0 +1,77 @@ +#!/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 $length1 = length($ARG[0]); + my $length2 = length($ARG[1]); + my $minimum_common_length = $length1 < $length2 ? + $length1 + : + $length2; + my $merged = ""; + + # Over the length the strings have in common, + # concatenate a pair of characters from each + for my $pos (0 .. $minimum_common_length - 1){ + $merged .= + substr($ARG[0], $pos, 1) + . + substr($ARG[1], $pos, 1); + } + + # At least one string has been used up; if one + # is longer than the minimum common length, + # concatenate the remaining characters from it + if($length1 > $minimum_common_length){ + $merged .= substr($ARG[0], $minimum_common_length); + } elsif($length2 > $minimum_common_length){ + $merged .= substr($ARG[1], $minimum_common_length); + } + + return($merged); + +} + + + |
