-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathstack.py
More file actions
46 lines (32 loc) · 916 Bytes
/
stack.py
File metadata and controls
46 lines (32 loc) · 916 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Implement a stack
"""
push - adds an item to the top of the stack
pop - removes an item from the top of the stack (and returns the value of that item)
size - returns the size of the stack
"""
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def size(self):
return len(self.items)
def peek(self):
# return the top element
if self.size() == 0:
return self.items[0]
def pop(self):
if self.size()==0:
return None
else:
return self.items.pop()
MyStack = Stack()
MyStack.push("Web Page 1")
MyStack.push("Web Page 2")
MyStack.push("Web Page 3")
print (MyStack.items)
MyStack.pop()
MyStack.pop()
print ("Pass" if (MyStack.items[0] == 'Web Page 1') else "Fail")
MyStack.pop()
print ("Pass" if (MyStack.pop() == None) else "Fail")