Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Digit Stack by MartynaDziubalka
def digit_stack(commands):
stack = []
peek = 0
count = 0
for x in commands:
if "PUSH" in x:
stack.append(x[-1])
elif "POP" in x:
if len(stack) != 0:
top = len(stack) - 1
count = count + int(stack[top])
del stack[top]
elif "PEEK" in x:
if len(stack) != 0:
top = len(stack) - 1
peek = int(stack[top])
count = count + peek
return count
if __name__ == '__main__':
#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"
Jan. 29, 2016