diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2025-11-17 15:15:27 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2025-11-17 15:15:27 +0000 |
| commit | aa47c05c82932dc7bf14a6ae5892d9fbfd707c01 (patch) | |
| tree | 94616f0bd0dbbf37b90bde066d7b25d0380c95f8 | |
| parent | 51f93bc0962522ed3bc5152f8266cc780c83f190 (diff) | |
| download | perlweeklychallenge-club-aa47c05c82932dc7bf14a6ae5892d9fbfd707c01.tar.gz perlweeklychallenge-club-aa47c05c82932dc7bf14a6ae5892d9fbfd707c01.tar.bz2 perlweeklychallenge-club-aa47c05c82932dc7bf14a6ae5892d9fbfd707c01.zip | |
w348 - Task 1 & 2 (Perl)
| -rwxr-xr-x | challenge-348/perlboy1967/perl/ch1.pl | 42 | ||||
| -rwxr-xr-x | challenge-348/perlboy1967/perl/ch2.pl | 51 |
2 files changed, 93 insertions, 0 deletions
diff --git a/challenge-348/perlboy1967/perl/ch1.pl b/challenge-348/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..9ec38e6739 --- /dev/null +++ b/challenge-348/perlboy1967/perl/ch1.pl @@ -0,0 +1,42 @@ +#!/bin/perl + +=pod + +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-348#TASK1> + +Author: Niels 'PerlBoy' van Dijke + +Task 1: String Alike +Submitted by: Mohammad Sajid Anwar + +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. + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +use boolean; + +sub stringAlike ($str) { + my $len = length($str); + # Must be even length string + return false unless $len % 2 == 0; + # Half the length for two identical long sub-strings + $len >>= 1; + # Get both half and their vowel length + my @p = grep { $_ != 0 } map { length s/[^aeiou]+//gir } unpack("(A$len)(A$len)",$str); + # Compare length and zero length check + return boolean (@p == 2 and $p[0] == $p[1]) +} + +is(stringAlike('textbook'),false,'Example 1'); +is(stringAlike('book'),true,'Example 2'); +is(stringAlike('AbCdEfGh'),true,'Example 3'); +is(stringAlike('rhythmmyth'),false,'Example 4'); +is(stringAlike('UmpireeAudio'),false,'Example 5'); + +done_testing; diff --git a/challenge-348/perlboy1967/perl/ch2.pl b/challenge-348/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..53b83e6eb5 --- /dev/null +++ b/challenge-348/perlboy1967/perl/ch2.pl @@ -0,0 +1,51 @@ +#!/bin/perl + +=pod + +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-348#TASK2> + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Covert 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. + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +sub convertTime ($source,$target) { + my $steps = 0; + $source =~ s#(\d+):(\d+)#60*$1 + $2#e; + $target =~ s#(\d+):(\d+)#60*$1 + $2#e; + $target += 24*60 unless ($target > $source); + while ($source != $target) { + for (qw{60 15 5 1}) { + if ($source + $_ <= $target) { + $source += $_; last; + } + } + $steps++; + } + return $steps; +} + +is(convertTime('02:30','02:45'),1,'Example 1'); +is(convertTime('11:55','12:15'),2,'Example 2'); +is(convertTime('09:00','13:00'),4,'Example 3'); +is(convertTime('23:45','00:30'),3,'Example 4'); +is(convertTime('14:20','15:25'),2,'Example 5'); +is(convertTime('00:00','23:59'),32,'Own Example'); + +done_testing; |
