diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-11-24 05:15:11 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-24 05:15:11 +0000 |
| commit | a2cb6c4430bd83945c9bd7554a619119ff270c7c (patch) | |
| tree | 5228849d23c1d09aebe5e5448113a81eb73df115 | |
| parent | 85796181ee9a4b22b363d09ccb547532c5ad1401 (diff) | |
| parent | 8361b08897b9065a52a34b4997cc98dbdfa2dd9c (diff) | |
| download | perlweeklychallenge-club-a2cb6c4430bd83945c9bd7554a619119ff270c7c.tar.gz perlweeklychallenge-club-a2cb6c4430bd83945c9bd7554a619119ff270c7c.tar.bz2 perlweeklychallenge-club-a2cb6c4430bd83945c9bd7554a619119ff270c7c.zip | |
Merge pull request #13069 from simongreen-net/master
sgreen solutions to challenge 348
| -rw-r--r-- | challenge-348/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-348/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-348/sgreen/perl/ch-1.pl | 32 | ||||
| -rwxr-xr-x | challenge-348/sgreen/perl/ch-2.pl | 45 | ||||
| -rwxr-xr-x | challenge-348/sgreen/python/ch-1.py | 29 | ||||
| -rwxr-xr-x | challenge-348/sgreen/python/ch-2.py | 46 | ||||
| -rwxr-xr-x | challenge-348/sgreen/python/test.py | 25 |
7 files changed, 180 insertions, 2 deletions
diff --git a/challenge-348/sgreen/README.md b/challenge-348/sgreen/README.md index ee0870fa09..bee5ff287a 100644 --- a/challenge-348/sgreen/README.md +++ b/challenge-348/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 347 +# The Weekly Challenge 348 -Blog: [The one about formatting](https://dev.to/simongreennet/weekly-challenge-the-one-about-formatting-kfp) +Blog: [Alike Time](https://dev.to/simongreennet/the-weekly-challenge-alike-time-41oe) diff --git a/challenge-348/sgreen/blog.txt b/challenge-348/sgreen/blog.txt new file mode 100644 index 0000000000..a15163b48d --- /dev/null +++ b/challenge-348/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/the-weekly-challenge-alike-time-41oe
\ No newline at end of file diff --git a/challenge-348/sgreen/perl/ch-1.pl b/challenge-348/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..a31fd53d87 --- /dev/null +++ b/challenge-348/sgreen/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub count_vowels ($s) { + return $s =~ tr/aeiouAEIOU/aeiouAEIOU/; +} + +sub string_alike ($input_string) { + # Ensure the input string is of even length + die "Input string must have an even length." + if length($input_string) % 2 == 1; + + # If there are no vowels at all, return False + if ( count_vowels($input_string) == 0 ) { + say "False"; + return; + } + + # Split the string into two halves, and count vowels in each half + my $mid = length($input_string) / 2; + my $first_half = substr( $input_string, 0, $mid ); + my $second_half = substr( $input_string, $mid ); + say count_vowels($first_half) == count_vowels($second_half) + ? "True" + : "False"; +} + +string_alike( $ARGV[0] ); diff --git a/challenge-348/sgreen/perl/ch-2.pl b/challenge-348/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..c524aa2300 --- /dev/null +++ b/challenge-348/sgreen/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub time_to_minute ($s) { + if ( $s !~ /^\d{1,2}:\d{2}$/ ) { + die "Invalid time format, should be HH:MM\n"; + } + + my ( $hour, $minute ) = split /:/, $s; + if ( $hour < 0 || $hour > 23 || $minute < 0 || $minute > 59 ) { + die "Invalid time value.\n"; + } + + return $hour * 60 + $minute; +} + +sub main ( $source, $target ) { + # Convert the time to minutes past midnight, and calculate the minutes + # between them + my $duration = time_to_minute($target) - time_to_minute($source); + if ( $duration < 0 ) { + # Adjust for next day + $duration += 24 * 60; + } + + # Possible increments in minutes + my @moves = ( 60, 15, 5, 1 ); + my $count = 0; + + foreach my $move (@moves) { + # Use as many of this move as possible + $count += int( $duration / $move ); + + # The remaining minutes + $duration = $duration % $move; + } + + say $count; +} + +main( $ARGV[0], $ARGV[1] ); diff --git a/challenge-348/sgreen/python/ch-1.py b/challenge-348/sgreen/python/ch-1.py new file mode 100755 index 0000000000..dcc826b062 --- /dev/null +++ b/challenge-348/sgreen/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import sys + + +def count_vowels(s: str) -> int: + vowels = 'aeiouAEIOU' + return sum(1 for char in s if char in vowels) + +def string_alike(input_string: str) -> bool: + # Ensure the input string is of even length + if len(input_string) % 2 == 1: + raise ValueError("Input string must have an even length.") + + if count_vowels(input_string) == 0: + return False + + # Split the string into two halves, and count vowels in each half + first_half = input_string[:len(input_string)//2] + second_half = input_string[len(input_string)//2:] + return count_vowels(first_half) == count_vowels(second_half) + +def main(): + result = string_alike(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-348/sgreen/python/ch-2.py b/challenge-348/sgreen/python/ch-2.py new file mode 100755 index 0000000000..8c92aae94e --- /dev/null +++ b/challenge-348/sgreen/python/ch-2.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +import re +import sys + +def time_to_minute(s: str) -> int: + if not re.match(r'^\d{1,2}:\d{2}$', s): + raise ValueError("Invalid time format, should be HH:MM") + + hour, minute = map(int, s.split(':')) + + if hour < 0 or hour > 23 or minute < 0 or minute > 59: + raise ValueError("Invalid time format") + + return hour * 60 + minute + + +def convert_time(source: str, target: str) -> int: + # Convert the time to minutes past midnight, and calculate the minutes + # between them + duration = time_to_minute(target) - time_to_minute(source) + if duration < 0: + # Adjust for next day + duration += 24 * 60 + + # Possible increments in minutes + moves = [60, 15, 5, 1] + count = 0 + + for move in moves: + # Use as many of this move as possible + count += duration // move + + # The remaining minutes + duration %= move + + return count + + +def main(): + result = convert_time(sys.argv[1], sys.argv[2]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-348/sgreen/python/test.py b/challenge-348/sgreen/python/test.py new file mode 100755 index 0000000000..4a154e489d --- /dev/null +++ b/challenge-348/sgreen/python/test.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + self.assertFalse(ch_1.string_alike("textbook")) + self.assertTrue(ch_1.string_alike("book")) + self.assertTrue(ch_1.string_alike("AbCdEfGh")) + self.assertFalse(ch_1.string_alike("rhythmmyth")) + self.assertFalse(ch_1.string_alike("UmpireeAudio")) + + def test_ch_2(self): + self.assertEqual(ch_2.convert_time("02:30", "02:45"), 1) + self.assertEqual(ch_2.convert_time("11:55", "12:15"), 2) + self.assertEqual(ch_2.convert_time("09:00", "13:00"), 4) + self.assertEqual(ch_2.convert_time("23:45", "00:30"), 3) + self.assertEqual(ch_2.convert_time("14:20", "15:25"), 2) + + +if __name__ == '__main__': + unittest.main() |
