Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
class solution in Clear category for Digit Stack by vit.aborigen
class Stack:
def __init__(self):
self.stack = []
def push(self, value):
self.stack.append(value)
def pop(self):
if self.stack:
return self.stack.pop()
return 0
def peek(self):
if self.stack:
return self.stack[-1]
return 0
def digit_stack(commands):
stack = Stack()
result = 0
for command in commands:
if command == 'POP':
result += stack.pop()
elif command == 'PEEK':
result += stack.peek()
elif command.startswith('PUSH'):
stack.push(int(command.split()[1]))
return result
Dec. 3, 2018