-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
50 lines (45 loc) · 1.14 KB
/
stack.py
File metadata and controls
50 lines (45 loc) · 1.14 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 ArrayStack:
def __init__(self,S):
self.stack = S
def length(self):
return len(self.stack)
def is_empty(self):
return len(self.stack) == 0
def top(self):
if self.is_empty():
print("stack is empty")
else:
return self.stack[-1]
def push(self,item):
self.stack.append(item)
def pop(self):
if self.is_empty():
print( "Stak is empty")
else:
return self.stack.pop()
print(ArrayStack([1,2]).length())
#reversing using stack
rev = ArrayStack([])
s = list("Divya")
for i in s: rev.push(i)
while rev.length()!=0:
print(rev.pop(),end="")
print('\n')
#matching parenthesis
left = list('{([<')
right = list('})]>')
s = list("[{<<(>}]")
par = ArrayStack([])
#for i in s: par.push(i)
for ch in s:
if ch in left:
par.push(ch)
elif ch in right:
idx = right.index(ch)
if par.top() == left[idx]:
par.pop()
else:
print("incorrect parenthessis")
break
if par.is_empty():
print("perfect")