-
Notifications
You must be signed in to change notification settings - Fork 0
/
DATA STRUCTURES IN PYTHON3
102 lines (80 loc) · 1.69 KB
/
DATA STRUCTURES IN PYTHON3
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
STACKS
REMOVE ADJACENT DUPLICATES USING STACKS
import string as s
stk=[]
n=input("Enter the characters")
for char in n:
if len(stk)==0 and char in s.ascii_lowercase:
stk.append(char)
elif char in s.ascii_lowercase and stk[-1]==char:
stk.pop()
elif char in s.ascii_lowercase and stk[-1]!=char:
stk.append(char)
print("characters with all adjacent duplicates removed are ")
print(''.join(stk))
POSTFIX EVALUATION USING A STACK
stk=[]
n=input('Enter the required postfix expression')
for char in n:
if char.isdigit():
stk.append(char)
else:
if char=='+':
m=stk.pop()
n=stk.pop()
stk.append(str(int(m)+int(n)))
elif char=='-':
m=stk.pop()
n=stk.pop()
stk.append(str(abs(int(m)-int(n))))
elif char=='*':
m=stk.pop()
n=stk.pop()
stk.append(str(int(m)*int(n)))
elif char=='/':
m=stk.pop()
n=stk.pop()
if int(m)>int(n):
stk.append(str(int(m)//int(n)))
else:
stk.append(str(int(n)//int(m)))
print("The value of the above postfix expression is");
print(int(stk[0]))
BALANCING PARENTHESIS USING STACKS
stk=[]
def push(k):
stk.append(k)
def popped():
if isEmpty():
return 0
else:
return stk.pop()
def check(b,r):
if b==')'and r=='(':
print('checked')
if b==']' and r=='[':
print('checked')
if b=='}' and r=='{':
print('checked')
def isEmpty():
return len(stk)==0
n=input("Enter the string to check the required parenthesis")
count=0
m=1
for i in n:
count+=1
if i in '{([':
push(i)
if i in ')}]':
m=popped()
if m==0:
print('Stack underflow')
break
else:
check(i,m)
if m is not 0:
if isEmpty():
print('Balanced Parenthesis')
else:
print('Unbalanced')
# ** if stack underflow occurs then still its unbalanced