-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
104 lines (75 loc) · 2.66 KB
/
stack.py
File metadata and controls
104 lines (75 loc) · 2.66 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 21 10:52:00 2021
@author: Ahmad Almakhamreh
"""
"""
all methods return & print the appropiate values
print statements are commented. you can always uncomment them to track the operations
"""
class Stack:
def __init__(self):
self.__mystack = []
"""
the leading underscores are simply to indicate that the attribute is inteded to be private and should not be accessed outside the class
"""
def push(self,item):
"""This method accepts an item as a parameter and appends it to the end of the List. prints the added item.
appending to the end of a list happens in constant time, so the runtime for this method is O(1)
"""
self.__mystack.append(item)
# print(f"push: ---> {item}")
def pop(self):
"""This method removes, return, and prints the last item in the list, which is the top item of the Stack.
removing from the end of a list (top) also happens in constant time, so the runtime for this method is O(1)
"""
if self.__mystack:
# print(f"pop: ---> {self.__mystack[-1]}" )
return self.__mystack.pop()
else:
print("cannot pop, stack is empty")
def peek(self):
"""This method prints and returns the last item in the list (top of the stack)
this method runs in constant time O(1), because indexing into a list is done in constant time. (Random access)
"""
if self.__mystack:
print(f"peek: ---> {self.__mystack[-1]} ")
return self.__mystack[-1]
else:
print("cannot peek, stack is empty")
def size(self):
"""This method prints & retuns the length of the list that is representing the Stack.
this method runs in constant time O(1), because finding the length of a list also happens in constant time.
"""
print(f"size: {len(self.__mystack)}")
return len(self.__mystack)
def isEmpty(self):
"""This method retuns a boolean value describing whether or not the Stack is empty.
testing for equality happens in constant time.
"""
print(f"isEmpty: ---> {self.__mystack == []}")
return not(self.__mystack)
# can also use
# return self.__mystack == []
def showItems(self):
"""this method prints the items in the list"""
print(f"{self.__mystack} <---- TOP")
def main():
"""
Test cases
"""
stack = Stack()
stack.push(5)
stack.push("ahmed")
stack.push(7.0)
stack.showItems()
stack.push('A')
stack.showItems()
stack.pop()
stack.pop()
stack.pop()
stack.peek()
stack.pop()
stack.pop()
stack.isEmpty()
if __name__ == "__main__" : main()