Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #348 (“String Alike” and “Convert Time”)
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here: The Weekly Challenge
The Weekly Challenge for the week of 2025-11-17 through 2025-11-23 is #348. The tasks for challenge #348 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 348-1: String Alike Submitted by: Mohammad Sajid Anwar Write a script to find out whether a given string can be split into two halves of equal lengths, each with the same non-zero number of vowels. Example #1: Input: "textbook" Output: false 1st half: "text" (1 vowel) 2nd half: "book" (2 vowels) Example #2: Input: "book" Output: true 1st half: "bo" (1 vowel) 2nd half: "ok" (1 vowel) Example #3: Input: "AbCdEfGh" Output: true 1st half: "AbCd" (1 vowel) 2nd half: "EfGh" (1 vowel) Example #4: Input: "rhythmmyth" Output: false 1st half: "rhyth" (0 vowel) 2nd half: "mmyth" (0 vowel) Example #5: Input: "UmpireeAudio" Output: false 1st half: "Umpire" (3 vowels) 2nd half: "eAudio" (5 vowels)
Firstly, if the string has odd length, return 'false'. Otherwise, just split the string in-half and count vowels in the halves, then use this logic:
if ( $v1<1 || $v2<1 || $v1 != $v2 ) { return 'false' }
else { return 'true' }
That's assuming that "vowel" means "one of [aeiouAEIOU]". In order to ensure that accented Latin vowels are also counted as being "vowels", I use "NFD" from "Unicode::Normalize" to detach all combining marks from all letters, then lowercase, then delete all codepoints matching [^aeiou], then count remaining characters; if the two halves now have the same non-zero number of vowels, we can split, else we can't.
Robbie Hatley's Perl Solution to The Weekly Challenge 348-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 348-2: Convert Time
Submitted by: Mohammad Sajid Anwar
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
One operation: "Add 15 minutes".
Example 2
Input: $source = "11:55"
$target = "12:15"
Output: 2
Two operations: "Add 15 minutes", "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: "Add 60 minutes", "Add 5 minutes".
To solve this problem, I'll use this procedure:
- Convert both times to minutes ($source -> $s, $target -> $t).
- while ( $t < $s ) {$t += 1440}
- my $ops = 0;
- while ( $t - $s >= 60 ) {$s += 60; ++$ops}
- while ( $t - $s >= 15 ) {$s += 15; ++$ops}
- while ( $t - $s >= 5 ) {$s += 5; ++$ops}
- while ( $t - $s >= 1 ) {$s += 1; ++$ops}
- return $ops
Robbie Hatley's Perl Solution to The Weekly Challenge 348-2
That's it for challenge 348; see you on challenge 349!
Comments
Post a Comment