From c3d922b31fb1afb2e2c09cf8bb66601e90e77fd6 Mon Sep 17 00:00:00 2001 From: pro100ton Date: Fri, 27 Dec 2024 21:38:04 +0300 Subject: [PATCH] Add leetcode 682 task solution --- ...rentheses.py => 0020_valid_parentheses.py} | 0 leetcode/stacks/0682_baseball_game.py | 46 ++++++++++++++++++ ...baseball_game.cpython-310-pytest-8.3.4.pyc | Bin 0 -> 1893 bytes 3 files changed, 46 insertions(+) rename leetcode/stacks/{20_valid_parentheses.py => 0020_valid_parentheses.py} (100%) create mode 100644 leetcode/stacks/0682_baseball_game.py create mode 100644 leetcode/stacks/__pycache__/0682_baseball_game.cpython-310-pytest-8.3.4.pyc 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..193fde0 --- /dev/null +++ b/leetcode/stacks/0682_baseball_game.py @@ -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 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 0000000000000000000000000000000000000000..ef1ecc4fe45e7bfb90a7dc5176cf061539c6e613 GIT binary patch literal 1893 zcmZux&5zqe6rVR9JC2iGsRgxN5Y-B)ALe7TyZuI8D0C4Qw5?RcN2JQQnMshq;yG;?6=K1$#elzd+`S+$+S&0ZfKmM5yeqx0D zVV&hCMCTR`HATY-hm3T=z3IrK1jvYWeXuJu3r3A@!(qt6ZulAD9%oMpXLH(ZitthN zti}BS;lVG!8-0o>MAYn49`V+Ia^mK!WedvNeB~*bL$}T09*I{EF+SOg!LZMVO82a{ z{9x{No^K-%F2^qAAUwg=FgM}%nahTC1McH_feG&w*;o&Y+(bnwWNM)@ffRZy^B!F~ zJV4yUaT|v+Xa;14XU{y1_0Ukk9OiVF+`m6#(-WFjP+`nD%>0Tz^D2Jnq zMGjhwXg0Y3SUVu!kQ|B$#r**HFK4=WNTNse)n#6(FlD)>0FxSb>i<20*Rq${C^dCL`yE!V8E z-@atdSl2`2<50FYUZ0rF|g&9Uy-_PcRQTae@CFn9K~MIM=1BotE{Y9#VXjRC~sf& zHAM^Nr3j+gMN7LBt2mmi@6?;F#R@y1 z$c*p)orzxgcV0Q!#4ed^TXV^pE7n|_uBJ)}sa@O~_K0$FBaWO3oN7#TtDI<|bdpMQ zB0(W$i4>*m6{9kh!nAQYc`zPkdT4L5tU!h`XAlOutem@;?N#oYA&7nhc@w>O#e^?DP;S{2){?p;8+97^0UJ_0kwYy^^LWZM zj^<>??g{jy9Oi?%F@t))tFmFERPLJ=Ruh|A%i&bGuuEyW#