-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2021MS10853_assignment_5.py
314 lines (297 loc) · 9.7 KB
/
2021MS10853_assignment_5.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#We start with and empty Data_list
Data_list=[]
#we define a helper function to check whether given input string is boolean
def isboolean(x):
if x=="True":
return True
elif x=="False":
return False
else:
return -1
#we start with defining a function func_3 that deals with cases having 3 elements in set
#i.e. we deal with cases of form "x"=something
#here something can be integer, boolean or variable
def func_3(line):
#first we run a loop to check if we already have a tuple for "x"
n=len(Data_list)
i=0
#Initially set c to be -1
c=-1
while i<n:
#first, check whether the element of the data list is a tuple or not
if type(Data_list[i])==tuple:
#if its a tuple check whether the first element is x
if Data_list[i][0]==line[0]:
#if we have a tuple for x at ith position that means i index
c=i
break
#if i is found break otherwise continue the loop
i+=1
#now we take 3 cases
#first case if "something" is an integer
if line[2].isdigit():
#now we check if that integer is already present in the list
if int(line[2]) in Data_list:
#if integer is present we find the index of that element
index=Data_list.index(int(line[2]))
else:
#otherwise, we first append that integer and then find the index
Data_list.append(int(line[2]))
index=Data_list.index(int(line[2]))
#second case if "something" is boolean
elif (isboolean(line[2])==True or isboolean(line[2])==False):
#again we check if that's present in list or not and follow similar procedure
if line[2] in Data_list:
index=Data_list.index(isboolean(line[2]))
else:
Data_list.append(isboolean(line[2]))
index=Data_list.index(isboolean(line[2]))
#third case if "something' is a variable eg x=y
#here we need to find the tuple containing y and check for the index mentioned
#then we go to that index element and find value of y
else:
i=0
while i<n:
if type(Data_list[i])==tuple:
if Data_list[i][0]==line[2]:
index=Data_list[i][1]
i+=1
#if c=-1 i.e. initial case which means x is not found in any tuple
#then we append a new tuple (x,index)
if c==-1:
Data_list.append((line[0],index))
#otherwise we change the existing tuple to (x,index)
else:
Data_list[c]=(line[0],index)
#now we define a function func_4 that deals with cases having 4 elements in set
#i.e. we deal with cases of form "x"=(-)/not something
#here something can be integer, boolean or variable
def func_4(line):
#again same process to check whether x is present in a tuple or not
n=len(Data_list)
i=0
c=-1
while i<n:
if type(Data_list[i])==tuple:
if Data_list[i][0]==line[0]:
c=i
break
i+=1
#now we divide into 2 cases that 3rd element is "-" or "not"
if line[2]=="not":
#for "not" we further have 3 cases that sth is integer, boolean or variable
#if its integer we call it a
#we call b=not a
#and follow similar process to check again if b is in list or not
if line[3].isdigit():
a=int(line[3])
b=not(a)
if b in Data_list:
index=Data_list.index(b)
else:
Data_list.append(b)
index=Data_list.index(b)
#similarly for boolean we take 2 cases that sth is True o
elif (isboolean(line[3])==True or isboolean(line[3])==False):
if isboolean(line[3])==True:
#x=False
if False in Data_list:
index=Data_list.index(False)
else:
Data_list.append(False)
index=Data_list.index(False)
else:
#x=True
if True in Data_list:
index=Data_list.index(True)
else:
Data_list.append(True)
index=Data_list.index(True)
else:
#x=not(y)
i=0
ind=-2
while i<n:
if type(Data_list[i])==tuple:
if Data_list[i][0]==line[3]:
ind=Data_list[i][1]
break
i+=1
if ind==-2:
raise Exception('Error')
else:
x=Data_list[ind]
if x.isdigit():
a=int(x)
b=not(a)
if b in Data_list:
index=Data_list.index(b)
else:
Data_list.append(b)
index=Data_list.index(b)
elif (isboolean(x)==True or isboolean(x)==False):
if isboolean(x)==True:
#x=False
if False in Data_list:
index=Data_list.index(False)
else:
Data_list.append(False)
index=Data_list.index(False)
else:
#x=True
if True in Data_list:
index=Data_list.index(True)
else:
Data_list.append(True)
index=Data_list.index(True)
elif line[2]=="-":
if line[3].isdigit():
if ((-1)*int(line[3])) in Data_list:
index=Data_list.index(((-1)*int(line[3])))
else:
Data_list.append(((-1)*int(line[3])))
index=Data_list.index(((-1)*int(line[3])))
else:
i=0
ind=-2
while i<n:
if type(Data_list[i])==tuple:
if Data_list[i][0]==line[3]:
ind=Data_list[i][1]
break
i+=1
if ind==-2:
raise Exception("Error")
else:
x=Data_list[ind]
if ((-1)*x) in Data_list:
index=Data_list.index((-1)*x)
else:
Data_list.append((-1)*x)
index=Data_list.index((-1)*x)
else:
raise "Error"
if c==-1:
Data_list.append((line[0],index))
else:
Data_list[c]=(line[0],index)
def func_5(line):
#x=sth op sth
n=len(Data_list)
if line[2].isdigit():
a=int(line[2])
if a in Data_list:
index=Data_list.index(a)
else:
Data_list.append(a)
elif (isboolean(line[2])==True or isboolean(line[2])==False):
a=isboolean(line[2])
if a in Data_list:
index=Data_list.index(a)
else:
Data_list.append(a)
else:
i=0
while i<n:
if type(Data_list[i])==tuple:
if Data_list[i][0]==line[2]:
index=Data_list[i][1]
break
i+=1
a=Data_list[index]
if a in Data_list:
index=Data_list.index(a)
else:
Data_list.append(a)
if line[4].isdigit():
b=int(line[4])
if b in Data_list:
index=Data_list.index(b)
else:
Data_list.append(b)
elif (isboolean(line[4])==True or isboolean(line[4])==False):
b=isboolean(line[4])
if b in Data_list:
index=Data_list.index(b)
else:
Data_list.append(b)
else:
i=0
while i<n:
if type(Data_list[i])==tuple:
if Data_list[i][0]==line[4]:
index=Data_list[i][1]
break
i+=1
b=Data_list[index]
if b in Data_list:
index=Data_list.index(b)
else:
Data_list.append(b)
if line[3]=="+":
y=a+b
elif line[3]=="-":
y=a-b
elif line[3]=="*":
y=a*b
elif line[3]=="/":
y=a//b
elif line[3]==">":
y=a>b
elif line[3]=="<":
y=a<b
elif line[3]==">=":
y=a>=b
elif line[3]=="<=":
y=a<=b
elif line[3]=="==":
y=a==b
elif line[3]=="!=":
y=a!=b
elif line[3]=="and":
y=(a and b)
elif line[3]=="or":
y=(a or b)
else:
raise Exception("Error")
line_1=[line[0],"=",str(y)]
func_3(line_1)
def func(line):
if len(line)==3:
func_3(line)
#func_3 handles eqn of form x=value/variable
elif len(line)==4:
func_4(line)
#func_4 handles eqn of form x=(-)/not(value/variable)
elif len(line)==5:
func_5(line)
#func_5 handles eqn of form x=sth operator sth
else:
raise "Error"
lines = [] # initalise to empty list
with open('a.txt') as f:
lines = f.readlines() # read all lines into a list of strings
for statement in lines: # each statement is on a separate line
token_list = statement.split() # split a statement into a list of tokens
# now process each statement
func(token_list)
print("The value of variables is found to be")
n=len(Data_list)
i=0
while i<n:
if type(Data_list[i])==tuple:
index=Data_list[i][1]
print(Data_list[i][0],"=",Data_list[index])
i+=1
print("Garbage Values are")
L=[]
n=len(Data_list)
i=0
while i<n:
if type(Data_list[i])==tuple:
index=Data_list[i][1]
L.append(Data_list[i])
L.append(Data_list[index])
i+=1
S=set(Data_list)-set(L)
print(list(S))