|
| 1 | +import pandas as pd |
| 2 | +print("One thing is noticed before starting of the program is that it is compulsory to exist an 'input.csv' file\n") |
| 3 | +print("The formate of 'input.csv' file is that it should contains a 'left' header and a 'right' header with values indicate left and right respectively.\n") |
| 4 | +print("Here we can't consider 'equal(=)' sign because it doesn't effect a bit of code with or without its presence.\n") |
| 5 | +print("The formate and input file for this program is as below..") |
| 6 | +a=pd.read_csv("input.csv") |
| 7 | +c=a.shape |
| 8 | +print(a) |
| 9 | +b=[] |
| 10 | +for i in range(c[0]): |
| 11 | + for j in range(i+1,c[0]): |
| 12 | + if(a['right'][i]==a['right'][j]): |
| 13 | + for d in range(c[0]): |
| 14 | + b=b+[(len(a['right'][d]))] |
| 15 | + for z in range(b[d]): |
| 16 | + if(a['right'][d][z]==a['left'][j]): |
| 17 | + x=list(a['right'][d]) |
| 18 | + x[z]=a['left'][i] |
| 19 | + l=''.join(x) |
| 20 | + a['right'][d]=a['right'][d].replace(a['left'][j],a['left'][i]) |
| 21 | + a['left'][j]=a['left'][i] |
| 22 | +df=pd.DataFrame(a) |
| 23 | +df.to_csv('output1.csv',index=False) |
| 24 | +p=pd.read_csv("output1.csv") |
| 25 | +print("After checking and putting the value of common exepression ") |
| 26 | +print(p) |
| 27 | +i=0 |
| 28 | +j=i+1 |
| 29 | +while(j<c[0]): |
| 30 | + if(p['right'][i]==p['right'][j]): |
| 31 | + if(p['left'][i]==p['left'][j]): |
| 32 | + p.drop([j],axis=0,inplace=True) |
| 33 | + i+=2 |
| 34 | + j+=1 |
| 35 | + else: |
| 36 | + i+=1 |
| 37 | + j+=1 |
| 38 | + if(j==c[0]): |
| 39 | + i=i+1 |
| 40 | + j=i+1 |
| 41 | + if(i==c[0]): |
| 42 | + j=c[0] |
| 43 | +print("After elemenating the common expression") |
| 44 | +print(p) |
| 45 | +df=pd.DataFrame(p) |
| 46 | +df.to_csv('output1.csv',index=False) |
| 47 | +p=pd.read_csv("output1.csv") |
| 48 | +c=p.shape |
| 49 | +count=0 |
| 50 | +for i in range(c[0]): |
| 51 | + for j in range(c[0]): |
| 52 | + b=[] |
| 53 | + b=b+[(len(p['right'][j]))] |
| 54 | + for z in range(b[0]): |
| 55 | + if(p['right'][j][z]==p['left'][i]): |
| 56 | + count+=1 |
| 57 | + else: |
| 58 | + count=0 |
| 59 | + if(count==0 and i!=c[0]-1): |
| 60 | + p.drop([i],axis=0,inplace=True) |
| 61 | + df=pd.DataFrame(p) |
| 62 | + df.to_csv('output1.csv',index=False) |
| 63 | + p=pd.read_csv("output1.csv") |
| 64 | + c=p.shape |
| 65 | + else: |
| 66 | + break |
| 67 | +print("After dead code elimination") |
| 68 | +print(p) |
| 69 | +df=pd.DataFrame(p) |
| 70 | +df.to_csv('output1.csv',index=False) |
| 71 | +p=pd.read_csv("output1.csv") |
| 72 | +c=p.shape |
| 73 | +print("The final optimized code is....") |
| 74 | +for i in range(c[0]): |
| 75 | + print(p['left'][i]+"="+p['right'][i]) |
0 commit comments