diff options
| author | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-07-22 03:46:43 -0400 |
|---|---|---|
| committer | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-07-22 03:46:43 -0400 |
| commit | be08cae19f652039be25cbae24c0142337386fbe (patch) | |
| tree | 0cab33fcb2a181a8cbda47ed547157d4f0aa0f35 | |
| parent | f59c17748d25a6f16a7e64cc3568ae4190f9f3fb (diff) | |
| download | perlweeklychallenge-club-be08cae19f652039be25cbae24c0142337386fbe.tar.gz perlweeklychallenge-club-be08cae19f652039be25cbae24c0142337386fbe.tar.bz2 perlweeklychallenge-club-be08cae19f652039be25cbae24c0142337386fbe.zip | |
new file: challenge-279/mattneleigh/perl/ch-1.pl
new file: challenge-279/mattneleigh/perl/ch-2.pl
| -rwxr-xr-x | challenge-279/mattneleigh/perl/ch-1.pl | 92 | ||||
| -rwxr-xr-x | challenge-279/mattneleigh/perl/ch-2.pl | 63 |
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)); + +} + + + |
