-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
117 lines (93 loc) · 3.01 KB
/
stack.py
File metadata and controls
117 lines (93 loc) · 3.01 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
105
106
107
108
109
110
111
112
113
114
115
116
117
from collections import deque # noqa: F401
from typing import Optional, List
from exceptions import UnderflowError
class Stack:
"""
# Stack
A stack is a linear data structure that stores items in a Last-In/First-Out `(LIFO)`
or First-In/Last-Out `(FILO)` manner.
A new element is added at one end and an element is removed from that end only.
"""
def __init__(self, length: int):
"""
Initialize the stack list with a number pointer.
:param length: the maximum length size of this queue
:type length: int
"""
self.__length, self.__number = length, 0
self.__stack: List[Optional[int]] = [None for _ in range(self.__length)]
def pop(self) -> int:
"""
Deletes the topmost element of the stack.
Time Complexity: `O(1)`
:raises UnderflowError: if the stack is empty, then it's Underflow exception
:return: the value to be popped
:rtype: int
"""
if self.is_empty:
raise UnderflowError("Stack is empty.")
self.__number -= 1
return self.__stack[self.__number]
def push(self, value: int):
"""
Inserts the element at the top of the stack.
Time Complexity: `O(1)`
:raises OverflowError: if the stack is full, then it's Overflow exception
:param value: the value to be pushed
:type value: int
"""
if self.is_full:
raise OverflowError("Stack Overflow.")
self.__stack[self.__number] = value
self.__number += 1
@property
def size(self) -> int:
"""
The number size of stack elements.
:return: the stack size number
:rtype: int
"""
return self.__number
@property
def length(self) -> int:
"""
The maximum length of the queue.
:return: the maximum size number
:rtype: int
"""
return self.__length
@property
def top(self) -> int:
"""
Topmost element of the stack
:raises UnderflowError: If the stack is empty
:return: top integer in stack
:rtype: int
"""
if self.is_empty:
raise UnderflowError("Queue is empty.")
return self.__stack[self.__number - 1]
@property
def is_empty(self) -> bool:
"""
Boolean indicating whether the stack is empty.
:return: flag to check if stack is empty
:rtype: bool
"""
return self.__number == 0
@property
def is_full(self) -> bool:
"""
Boolean indicating whether the stack is full.
:return: flag to check if stack is full
:rtype: bool
"""
return self.__number == self.__length
def __repr__(self) -> str: # pragma: no cover
"""
Representation the stack list.
:return: representation string
:rtype: str
"""
items = (str(elem) for elem in self.__stack[: self.__number])
return f"{' | '.join(items)} <=>"