diff options
Diffstat (limited to 'challenge-336/sgreen/python')
| -rwxr-xr-x | challenge-336/sgreen/python/ch-1.py | 30 | ||||
| -rwxr-xr-x | challenge-336/sgreen/python/ch-2.py | 42 | ||||
| -rwxr-xr-x | challenge-336/sgreen/python/test.py | 25 |
3 files changed, 97 insertions, 0 deletions
diff --git a/challenge-336/sgreen/python/ch-1.py b/challenge-336/sgreen/python/ch-1.py new file mode 100755 index 0000000000..d6bd5aa0df --- /dev/null +++ b/challenge-336/sgreen/python/ch-1.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +import sys +from collections import Counter + +def equal_group(ints: list) -> bool: + # Calculate the frequency of each integer in the list + freq = Counter(ints).values() + + # If any integer appears only once, it is always false. + if min(freq) == 1: + return False + + # Check if all frequencies are evenly divisible by an integer. + for i in range(2, max(freq) + 1): + if all(f % i == 0 for f in freq): + return True + + return False + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = equal_group(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-336/sgreen/python/ch-2.py b/challenge-336/sgreen/python/ch-2.py new file mode 100755 index 0000000000..ce381c551e --- /dev/null +++ b/challenge-336/sgreen/python/ch-2.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def final_score(scores: list[str]) -> int: + score_stack = [] + + for score in scores: + if score == "C": + # Clear the previous score + if not score_stack: + raise ValueError("No scores to remove for 'C' operation") + score_stack.pop() + elif score == "D": + # Double the previous score + if not score_stack: + raise ValueError("No scores to double for 'D' operation") + score_stack.append(2 * score_stack[-1]) + elif score == "+": + # Sum the previous two scores + if len(score_stack) < 2: + raise ValueError("Not enough scores to sum for '+' operation") + score_stack.append(score_stack[-1] + score_stack[-2]) + elif re.match(r"^-?\d+$", score): + # It's a valid integer score + score_stack.append(int(score)) + else: + # We don't know what score this is + raise ValueError(f"Invalid score entry: {score}") + + return sum(score_stack) + + +def main(): + result = final_score(sys.argv[1:]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-336/sgreen/python/test.py b/challenge-336/sgreen/python/test.py new file mode 100755 index 0000000000..4255288078 --- /dev/null +++ b/challenge-336/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.assertTrue(ch_1.equal_group([1,1,2,2,2,2])) + self.assertFalse(ch_1.equal_group([1,1,1,2,2,2,3,3])) + self.assertTrue(ch_1.equal_group([5,5,5,5,5,5,7,7,7,7,7,7])) + self.assertFalse(ch_1.equal_group([1,2,3,4])) + self.assertTrue(ch_1.equal_group([8,8,9,9,10,10,11,11])) + + def test_ch_2(self): + self.assertEqual(ch_2.final_score(["5","2","C","D","+"]), 30) + self.assertEqual(ch_2.final_score(["5","-2","4","C","D","9","+","+"]), 27) + self.assertEqual(ch_2.final_score(["7","D","D","C","+","3"]), 45) + self.assertEqual(ch_2.final_score(["-5","-10","+","D","C","+"]), -55) + self.assertEqual(ch_2.final_score(["3","6","+","D","C","8","+","D","-2","C","+"]), 128) + + +if __name__ == '__main__': + unittest.main() |
