diff options
Diffstat (limited to 'challenge-326/sgreen/python')
| -rwxr-xr-x | challenge-326/sgreen/python/ch-1.py | 29 | ||||
| -rwxr-xr-x | challenge-326/sgreen/python/ch-2.py | 28 | ||||
| -rwxr-xr-x | challenge-326/sgreen/python/test.py | 21 |
3 files changed, 78 insertions, 0 deletions
diff --git a/challenge-326/sgreen/python/ch-1.py b/challenge-326/sgreen/python/ch-1.py new file mode 100755 index 0000000000..c05a79a942 --- /dev/null +++ b/challenge-326/sgreen/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +from datetime import date +import re +import sys + + +def day_of_year(input_date: str) -> int: + """ + Calculate the day of the year from a date string in 'YYYY-MM-DD' format. + :param input_date: Date string in 'YYYY-MM-DD' format + :return: Day of the year as an integer + """ + + # Validate the input date format + if not re.match(r'^\d{4}-\d\d?-\d\d?$', input_date): + raise ValueError("Input date must be in 'YYYY-MM-DD' format") + + year, month, day = map(int, input_date.split('-')) + return int((date(year, month, day).strftime('%j'))) + + +def main(): + result = day_of_year(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-326/sgreen/python/ch-2.py b/challenge-326/sgreen/python/ch-2.py new file mode 100755 index 0000000000..153c368500 --- /dev/null +++ b/challenge-326/sgreen/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +import sys + + +def decompressed_list(ints: list) -> list: + """ + Decompress a list of integers where each integer represents the number of times the next integer should be repeated. + :param ints: List of integers + :return: Decompressed list of integers + """ + result = [] + for i in range(0, len(ints), 2): + count = ints[i] + value = ints[i + 1] + result.extend([value] * count) + return result + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = decompressed_list(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-326/sgreen/python/test.py b/challenge-326/sgreen/python/test.py new file mode 100755 index 0000000000..2215ec3e13 --- /dev/null +++ b/challenge-326/sgreen/python/test.py @@ -0,0 +1,21 @@ +#!/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.day_of_year('2025-02-02'), 33) + self.assertEqual(ch_1.day_of_year('2025-04-10'), 100) + self.assertEqual(ch_1.day_of_year('2025-09-07'), 250) + + def test_ch_2(self): + self.assertEqual(ch_2.decompressed_list([1, 3, 2, 4]), [3, 4, 4]) + self.assertEqual(ch_2.decompressed_list([1, 1, 2, 2]), [1, 2, 2]) + self.assertEqual(ch_2.decompressed_list([3, 1, 3, 2]), [1, 1, 1, 2, 2, 2]) + + +if __name__ == '__main__': + unittest.main() |
