algos_and_structures/leetcode/stacks/0682_baseball_game.py

45 lines
1.1 KiB
Python

from typing import List
import pytest
class Solution:
def calPoints(self, operations: List[str]) -> int:
r: List[int] = [] # result
r_l: int = 0
for o_i in range(0, len(operations)):
try:
i = int(operations[o_i])
r.append(i)
r_l += 1
continue
except ValueError:
pass
if operations[o_i] == "+":
r.append(r[r_l - 1] + r[r_l - 2])
r_l += 1
continue
elif operations[o_i] == "D":
r.append(r[r_l - 1] * 2)
r_l += 1
continue
elif operations[o_i] == "C":
r.pop()
r_l -= 1
continue
else:
pass
return sum(r)
@pytest.mark.parametrize(
"input, exp_output",
[
(["5", "2", "C", "D", "+"], 30),
(["5", "-2", "4", "C", "D", "9", "+", "+"], 27),
(["1", "C"], 0),
([], 0),
],
)
def test_solution(input, exp_output):
sol = Solution()
assert sol.calPoints(input) == exp_output