aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-275/mattneleigh/perl/ch-1.pl95
-rwxr-xr-xchallenge-275/mattneleigh/perl/ch-2.pl98
2 files changed, 193 insertions, 0 deletions
diff --git a/challenge-275/mattneleigh/perl/ch-1.pl b/challenge-275/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..f4749aefe9
--- /dev/null
+++ b/challenge-275/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,95 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @sentence_data_sets = (
+ [
+ "Perl Weekly Challenge",
+ [ "l", "a" ]
+ ],
+ [
+ "Perl and Raku",
+ [ "a" ]
+ ],
+ [
+ "Well done Team PWC",
+ [ "l", "o" ]
+ ],
+ [
+ "The joys of polyglottism",
+ [ "T" ]
+ ]
+);
+
+print("\n");
+foreach my $sentence_data (@sentence_data_sets){
+ printf(
+ "Input: \$sentence = \"%s\", \@keys = (%s)\nOutput: %d\n\n",
+ $sentence_data->[0],
+ join(
+ ", ",
+ map(
+ "'" . $_ . "'",
+ @{$sentence_data->[1]}
+ )
+ ),
+ count_words_with_unbroken_keys($sentence_data)
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a sentence and a list of keys on a keyboard that are broken, determine
+# how many words in the sentence can be typed properly, without the use of the
+# broken keys
+# Takes one argument:
+# * A ref to an array that contains the sentence to examine, and an array of
+# broken keys (e.g.
+# [
+# "Well done Team PWC",
+# [ "l", "o" ]
+# ]
+# )
+# Returns:
+# * The count of words within the sentence that can be typed without the use of
+# the broken keys (e.g. 2 )
+################################################################################
+sub count_words_with_unbroken_keys{
+ my $sentence_and_keys = shift();
+
+ my $unbroken = 0;
+
+ # Make a character class based on the broken
+ # keys
+ my $expr = "[" . join("", @{$sentence_and_keys->[1]}) . "]";
+
+ # Compile and store a regex for future use-
+ # with case-insensitive matching
+ $expr = qr/$expr/i;
+
+ # Examine each word in the sentence
+ foreach my $word (split(" ", $sentence_and_keys->[0])){
+ # Increment the counter of unbroken words if
+ # this word does NOT include a broken key
+ $unbroken++
+ if($word !~ $expr);
+ }
+
+ return($unbroken);
+
+}
+
+
+
diff --git a/challenge-275/mattneleigh/perl/ch-2.pl b/challenge-275/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..3967338edd
--- /dev/null
+++ b/challenge-275/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,98 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @strings = (
+ # Given cases
+ "a1c1e1",
+ "a1b2c3d4",
+ "b2b",
+ "a16z",
+
+ # Additional test cases
+ "z1",
+ "_123V_456_"
+);
+
+print("\n");
+foreach my $string (@strings){
+ printf(
+ "Input: \$str = '%s'\nOuput: '%s'\n\n",
+ $string,
+ character_advance($string)
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a string of letters (within the English alphabet) and digits, replace
+# each digit with the previous letter, advanced by the value of the digit.
+# Digits that precede the first letter will be removed from the resulting
+# string, while non-letter and non-digit characters will be ignored; if a
+# computed character value lies beyond the alphabet, it will be wrapped around
+# to the beginning (i.e. "z5" -> "ze").
+# Takes one argument:
+# * The string to examine (e.g. "a1c1e1")
+# Returns:
+# * The modified string (e.g. "abcdef")
+################################################################################
+sub character_advance{
+ my @chars = split("", shift());
+
+ my $last_letter = undef;
+ my $char_value;
+
+ # Examine each character
+ foreach my $i (0 .. $#chars){
+ if($chars[$i] =~ m/\d/){
+ # This character is a digit
+ if(defined($last_letter)){
+ # A previous letter was defined- replace
+ # the digit with that letter, advanced
+ # by the value of the current digit
+ $char_value = ord($chars[$last_letter]) + $chars[$i];
+
+ # Wrap around unless we haven't exceeded
+ # the bounds of the alphabet
+ $char_value -= 26
+ unless(
+ # Upper case
+ (($char_value > 64) && ($char_value < 91))
+ ||
+ # Lower case
+ (($char_value > 96) && ($char_value < 123))
+ );
+
+ # Store the new letter in place of
+ # the digit
+ $chars[$i] = chr($char_value);
+ } else{
+ # No previous letter was defined- replace
+ # with the empty string
+ $chars[$i] = "";
+ }
+ } elsif($chars[$i] =~ m/[A-Za-z]/){
+ # This character is a letter- make a note
+ # of its location
+ $last_letter = $i;
+ }
+ }
+
+ return(join("", @chars));
+
+}
+
+
+