Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Digit Stack solution in Uncategorized category for Digit Stack by capback250
def digit_stack(text):
LIFO = []
s = 0
for letter in text:
if "PUSH" in letter:
LIFO.append(int(letter[5:]))
if "PEEK" in letter:
if LIFO:
s += int(str(LIFO[-1]))
else:
continue
if "POP" in letter:
if LIFO:
s += LIFO.pop()
else:
continue
return s
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"
Aug. 1, 2015