diff options
Diffstat (limited to 'challenge-347/sgreen/python')
| -rwxr-xr-x | challenge-347/sgreen/python/ch-1.py | 51 | ||||
| -rwxr-xr-x | challenge-347/sgreen/python/ch-2.py | 27 | ||||
| -rwxr-xr-x | challenge-347/sgreen/python/test.py | 25 |
3 files changed, 103 insertions, 0 deletions
diff --git a/challenge-347/sgreen/python/ch-1.py b/challenge-347/sgreen/python/ch-1.py new file mode 100755 index 0000000000..a0e124c070 --- /dev/null +++ b/challenge-347/sgreen/python/ch-1.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +import re +import sys + +DAYS = [ + "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", + "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", + "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", + "29th", "30th", "31st" +] + +MONTHS = [ + "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", + "Nov", "Dec" +] + +YEARS = list(map(str, range(1900, 2101))) + + +def format_date(input_string: str) -> str: + fields = [ + ("day of month", DAYS, 1), + ("month", MONTHS, 1), + ("year", YEARS, 1900) + ] + + input_list = input_string.split() + output_list = [] + + if len(input_list) != 3: + raise ValueError("Input must contain day, month, and year") + + for i in (range(3)): + name, values, offset = fields[i] + value = input_list[i] + if value not in values: + raise ValueError(f"Invalid {name}: {value}") + index = values.index(value) + offset + output_list.append(f"{index:02d}") + + return "-".join(reversed(output_list)) + + +def main(): + result = format_date(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-347/sgreen/python/ch-2.py b/challenge-347/sgreen/python/ch-2.py new file mode 100755 index 0000000000..776a289e1d --- /dev/null +++ b/challenge-347/sgreen/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def format_phone(input_string: str) -> str: + # Strip all non-digit characters + input_string = re.sub(r'\D', '', input_string) + + parts = [] + while input_string: + # Decide length of next part + l = 2 if len(input_string) == 4 else min(3, len(input_string)) + parts.append(input_string[:l]) + input_string = input_string[l:] + + return '-'.join(parts) + + +def main(): + result = format_phone(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-347/sgreen/python/test.py b/challenge-347/sgreen/python/test.py new file mode 100755 index 0000000000..369b8579bc --- /dev/null +++ b/challenge-347/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.assertEqual(ch_1.format_date('1st Jan 2025'), '2025-01-01') + self.assertEqual(ch_1.format_date('22nd Feb 2025'), '2025-02-22') + self.assertEqual(ch_1.format_date('15th Apr 2025'), '2025-04-15') + self.assertEqual(ch_1.format_date('23rd Oct 2025'), '2025-10-23') + self.assertEqual(ch_1.format_date('31st Dec 2025'), '2025-12-31') + + def test_ch_2(self): + self.assertEqual(ch_2.format_phone('1-23-45-6'), '123-456') + self.assertEqual(ch_2.format_phone('1234'), '12-34') + self.assertEqual(ch_2.format_phone('12 345-6789'), '123-456-789') + self.assertEqual(ch_2.format_phone('123 4567'), '123-45-67') + self.assertEqual(ch_2.format_phone('123 456-78'), '123-456-78') + + +if __name__ == '__main__': + unittest.main() |
