-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimon_says.py
58 lines (47 loc) · 1.64 KB
/
simon_says.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# -*- coding: utf8 -*-
import unittest
# Weekly Challenge October 22, 2024
# Simon Says
# Simon asks you to perform operations on a list of numbers that only he tells you.
# You should ignore all other instructions given.
# Create a function which evaluates a list of commands (written in plain English) if the command begins with Simon says.
# Return the result as an integer.
# Notes
# If no instructions are given by Simon, return 0.
# For the sake of simplicity, there will be no command for dividing.
def simon_says(instructions: list[str]) -> int:
output = 0
for instruction in instructions:
if instruction.startswith("Simon says"):
words = instruction.split(" ")
command = words[2]
if command == "add":
output += int(words[3])
elif command == "subtract":
output -= int(words[3])
elif command == "multiply":
output *= int(words[4])
return output
class SimonSaysTest(unittest.TestCase):
def test_simon_says(self) -> None:
self.assertEqual(
10, simon_says(["Simon says add 4", "Simon says add 6", "Then add 5"])
)
self.assertEqual(
24,
simon_says(
["Susan says add 10", "Simon says add 3", "Simon says multiply by 8"]
),
)
self.assertEqual(
0,
simon_says(
[
"Firstly, add 4",
"Simeon says subtract 100", # Look at the name closely :)
]
),
)
if __name__ == "__main__":
unittest.main()