diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-11-18 10:14:27 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-18 10:14:27 +0000 |
| commit | 059d536ce44e458dc395f798f2655286840e2c21 (patch) | |
| tree | f7d519cc03bb3264ca14c2b157cbf5109ba3b716 | |
| parent | 2fb2e4d62b6809342b67cb69545ae38b07c6eb3c (diff) | |
| parent | 4fbb9e3cddc115b656a62b60ad992c44998d0bf7 (diff) | |
| download | perlweeklychallenge-club-059d536ce44e458dc395f798f2655286840e2c21.tar.gz perlweeklychallenge-club-059d536ce44e458dc395f798f2655286840e2c21.tar.bz2 perlweeklychallenge-club-059d536ce44e458dc395f798f2655286840e2c21.zip | |
Merge pull request #13051 from mattneleigh/pwc348
new file: challenge-348/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-348/mattneleigh/perl/ch-1.pl | 81 | ||||
| -rwxr-xr-x | challenge-348/mattneleigh/perl/ch-2.pl | 85 |
2 files changed, 166 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..56a3e4098d --- /dev/null +++ b/challenge-348/mattneleigh/perl/ch-2.pl @@ -0,0 +1,85 @@ +#!/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) ]; + + # Convert the times into numbers of minutes since + # 00:00 + $end = ($end->[0] * 60 + $end->[1]); + $start = ($start->[0] * 60 + $start->[1]); + + # If the end appears to be before the start, + # assume that it's meant to be the next day + $end += 1440 + if($end < $start); + + # Calculate the difference between the start and + # end times + $diff = $end - $start; + + # 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); + +} + + + |
