From 9efe348c32f917965b3716d031f0ee48b40d6714 Mon Sep 17 00:00:00 2001 From: Matthew Neleigh Date: Fri, 3 Oct 2025 05:29:22 -0400 Subject: renamed: challenge-341/mattneleigh/ch-1.pl -> challenge-341/mattneleigh/perl/ch-1.pl renamed: challenge-341/mattneleigh/ch-2.pl -> challenge-341/mattneleigh/perl/ch-2.pl --- challenge-341/mattneleigh/ch-1.pl | 94 ---------------------------------- challenge-341/mattneleigh/ch-2.pl | 84 ------------------------------ challenge-341/mattneleigh/perl/ch-1.pl | 94 ++++++++++++++++++++++++++++++++++ challenge-341/mattneleigh/perl/ch-2.pl | 84 ++++++++++++++++++++++++++++++ 4 files changed, 178 insertions(+), 178 deletions(-) delete mode 100755 challenge-341/mattneleigh/ch-1.pl delete mode 100755 challenge-341/mattneleigh/ch-2.pl create mode 100755 challenge-341/mattneleigh/perl/ch-1.pl create mode 100755 challenge-341/mattneleigh/perl/ch-2.pl diff --git a/challenge-341/mattneleigh/ch-1.pl b/challenge-341/mattneleigh/ch-1.pl deleted file mode 100755 index a3d3442276..0000000000 --- a/challenge-341/mattneleigh/ch-1.pl +++ /dev/null @@ -1,94 +0,0 @@ -#!/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 deleted file mode 100755 index cd00fbe876..0000000000 --- a/challenge-341/mattneleigh/ch-2.pl +++ /dev/null @@ -1,84 +0,0 @@ -#!/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]); - -} - - - diff --git a/challenge-341/mattneleigh/perl/ch-1.pl b/challenge-341/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..a3d3442276 --- /dev/null +++ b/challenge-341/mattneleigh/perl/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/perl/ch-2.pl b/challenge-341/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..cd00fbe876 --- /dev/null +++ b/challenge-341/mattneleigh/perl/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]); + +} + + + -- cgit