-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
48 lines (38 loc) · 953 Bytes
/
stack.py
File metadata and controls
48 lines (38 loc) · 953 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
38
39
40
41
42
43
44
45
46
47
48
class Stack():
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def is_empty(self):
return self.items == []
def peek(self):
if not self.is_empty():
return self.items[-1]
def get_stack(self):
return self.items
#reverse a string using a stack
def reverse_string(stack, input_string):
reversed_str = ""
for i in range(len(input_string)):
stack.push(input_string[i])
while not stack.is_empty():
reversed_str += stack.pop()
return reversed_str
# uncomment to test
# s = Stack()
# print(s.is_empty())
# s.push(1)
# s.push(2)
# s.push(3)
# s.push(4)
# print(s.get_stack())
# print(s.peek())
# s.pop()
# print(s.get_stack())
# print(s.peek())
# print(s.is_empty())
# stack = Stack()
# print("Reversing string 'Sherman'")
# print(reverse_string(stack, 'Sherman'))