aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-278/mattneleigh/perl/ch-1.pl71
-rwxr-xr-xchallenge-278/mattneleigh/perl/ch-2.pl68
2 files changed, 139 insertions, 0 deletions
diff --git a/challenge-278/mattneleigh/perl/ch-1.pl b/challenge-278/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..5afd13b38d
--- /dev/null
+++ b/challenge-278/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @sentences = (
+ "and2 Raku3 cousins5 Perl1 are4",
+ "guest6 Python1 most4 the3 popular5 is2 language7",
+ "Challenge3 The1 Weekly2"
+);
+
+print("\n");
+foreach my $sentence (@sentences){
+ printf(
+ "Input: \$str = \"%s\"\nOutput: \"%s\"\n\n",
+ $sentence,
+ sort_shuffled_words($sentence)
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a string containing shuffled words and a numerical suffix that defines
+# the order in which they should appear, correct the order of the sentence,
+# removing each word's suffix in the process
+# Takes one argument:
+# * A string containing words and their sorting order, separated by spaces
+# (e.g. "Challenge3 The1 Weekly2" )
+# Returns:
+# * The sorted sentence (e.g. "The Weekly Challenge" )
+################################################################################
+sub sort_shuffled_words{
+
+ return(
+ # 5: Combine the sorted words into a sentence
+ join(
+ " ",
+ # 4: Extract a list of just the words themselves
+ map(
+ $_->[0],
+ # 3: Sort the word list by the specified sorting order
+ sort(
+ { $a->[1] <=> $b->[1] }
+ # 2: Create an array of arrays containing the words and
+ # their sort order (e.g. [ "Challenge", 3 ] )
+ map(
+ [ ($_ =~ m/(.+)(\d+)$/) ],
+ # 1: Split the given sentence into its constituent
+ # words
+ split(/ /, shift())
+ )
+ )
+ )
+ )
+ );
+
+}
+
+
+
diff --git a/challenge-278/mattneleigh/perl/ch-2.pl b/challenge-278/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..f88887aca7
--- /dev/null
+++ b/challenge-278/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @words = (
+ [ "challenge", "e" ],
+ [ "programming", "a" ],
+ [ "champion", "b" ]
+);
+
+print("\n");
+foreach my $word (@words){
+ printf(
+ "Input: \$str = \"%s\", \$char = \"%s\"\nOuput: \"%s\"\n\n",
+ $word->[0],
+ $word->[1],
+ character_substring_sort($word)
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a word and a character to look for, sort all characters in the word up
+# to and including the first instance of the specified character, in
+# lexicographical order; if the character was not found within the word, no
+# changes will be made and the word returned without modification
+# Takes one argument:
+# * A ref to an array that contains a word and a character to look for (e.g.
+# [ "challenge", "e" ] )
+# Returns:
+# * The word processed as described above, with all characters up to and
+# including the first instance of the specified character sorted in
+# lexicographical order (e.g. "acehllnge" ). If the specified character was
+# not present in the word, the word is returned unmodified.
+################################################################################
+sub character_substring_sort{
+
+ # See if the word has the desired character...
+ if($ARG[0][0] =~ m/^([^$ARG[0][1]]*[$ARG[0][1]])(.*)$/){
+ # It did- we captured the first instance of it and all
+ # characters that preceded it in $1, and all subsequent
+ # characters in $2; split the contents of $1, sort
+ # them, join them together, concatenate them with $2,
+ # and return the result
+ return(join("", sort(split("", $1))) . $2);
+ } else{
+ # It didn't- return the unmodified string
+ return($ARG[0][0]);
+ }
+
+ return(0);
+
+}
+
+
+