aboutsummaryrefslogtreecommitdiff
path: root/challenge-326/sgreen/python
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2025-06-22 14:30:30 +1000
committerSimon Green <mail@simon.green>2025-06-22 14:30:30 +1000
commit0de218362fdc7aa68ece367dd628e3e0b7e5adc7 (patch)
tree00c2b756b84e702f8ee3072437240cc7ea8908ca /challenge-326/sgreen/python
parent26cfae99bb0a2fdf9710bcc51e8abc8d7ed627f6 (diff)
downloadperlweeklychallenge-club-0de218362fdc7aa68ece367dd628e3e0b7e5adc7.tar.gz
perlweeklychallenge-club-0de218362fdc7aa68ece367dd628e3e0b7e5adc7.tar.bz2
perlweeklychallenge-club-0de218362fdc7aa68ece367dd628e3e0b7e5adc7.zip
sgreen solutions to challenge 326
Diffstat (limited to 'challenge-326/sgreen/python')
-rwxr-xr-xchallenge-326/sgreen/python/ch-1.py29
-rwxr-xr-xchallenge-326/sgreen/python/ch-2.py28
-rwxr-xr-xchallenge-326/sgreen/python/test.py21
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()