-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_classin_.py
More file actions
executable file
·50 lines (37 loc) · 1.03 KB
/
stack_classin_.py
File metadata and controls
executable file
·50 lines (37 loc) · 1.03 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
class Stack:
def __init__(self) -> None:
self.item =[] #empty lst # creting object
def is_empty(self):
#return len(self.item)==0
return not self.item
#inside the class functions are the methods
def push(self,items):
self.item.append(items)
def pop(self):
return self.item.pop() #return and remove the item defualt at last or from the right as lifo
def peek(self):
return self.item[-1]
def size(self):
return len(self.item)
def __str__(self) -> str:
return str(self.item)
if __name__ == "__main__":
s= Stack()
''' print(s)
print(s.is_empty())
s.push(3)
s.push("Hello")
s.push(4)
print(s)
print(s.pop())
print(s.peek())
print(s.size())
'''
string ="gninrael nIdekniL htiw tol a nraeL "
reversed_string=""
#reversing the string using stack
for char in string:
s.push(char)
while not s.is_empty():
reversed_string += s.pop()
print(reversed_string)