aboutsummaryrefslogtreecommitdiff
path: root/challenge-336/lubos-kolouch/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-336/lubos-kolouch/python/ch-2.py')
-rw-r--r--challenge-336/lubos-kolouch/python/ch-2.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/challenge-336/lubos-kolouch/python/ch-2.py b/challenge-336/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..a7dc021f95
--- /dev/null
+++ b/challenge-336/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,48 @@
+#!/usr/bin/env python3
+"""The Weekly Challenge 336 Task 2: Final Score."""
+import unittest
+
+ScoreList = list[str]
+
+
+def final_score(scores: ScoreList) -> int:
+ """Calculate the total score from a list of operations."""
+ record: list[int] = []
+ for entry in scores:
+ if entry == "C":
+ record.pop()
+ elif entry == "D":
+ record.append(2 * record[-1])
+ elif entry == "+":
+ record.append(record[-1] + record[-2])
+ else:
+ record.append(int(entry))
+ return sum(record)
+
+
+class TestFinalScore(unittest.TestCase):
+ """Unit tests derived from the specification examples."""
+
+ def test_example1(self) -> None:
+ self.assertEqual(final_score(["5", "2", "C", "D", "+"]), 30)
+
+ def test_example2(self) -> None:
+ self.assertEqual(
+ final_score(["5", "-2", "4", "C", "D", "9", "+", "+"]), 27)
+
+ def test_example3(self) -> None:
+ self.assertEqual(final_score(["7", "D", "D", "C", "+", "3"]), 45)
+
+ def test_example4(self) -> None:
+ self.assertEqual(final_score(["-5", "-10", "+", "D", "C", "+"]), -55)
+
+ def test_example5(self) -> None:
+ self.assertEqual(
+ final_score(
+ ["3", "6", "+", "D", "C", "8", "+", "D", "-2", "C", "+"]),
+ 128,
+ )
+
+
+if __name__ == "__main__":
+ unittest.main()