-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_da.py
145 lines (112 loc) · 3.43 KB
/
stack_da.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Name: Haris Hambasic
# OSU Email: [email protected]
# Course: CS261 - Data Structures
# Assignment: 3
# Due Date: 7 February 2022
# Description: Implementation of a stack ADT via a dynamic array data structure
from dynamic_array import *
class StackException(Exception):
"""
Custom exception to be used by Stack class
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
pass
class Stack:
def __init__(self):
"""
Init new stack based on Dynamic Array
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
self._da = DynamicArray()
def __str__(self) -> str:
"""
Return content of stack in human-readable form
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
out = "STACK: " + str(self._da.length()) + " elements. ["
out += ', '.join([str(self._da[i]) for i in range(self._da.length())])
return out + ']'
def is_empty(self) -> bool:
"""
Return True is the stack is empty, False otherwise
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
return self._da.is_empty()
def size(self) -> int:
"""
Return number of elements currently in the stack
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
return self._da.length()
# -----------------------------------------------------------------------
def push(self, value: object) -> None:
"""
Adds a new element to the top of the stack
Args:
value (object): the value to add
Return:
None
"""
self._da.append(value)
return None
def pop(self) -> object:
"""
Removes and returns the top element of the stack
Args:
n/a
Return:
1) The top element of the stack
2) StackException: if there are no elements in the stack
"""
index = self._da.length() - 1
if index < 0:
raise StackException
popped_value = self._da.get_at_index(index)
self._da.remove_at_index(index)
return popped_value
def top(self) -> object:
"""
Returns the top element of the stack without removing it
Args:
n/a
Return:
1) The top element of the stack
2) StackException: if the stack is empty
"""
if self.is_empty():
raise StackException
top_item = self._da.get_at_index(self._da.length() - 1)
return top_item
# ------------------- BASIC TESTING -----------------------------------------
if __name__ == "__main__":
print("\n# push example 1")
s = Stack()
print(s)
for value in [1, 2, 3, 4, 5]:
s.push(value)
print(s)
print("\n# pop example 1")
s = Stack()
try:
print(s.pop())
except Exception as e:
print("Exception:", type(e))
for value in [1, 2, 3, 4, 5]:
s.push(value)
for i in range(6):
try:
print(s.pop())
except Exception as e:
print("Exception:", type(e))
print("\n# top example 1")
s = Stack()
try:
s.top()
except Exception as e:
print("No elements in stack", type(e))
s.push(10)
s.push(20)
print(s)
print(s.top())
print(s.top())
print(s)