Tuesday, April 1, 2014

Dictionary Encoding Data compression in python

#Dictionary Encoding
str=input("Enter text to be encoded ")
count=None
s1=str
lst=[]
def find(x,lst):
    for i in range(0,len(lst)):
        if x==lst[i]:
            
            return i
    
    return -1
lst=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
#print(lst)
s=s1[0]
s2=""
for i in range(0,len(s1)-1):
    c=s1[i+1]
    x=s+c
    #print("s=",s,"c=",c,"s+c=",x,"list=",lst)
    if find(x,lst)!=-1:
        s=s+c
        
    else:
        
        print(find(s,lst)+1)
        lst.append(s+c)
        s=c
print(find(s,lst)+1)
input()

No comments:

Post a Comment