-
Notifications
You must be signed in to change notification settings - Fork 0
/
682-baseball-game.py
35 lines (31 loc) · 1.04 KB
/
682-baseball-game.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
from typing import List
class Solution:
@classmethod
def calPoints(self, operations: List[str]) -> int:
score = []
for char in operations:
try:
digit = int(char)
score.append(digit)
except:
# not a digit
if char == '+':
a = score[-1]
b = score[-2]
score.append(a + b)
if char == 'D':
score.append(score[-1] * 2)
if char == 'C':
score.pop()
return sum(score)
# Explanation:
# "5" - Add 5 to the record, record is now [5].
# "2" - Add 2 to the record, record is now [5, 2].
# "C" - Invalidate and remove the previous score, record is now [5].
# "D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
# "+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
# The total sum is 5 + 10 + 15 = 30.
ops = ["5", "2", "C", "D", "+"]
result = Solution.calPoints(ops)
assert result, 30
print(ops, result)