aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-07-22 09:32:35 +0100
committerGitHub <noreply@github.com>2024-07-22 09:32:35 +0100
commit5f358152da93aeec4496a14dd7a576ffd6bd3208 (patch)
treef518482e095c215de48fef3fb91307908fcc402b
parent8d126c1681dba4412dbc1d5d29574bbc375bea15 (diff)
parentbe08cae19f652039be25cbae24c0142337386fbe (diff)
downloadperlweeklychallenge-club-5f358152da93aeec4496a14dd7a576ffd6bd3208.tar.gz
perlweeklychallenge-club-5f358152da93aeec4496a14dd7a576ffd6bd3208.tar.bz2
perlweeklychallenge-club-5f358152da93aeec4496a14dd7a576ffd6bd3208.zip
Merge pull request #10471 from mattneleigh/pwc279
new file: challenge-279/mattneleigh/perl/ch-1.pl
-rwxr-xr-xchallenge-279/mattneleigh/perl/ch-1.pl92
-rwxr-xr-xchallenge-279/mattneleigh/perl/ch-2.pl63
2 files changed, 155 insertions, 0 deletions
diff --git a/challenge-279/mattneleigh/perl/ch-1.pl b/challenge-279/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..3daa462f63
--- /dev/null
+++ b/challenge-279/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,92 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @letter_sets = (
+ [
+ [ 'R', 'E', 'P', 'L' ],
+ [ 3, 2, 1, 4 ]
+ ],
+ [
+ [ 'A', 'U', 'R', 'K' ],
+ [ 2, 4, 1, 3 ]
+ ],
+ [
+ [ 'O', 'H', 'Y', 'N', 'P', 'T' ],
+ [ 5, 4, 2, 6, 1, 3 ]
+ ]
+);
+
+print("\n");
+foreach my $letter_set (@letter_sets){
+ printf(
+ "Input: \@letters = (%s)\n\@weights = (%s)\nOutput: %s\n\n",
+ join(
+ ", ",
+ map(
+ "'" . $_ . "'",
+ @{$letter_set->[0]}
+ )
+ ),
+ join(
+ ", ",
+ @{$letter_set->[1]}
+ ),
+ combine_letters_by_weight($letter_set)
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a set of letters and relative numerical weights, combine the letters
+# into a single word, ordered by the specified weights
+# Takes one argument:
+# * A ref to an array that contains two parallel arrays- one of letters, and
+# one of relative weights (e.g.
+# [
+# [ 'R', 'E', 'P', 'L' ],
+# [ 3, 2, 1, 4 ]
+# ]
+# )
+# Returns:
+# * A word made up of the letters sorted by the specified weights (e.g. "PERL")
+################################################################################
+sub combine_letters_by_weight{
+
+ return(
+ # 4: Join the letters into a single word
+ join(
+ "",
+ # 3: Extract a list of just the sorted letters
+ map(
+ $_->[0],
+ # 2: Sort the list in ascending order by weight
+ sort(
+ { $a->[1] <=> $b->[1] }
+ # 1: Make a list of of letters and their specified
+ # weights (e.g. [ "R", 3 ] )
+ map(
+ [ $ARG[0][0][$_], $ARG[0][1][$_] ],
+ 0 .. $#{$ARG[0][0]}
+ )
+ )
+ )
+ )
+ );
+
+}
+
+
+
diff --git a/challenge-279/mattneleigh/perl/ch-2.pl b/challenge-279/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..3539be6207
--- /dev/null
+++ b/challenge-279/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @strings = (
+ "perl",
+ "book",
+ "good morning"
+);
+
+print("\n");
+foreach my $string (@strings){
+ printf(
+ "Input: \$str = \"%s\"\nOuput: %s\n\n",
+ $string,
+ can_evenly_split_vowels($string) ? "true" : "false"
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a string, determine whether it is possible to split a string into two
+# substrings with an equal number of vowels
+# Takes one argument:
+# * A string to examine
+# Returns:
+# * A value that evaluates as true if it is possible to split the string into
+# two substrings with an equal number of vowels, and a value that evaluates
+# as false if it is not possible to split the string into two substrings with
+# an equal number of vowels; a false value will be returned if there are no
+# vaowels present
+################################################################################
+sub can_evenly_split_vowels{
+
+ my $total = 0;
+
+ # Split the word into individual letters and
+ # count the vowels, case-insensitively
+ foreach my $letter (split('', shift())){
+ $total++
+ if($letter =~ m/[aeiou]/i);
+ }
+
+ # Return true if we had vowels, and an even
+ # number of them
+ return($total && !($total % 2));
+
+}
+
+
+