Add leetcode 682 task solution
This commit is contained in:
parent
21c021dfba
commit
c3d922b31f
3 changed files with 46 additions and 0 deletions
46
leetcode/stacks/0682_baseball_game.py
Normal file
46
leetcode/stacks/0682_baseball_game.py
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
# Link: https://leetcode.com/problems/baseball-game/description/
|
||||||
|
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
|
Binary file not shown.
Loading…
Reference in a new issue