Created
August 11, 2021 15:26
-
-
Save TAM360/a94ad184ec84e0c0f13f5dd40f8ceb7d to your computer and use it in GitHub Desktop.
basball question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import reduce | |
def baseball_game_func(ops): | |
res = [] | |
for elm in ops: | |
if elm.lstrip('-+').isdigit(): | |
res.append(int(elm)) | |
elif elm == "+": | |
res.append(res[-1] + res[-2]) | |
elif elm == "D": | |
res.append(2 * res[-1]) | |
elif elm == "C": | |
res.pop(-1) | |
return reduce(lambda x, y: x + y, res) if len(res) > 0 else 0 | |
tests = [ | |
["5", "2", "C", "D", "+"], | |
["5", "-2", "4", "C", "D", "9", "+", "+"], | |
[""] | |
] | |
for test in tests: | |
print(baseball_game_func(test)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment