forked from codebasics/data-structures-algorithms-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_string.py
More file actions
37 lines (25 loc) · 691 Bytes
/
reverse_string.py
File metadata and controls
37 lines (25 loc) · 691 Bytes
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
from collections import deque
class Stack:
def __init__(self):
self.container = deque()
def push(self, val):
self.container.append(val)
def pop(self):
return self.container.pop()
def peek(self):
return self.container[-1]
def is_empty(self):
return len(self.container) == 0
def size(self):
return len(self.container)
def reverse_string(s):
stack = Stack()
for ch in s:
stack.push(ch)
rstr = ''
while stack.size()!=0:
rstr += stack.pop()
return rstr
if __name__ == '__main__':
print(reverse_string("We will conquere COVI-19"))
print(reverse_string("I am the king"))