Skip to content

Instantly share code, notes, and snippets.

@TAM360
Created August 11, 2021 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TAM360/a94ad184ec84e0c0f13f5dd40f8ceb7d to your computer and use it in GitHub Desktop.
Save TAM360/a94ad184ec84e0c0f13f5dd40f8ceb7d to your computer and use it in GitHub Desktop.
basball question
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