Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Digit Stack by johankor
def digit_stack(commands):
SUM = 0
STACK = []
for c in commands:
if "PUSH" in c:
cc = c.split()
STACK.append(int(cc[1]))
elif c == "POP":
if len(STACK) != 0:
SUM += STACK.pop()
else:
SUM += 0
elif c == "PEEK":
if len(STACK) != 0:
SUM += STACK[-1]
else:
SUM += 0
return SUM if commands else 0
if __name__ == '__main__':
print("Example:")
print(digit_stack(["PUSH 3", "POP", "POP", "PUSH 4", "PEEK",
"PUSH 9", "PUSH 0", "PEEK", "POP", "PUSH 1", "PEEK"]))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert digit_stack(["PUSH 3", "POP", "POP", "PUSH 4", "PEEK",
"PUSH 9", "PUSH 0", "PEEK", "POP", "PUSH 1", "PEEK"]) == 8, "Example"
assert digit_stack(["POP", "POP"]) == 0, "pop, pop, zero"
assert digit_stack(["PUSH 9", "PUSH 9", "POP"]) == 9, "Push the button"
assert digit_stack([]) == 0, "Nothing"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
June 12, 2019
Comments: