Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
small logical use of dictionary solution in Clear category for Digit Stack by rybld2
def digit_stack(commands):
def push_(a):
stack.append(a)
return 0
def pop_(a):
return int(stack.pop()) if stack else 0
def peek_(a):
return int(stack[-1]) if stack else 0
dico = {
"PEEK": peek_,
"PUSH": push_,
"POP": pop_
}
com = [c.split(' ') for c in commands]
com = [[c[0], '0'] if len(c) == 1 else c for c in com]
stack = []
return sum([dico.get(c[0])(c[1]) for c in com])
Oct. 12, 2021