Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # with inspiration from chapter 21 of The Self-Taught Programmer by Corey Althoff
- class Stack:
- """a stack is a LIFO data structure where the last item put in is the first item taken out"""
- def __init__(self):
- self.items = []
- def push(self, item):
- """adds a new item to the stack"""
- return self.items.append(item)
- def pop(self):
- """removes the last item from the stack"""
- return self.items.pop()
- def size(self):
- """returns the size of stack"""
- return len(self.items)
- order_ids = [1,2,3,4,5,6,7,8,9]
- reversed_order_ids = []
- stack = Stack()
- for i in order_ids:
- stack.push(i)
- for i in range(stack.size()):
- reversed_order_ids.append(stack.pop())
- # should return [9,8,7,6,5,4,3,2,1]
- print(reversed_order_ids)
Add Comment
Please, Sign In to add comment