diff options
| author | Simon Green <mail@simon.green> | 2025-11-23 23:05:41 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2025-11-23 23:05:41 +1000 |
| commit | 8361b08897b9065a52a34b4997cc98dbdfa2dd9c (patch) | |
| tree | bd49a8ef994445f597a3ceaaf3da7ce57afc89ca /challenge-348/sgreen/python | |
| parent | 51f93bc0962522ed3bc5152f8266cc780c83f190 (diff) | |
| download | perlweeklychallenge-club-8361b08897b9065a52a34b4997cc98dbdfa2dd9c.tar.gz perlweeklychallenge-club-8361b08897b9065a52a34b4997cc98dbdfa2dd9c.tar.bz2 perlweeklychallenge-club-8361b08897b9065a52a34b4997cc98dbdfa2dd9c.zip | |
sgreen solutions to challenge 348
Diffstat (limited to 'challenge-348/sgreen/python')
| -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 |
3 files changed, 100 insertions, 0 deletions
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() |
