diff options
| author | Matthew Neleigh <matthew.neleigh@gmail.com> | 2025-11-17 15:22:36 -0500 |
|---|---|---|
| committer | Matthew Neleigh <matthew.neleigh@gmail.com> | 2025-11-17 15:22:36 -0500 |
| commit | d1da7f260175c7d9f93d757df1bccdfe1e3d6d04 (patch) | |
| tree | c2694734f8a50ac1050ddd799271c7400c672a22 | |
| parent | 51f93bc0962522ed3bc5152f8266cc780c83f190 (diff) | |
| download | perlweeklychallenge-club-d1da7f260175c7d9f93d757df1bccdfe1e3d6d04.tar.gz perlweeklychallenge-club-d1da7f260175c7d9f93d757df1bccdfe1e3d6d04.tar.bz2 perlweeklychallenge-club-d1da7f260175c7d9f93d757df1bccdfe1e3d6d04.zip | |
new file: challenge-348/mattneleigh/perl/ch-1.pl
new file: challenge-348/mattneleigh/perl/ch-2.pl
| -rwxr-xr-x | challenge-348/mattneleigh/perl/ch-1.pl | 81 | ||||
| -rwxr-xr-x | challenge-348/mattneleigh/perl/ch-2.pl | 81 |
2 files changed, 162 insertions, 0 deletions
diff --git a/challenge-348/mattneleigh/perl/ch-1.pl b/challenge-348/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..36672b38c4 --- /dev/null +++ b/challenge-348/mattneleigh/perl/ch-1.pl @@ -0,0 +1,81 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @strings = ( + "textbook", + "book", + "AbCdEfGh", + "rhythmmyth", + "UmpireeAudio" +); + +print("\n"); +foreach my $string (@strings){ + printf( + "Input: \$str = \"%s\"\nOutput: %s\n\n", + $string, + vowels_balanced($string) ? "true" : "false" + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Determine whether the number of vowels in the first half of a string with an +# even number of characters is balanced with the number of vowels in the second +# half; any condition involving zero vowels in either half is considered a +# mismatch, even if both halves are vowel-less +# Takes one argument: +# * The string to examine (e.g. "AbCdEfGh") +# Returns: +# * A value that evaluates as true if the halves of the string have a balanced +# quantity of vowels (as would be the case in the example above) +# * A value that evaluates as false if the halves of the string do NOT have a +# balanced quantity of vowels, or if either half has zero vowels +################################################################################ +sub vowels_balanced{ + # Store the lower-cased input string in the second + # position in the array, since extracting the + # first half therefrom later is simpler + my @halves = ( undef, lc(shift()) ); + + # Extract the first half of the input string, + # modifying the input value to only contain the + # second half + $halves[0] = substr($halves[1], 0, (length($halves[1]) / 2), ""); + + # Loop over the two halves of the string + for my $i (0 .. 1){ + # Count the vowels by splitting the half-string + # into a list of characters, filtering out + # anything that isn't a vowel, then counting the + # members of the filtered list; this count + # replaces the half-string in the array + $halves[$i] = scalar(grep(/[aeiou]/, split("", $halves[$i]))); + + # If there are no vowels in either half, return a + # false value, since any condition involving zero + # vowels is considered a mismatch + return("") + unless($halves[$i]); + } + + # Compare the counts and return + return($halves[0] == $halves[1]); + +} + + + diff --git a/challenge-348/mattneleigh/perl/ch-2.pl b/challenge-348/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..46516f7188 --- /dev/null +++ b/challenge-348/mattneleigh/perl/ch-2.pl @@ -0,0 +1,81 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @time_pairs = ( + [ "02:30", "02:45" ], + [ "11:55", "12:15" ], + [ "09:00", "13:00" ], + [ "23:45", "00:30" ], + [ "14:20", "15:25" ] +); + +print("\n"); +foreach my $time_pair (@time_pairs){ + printf( + "Input: \$source = \"%s\"\n \$target = \"%s\"\nOutput: %d\n\n", + $time_pair->[0], + $time_pair->[1], + ops_to_make_time_difference(@{$time_pair}) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a pair of times in the format "HH:MM" where "HH" ranges from "00" to +# "24", and "MM" ranges from "00" to "59", calculate the number of operations +# needed to construct the difference between the two times, where each +# operation consists of adding minutes in blocks of 60, 15, 5, or 1 +# Takes two arguments: +# * The first time to examine (e.g. "23:45") +# * The second time to examine (e.g. "00:30") +# Returns: +# * The number of operations required to construct the difference in minutes +# between the two, where each operation is an addition of minutes in +# quantities defined above (e.g. 3) +################################################################################ +sub ops_to_make_time_difference{ + my ($start, $end) = @ARG; + + my $diff; + my $ops = 0; + + # Split the times into hours and minutes + $start = [ split(/:/, $start) ]; + $end = [ split(/:/, $end) ]; + + # If the hour of the end time appears to be less + # than that of the start time, assume that it's + # from the next day + $end->[0] += 24 + if($end->[0] < $start->[0]); + + # Convert hours and minutes to just minutes, and + # calculate the difference + $diff = ($end->[0] * 60 + $end->[1]) - ($start->[0] * 60 + $start->[1]); + + # Loop over each unit of minutes, determining how + # many of each are required to equal the difference + for my $unit_size (60, 15, 5, 1){ + $ops += int($diff / $unit_size); + $diff = $diff % $unit_size; + } + + return($ops); + +} + + + |
