Guest User

Untitled

a guest
Jan 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. # with inspiration from chapter 21 of The Self-Taught Programmer by Corey Althoff
  2.  
  3. class Stack:
  4. """a stack is a LIFO data structure where the last item put in is the first item taken out"""
  5. def __init__(self):
  6. self.items = []
  7.  
  8. def push(self, item):
  9. """adds a new item to the stack"""
  10. return self.items.append(item)
  11.  
  12. def pop(self):
  13. """removes the last item from the stack"""
  14. return self.items.pop()
  15.  
  16. def size(self):
  17. """returns the size of stack"""
  18. return len(self.items)
  19.  
  20.  
  21. order_ids = [1,2,3,4,5,6,7,8,9]
  22. reversed_order_ids = []
  23. stack = Stack()
  24.  
  25. for i in order_ids:
  26. stack.push(i)
  27.  
  28. for i in range(stack.size()):
  29. reversed_order_ids.append(stack.pop())
  30.  
  31. # should return [9,8,7,6,5,4,3,2,1]
  32. print(reversed_order_ids)
Add Comment
Please, Sign In to add comment