diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-11-21 12:06:10 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-21 12:06:10 +0000 |
| commit | d5a6c887700884456c20707634772d826b400a35 (patch) | |
| tree | b38ed25b092f755e58cd896f79a31add5a48b578 | |
| parent | 70a976d79c7bc42ba97ceff0d21f6b95e19d8941 (diff) | |
| parent | 247ff32dca436dcc434ab4901b0a9de6e2395e4b (diff) | |
| download | perlweeklychallenge-club-d5a6c887700884456c20707634772d826b400a35.tar.gz perlweeklychallenge-club-d5a6c887700884456c20707634772d826b400a35.tar.bz2 perlweeklychallenge-club-d5a6c887700884456c20707634772d826b400a35.zip | |
Merge pull request #13063 from wanderdoc/master
pwc 348 (wanderdoc)
| -rw-r--r-- | challenge-348/wanderdoc/perl/ch-1.pl | 88 | ||||
| -rw-r--r-- | challenge-348/wanderdoc/perl/ch-2.pl | 99 |
2 files changed, 187 insertions, 0 deletions
diff --git a/challenge-348/wanderdoc/perl/ch-1.pl b/challenge-348/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..a31b43c783 --- /dev/null +++ b/challenge-348/wanderdoc/perl/ch-1.pl @@ -0,0 +1,88 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string of even length. +Write a script to find out whether the given string can be split into two halves of equal lengths, each with the same non-zero number of vowels. + +Example 1 + +Input: $str = "textbook" +Output: false + +1st half: "text" (1 vowel) +2nd half: "book" (2 vowels) + + +Example 2 + +Input: $str = "book" +Output: true + +1st half: "bo" (1 vowel) +2nd half: "ok" (1 vowel) + + +Example 3 + +Input: $str = "AbCdEfGh" +Output: true + +1st half: "AbCd" (1 vowel) +2nd half: "EfGh" (1 vowel) + +Example 4 + +Input: $str = "rhythmmyth" +Output: false + + +1st half: "rhyth" (0 vowel) +2nd half: "mmyth" (0 vowel) + + +Example 5 + +Input: $str = "UmpireeAudio" +Output: false + +1st half: "Umpire" (3 vowels) +2nd half: "eAudio" (5 vowels) +=cut + + +use constant { true => 1, false => 0 }; +use Test2::V0 -no_srand => 1; + + +is(string_alike("textbook"), false, 'Example 1'); +is(string_alike("book"), true, 'Example 2'); +is(string_alike("AbCdEfGh"), true, 'Example 3'); +is(string_alike("rhythmmyth"), false, 'Example 4'); +is(string_alike("UmpireeAudio"), false, 'Example 5'); +done_testing(); + +sub string_alike +{ + my $str = $_[0]; + my $length = length($str); + die "Odd length of $str!" if ($length % 2 == 1); + if ( (count_vowels(substr($str, 0, $length/2)) == + count_vowels(substr($str, $length/2))) and + count_vowels(substr($str, 0, $length/2)) > 0 + ) + { + return true; + } + return false; +} + + + +sub count_vowels +{ + my $str = lc $_[0]; + my $count = $str =~ tr/aeiou//; + return $count; +} diff --git a/challenge-348/wanderdoc/perl/ch-2.pl b/challenge-348/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..8c00af66f0 --- /dev/null +++ b/challenge-348/wanderdoc/perl/ch-2.pl @@ -0,0 +1,99 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given two strings, $source and $target, containing time in 24-hour time form. +Write a script to convert the source into target by performing one of the following operations: + +1. Add 1 minute +2. Add 5 minutes +3. Add 15 minutes +4. Add 60 minutes + +Find the total operations needed to get to the target. + +Example 1 +Input: $source = "02:30" + $target = "02:45" +Output: 1 + +Just one operation i.e. "Add 15 minutes". + + +Example 2 + +Input: $source = "11:55" + $target = "12:15" +Output: 2 + +Two operations i.e. "Add 15 minutes" followed by "Add 5 minutes". + +Example 3 + +Input: $source = "09:00" + $target = "13:00" +Output: 4 + +Four operations of "Add 60 minutes". + + +Example 4 + +Input: $source = "23:45" + $target = "00:30" +Output: 3 + +Three operations of "Add 15 minutes". + + +Example 5 + +Input: $source = "14:20" + $target = "15:25" +Output: 2 + +Two operations, one "Add 60 minutes" and one "Add 5 minutes" +=cut + +use Time::Piece; +use Time::Seconds; +use Test2::V0 -no_srand => 1; + + +is(convert_time("02:30", "02:45"), 1, 'Example 1'); +is(convert_time("11:55", "12:15"), 2, 'Example 2'); +is(convert_time("09:00", "13:00"), 4, 'Example 3'); +is(convert_time("23:45", "00:30"), 3, 'Example 4'); +is(convert_time("14:20", "15:25"), 2, 'Example 5'); +done_testing(); + +sub convert_time +{ + my ($source, $target) = @_; + my $dummy_date = '20250101'; + my @hours = map { (split(/:/))[0] } ($source, $target); + $source = Time::Piece->strptime(join(' ', $dummy_date, $source), '%Y%m%d %H:%M'); + $target = Time::Piece->strptime(join(' ', $dummy_date, $target), '%Y%m%d %H:%M'); + if ( $hours[1] < $hours[0] ) + { + $target += ONE_DAY; + } + + my $steps = 0; + my @operations = map { ONE_MINUTE * $_ } (60, 15, 5, 1); + + my $diff = $target - $source; + + OUTER: for my $op ( @operations ) + { + while ( $diff >= $op ) + { + $source += $op; + $steps++; + last OUTER if ($source == $target); + $diff = $target - $source; + } + } + return $steps; +} |
