-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstack.py
More file actions
29 lines (23 loc) · 740 Bytes
/
Copy pathstack.py
File metadata and controls
29 lines (23 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
""" class for the stack """
class Stack(object):
""" class implementing stack """
def __init__(self):
self.stack = []
def push(self, data):
""" push data onto the stack """
self.stack.append(data)
def pop(self):
""" pops the top of the stack """
try:
return self.stack.pop(-1)
except IndexError:
print 'Stack is empty'
def get_top_n(self, num):
""" returns the top - numth element of the stack """
try:
return self.stack[-1 - num]
except IndexError:
print "there is only {} elements!".format(len(self.stack))
def print_stack(self):
""" prints the stack """
print self.stack