-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stack.py
75 lines (64 loc) · 2.31 KB
/
Stack.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
class Stack:
"""Class to represent a sequential static stack in Python3"""
def __init__(self, maximum):
self.max = maximum # The maximum stack size
self._stack = [None] * maximum # Empty stack
self._size = 0 # The size of stack
@property
def max(self):
"""Getters to max stack size"""
return self._max
@max.setter
def max(self, maximum):
"""Setters to max stack size"""
if isinstance(maximum, int):
self._max = maximum
else:
raise Exception("The type must be an integer")
def push(self, elem):
"""Adds an element at the top of a stack"""
if self._size == self.max:
raise Exception("Full stack")
self._stack[self._size] = elem
self._size += 1
def pop(self):
"""Removes the topmost element of a stack"""
if self._size == 0:
raise Exception("Empty stack")
elem, self._stack[self._size - 1] = self._stack[self._size - 1], None
self._size -= 1
return elem
def top(self):
"""Returns the element on the top of the stack but does not remove it"""
if self._size == 0:
raise Exception("Empty stack")
return self._stack[self._size - 1]
def empty(self):
"""Returns true if the stack is empty, otherwise, it returns false"""
if self._size == 0:
return True
return False
def length(self):
"""Returns the size of stack"""
return self._size
def __del__(self):
"""Destructor method"""
def __str__(self):
"""Method for representing the stack, excluding NoneType objects (user)"""
rep = "\033[1;34m" + "top -> " + "\033[0;0m"
if self._size == 0:
rep += "None"
return rep
for i in range(self._size - 1, -1, -1):
if self._stack[i] == None:
pass
elif i == self._size - 1:
rep += f"{str(self._stack[i]).rjust(2)}"
elif i == 0:
rep += f"\n{str(self._stack[i]).rjust(10)}"
else:
rep += f"\n{str(self._stack[i]).rjust(10)}"
return rep
def __repr__(self):
"""Method for representing the stack, excluding NoneType objects (developer)"""
return str(self)