Building a Stack

In [33]:
class Stack:
    def __init__(self):
        self.items = []  # or, use 'list()'
        
    def is_empty(self):
        return self.items == []
    
    def size(self):
        return len(self.items)
    
    def peek(self):
        # look at last item in our underlying list
        if len(self.items):
            return self.items[-1]
        else:
            return None
        
        """
        Alternative, using exception handling:
        try:
            return self.items[-1]
        except IndexError:
            return None
        """
    
        """
        How negative indexing works:
        return (self.items[len(self.items)-1])  # last item in a list
        return (self.items[len(self.items)-2])  # secont to last
        return (self.items[len(self.items)-3])  # third to last
        return (self.items[len(self.items)-4])  # fourth to last

        return (self.items[-1])  # last item in a list
        return (self.items[-2])  # secont to last
        return (self.items[-3])  # third to last
        return (self.items[-4])  # fourth to last
        """

    def push(self, item):
        self.items.append(item)
        
        # If you wish to prepend items at the start of your list
        # and peek/pop from index 0 do this.
        # Warning: not efficient.
        # self.insert(0, item)
        
    def pop(self):
        return self.items.pop()

Using our Stack

In [34]:
s = Stack()
In [35]:
s.size()
Out[35]:
0
In [36]:
print(s.peek())
None
In [37]:
from random import sample
nums = sample(range(100), k=20)
In [38]:
print(nums)
[51, 52, 88, 4, 9, 47, 84, 81, 45, 42, 33, 48, 12, 46, 37, 72, 39, 21, 43, 49]
In [39]:
for n in nums:
    s.push(n)
In [40]:
s
Out[40]:
<__main__.Stack at 0x10f621090>
In [41]:
s.peek()
Out[41]:
49
In [42]:
for i in range(s.size()):
    print(f"Stack before pop(): {s.peek()}")
    print(f"Popped value: {s.pop()}")
    print(f"Stack after pop(): {s.peek()}")
    print()
Stack before pop(): 49
Popped value: 49
Stack after pop(): 43

Stack before pop(): 43
Popped value: 43
Stack after pop(): 21

Stack before pop(): 21
Popped value: 21
Stack after pop(): 39

Stack before pop(): 39
Popped value: 39
Stack after pop(): 72

Stack before pop(): 72
Popped value: 72
Stack after pop(): 37

Stack before pop(): 37
Popped value: 37
Stack after pop(): 46

Stack before pop(): 46
Popped value: 46
Stack after pop(): 12

Stack before pop(): 12
Popped value: 12
Stack after pop(): 48

Stack before pop(): 48
Popped value: 48
Stack after pop(): 33

Stack before pop(): 33
Popped value: 33
Stack after pop(): 42

Stack before pop(): 42
Popped value: 42
Stack after pop(): 45

Stack before pop(): 45
Popped value: 45
Stack after pop(): 81

Stack before pop(): 81
Popped value: 81
Stack after pop(): 84

Stack before pop(): 84
Popped value: 84
Stack after pop(): 47

Stack before pop(): 47
Popped value: 47
Stack after pop(): 9

Stack before pop(): 9
Popped value: 9
Stack after pop(): 4

Stack before pop(): 4
Popped value: 4
Stack after pop(): 88

Stack before pop(): 88
Popped value: 88
Stack after pop(): 52

Stack before pop(): 52
Popped value: 52
Stack after pop(): 51

Stack before pop(): 51
Popped value: 51
Stack after pop(): None

Transferring to our pythonds9 project

Once transferred to our development Python pythonds9 package, we can now import our new ADT anywhere

In [44]:
from pythonds9.basic.stack import Stack as StackDS9
In [45]:
print(Stack, StackDS9)
<class '__main__.Stack'> <class 'pythonds9.basic.stack.Stack'>
In [46]:
s2 = StackDS9()
In [47]:
for n in nums:
    s2.push(n)
In [48]:
s2.size()
Out[48]:
20