diff options
| author | Matthew Neleigh <matthew.neleigh@gmail.com> | 2025-10-02 16:00:21 -0400 |
|---|---|---|
| committer | Matthew Neleigh <matthew.neleigh@gmail.com> | 2025-10-02 16:00:21 -0400 |
| commit | 99bb8cf653cf6a23c4a72a3f4e8fc0f3c42bb2e5 (patch) | |
| tree | 490cf1bb82ed36def176de47b2fdc367350b12f5 | |
| parent | bf077ef14568e6c2c0bfe6f8012aa141c4360544 (diff) | |
| download | perlweeklychallenge-club-99bb8cf653cf6a23c4a72a3f4e8fc0f3c42bb2e5.tar.gz perlweeklychallenge-club-99bb8cf653cf6a23c4a72a3f4e8fc0f3c42bb2e5.tar.bz2 perlweeklychallenge-club-99bb8cf653cf6a23c4a72a3f4e8fc0f3c42bb2e5.zip | |
new file: challenge-341/mattneleigh/ch-1.pl
new file: challenge-341/mattneleigh/ch-2.pl
| -rwxr-xr-x | challenge-341/mattneleigh/ch-1.pl | 94 | ||||
| -rwxr-xr-x | challenge-341/mattneleigh/ch-2.pl | 84 |
2 files changed, 178 insertions, 0 deletions
diff --git a/challenge-341/mattneleigh/ch-1.pl b/challenge-341/mattneleigh/ch-1.pl new file mode 100755 index 0000000000..a3d3442276 --- /dev/null +++ b/challenge-341/mattneleigh/ch-1.pl @@ -0,0 +1,94 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @keyboard_data_sets = ( + [ + "Hello World", + [ "d" ] + ], + [ + "apple banana cherry", + [ "a", "e" ] + ], + [ + "Coding is fun", + [ ] + ], + [ + "The Weekly Challenge", + [ "a","b" ] + ], + [ + "Perl and Python", + [ "p" ] + ] +); + +print("\n"); +foreach my $keyboard_data (@keyboard_data_sets){ + printf( + "Input: \$str = '%s', \@keys = (%s)\nOutput: %d\n\n", + $keyboard_data->[0], + join( + ", ", + @{$keyboard_data->[1]} + ), + count_typeable_words($keyboard_data) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Count the words in a string that can be typed with a keyboard that has a +# specified set of broken letter keys +# Takes one argument: +# * A ref to an array that contains the string to examine, and a list of +# broken letter keys (e.g. [ "apple banana cherry", [ "a", "e" ] ] ) +# Returns: +# * The number of words in the string that can be typed without using the +# broken keys (e.g. 0 ) +# Note that the examination process is not case-sensitive +################################################################################ +sub count_typeable_words{ + my @words = split(/ /, $ARG[0][0]); + + # If no keys are broken, all words can be + # typed + return(scalar(@words)) + unless(scalar(@{$ARG[0][1]})); + + # Set up and precompile a regex with a + # character class that includes the broken + # keys + my $broken = "[" . join("", @{$ARG[0][1]}) . "]"; + $broken = qr/$broken/i; + + my $count = 0; + my $word; + + # Loop over each word and count the words + # that do NOT include the broken keys + foreach $word (@words){ + $count++ + unless($word =~ m/$broken/); + } + + return($count); + +} + + + diff --git a/challenge-341/mattneleigh/ch-2.pl b/challenge-341/mattneleigh/ch-2.pl new file mode 100755 index 0000000000..cd00fbe876 --- /dev/null +++ b/challenge-341/mattneleigh/ch-2.pl @@ -0,0 +1,84 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @strings_and_letters = ( + # Given cases + [ "programming", "g" ], + [ "hello", "h" ], + [ "abcdefghij", "h" ], + [ "reverse", "s" ], + [ "perl", "r" ], + + # Additional test cases + [ "missing", "x" ] +); + +print("\n"); +foreach my $string_and_letter (@strings_and_letters){ + printf( + "Input: \$str = \"%s\", \$char = \"%s\"\nOutput: \"%s\"\n\n", + $string_and_letter->[0], + $string_and_letter->[1], + reverse_string_prefix($string_and_letter) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a string and a character to find therein, reverse the contents of the +# string from the beginning up to and including the location of the first +# instance of the specified character +# Takes one argument: +# * A ref to an array containing the string to examine and the character to +# search for (e.g. [ "programming", "g" ] ) +# Returns: +# * The supplied string, with all characters reversed up to and including the +# first instance of the specified character (e.g. "gorpramming" ) +# - OR - +# * The unmodified string if the specified character was not found +################################################################################ +sub reverse_string_prefix{ + + my $max = length($ARG[0][0]) - 1; + my $i; + my $char; + my $reverse = ""; + + # Loop over all letters in the string + for $i (0 .. $max){ + # Extract the letter at the current + # location and add it to the start of the + # reversed string + $char = substr($ARG[0][0], $i, 1); + $reverse = $char . $reverse; + + # If the current letter is the one we're + # looking for, concatenate the reversed + # string (which includes the current + # letter) with what's left of the original + # string, and return + return($reverse . substr($ARG[0][0], -($max - $i))) + if($char eq $ARG[0][1]); + } + + # If we got here, the desired letter was + # not found + return($ARG[0][0]); + +} + + + |
