-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathstack-array-impl.py
41 lines (28 loc) · 914 Bytes
/
stack-array-impl.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
class Stack:
def __init__(self):
self._stack: list = []
def is_empty(self) -> bool:
return len(self._stack) == 0
def push(self, item):
self._stack.append(item)
def pop(self):
if self.is_empty():
raise Exception("Stack is empty")
return self._stack.pop()
def peek(self):
if self.is_empty():
raise Exception("Stack is empty")
# return self._stack[-1] # python way
return self._stack[len(self._stack) - 1]
def size(self) -> int:
return len(self._stack)
def reverse_string(s: str) -> str:
stack: Stack = Stack()
for character in s:
stack.push(character)
result: str = ""
while not stack.is_empty():
result += stack.pop()
return result
string: str = "This string will be reversed ..."
print(reverse_string(string)) # ... desrever eb lliw gnirts sihT