diff --git a/leetcode/stacks/20_valid_parentheses.py b/leetcode/stacks/0020_valid_parentheses.py similarity index 100% rename from leetcode/stacks/20_valid_parentheses.py rename to leetcode/stacks/0020_valid_parentheses.py diff --git a/leetcode/stacks/0682_baseball_game.py b/leetcode/stacks/0682_baseball_game.py new file mode 100644 index 0000000..af4e336 --- /dev/null +++ b/leetcode/stacks/0682_baseball_game.py @@ -0,0 +1,45 @@ +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 diff --git a/leetcode/stacks/__pycache__/0682_baseball_game.cpython-310-pytest-8.3.4.pyc b/leetcode/stacks/__pycache__/0682_baseball_game.cpython-310-pytest-8.3.4.pyc new file mode 100644 index 0000000..ef1ecc4 Binary files /dev/null and b/leetcode/stacks/__pycache__/0682_baseball_game.cpython-310-pytest-8.3.4.pyc differ