Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. def compress(data):
  2.     data=toBinary(data)
  3.  
  4.     Dict = {}
  5.     Dict['0'] = 0
  6.     Dict['1'] = 1
  7.  
  8.     N=2
  9.     i=0
  10.  
  11.     result=elias_generic(len(data))
  12.     while i<len(data):
  13.        
  14.           maxPref=1
  15.           while i+maxPref+1<=len(data):
  16.                 cur_str=data[i:i+maxPref+1]
  17.                 if Dict.get(cur_str)==None:
  18.                     break
  19.                 maxPref+=1
  20.  
  21.           # increase length if we are at the end
  22.           s=''
  23.           while Dict.get(data[i:i + maxPref]+s)==-1:
  24.                 s+='0'
  25.           cur_str = data[i:i + maxPref]+s
  26.  
  27.           # find index of element in lexicon and remove old word
  28.           ind=Dict.get(cur_str)
  29.           Dict[cur_str]=-1
  30.  
  31.           # add new lexicon words
  32.           Dict[cur_str+'0']=ind
  33.           Dict[cur_str+'1']=N+1
  34.  
  35.           N+=1
  36.           i+=len(cur_str)
  37.  
  38.           compressed_len=int(math.log(N-1, 2))+1
  39.           bin_ind="{0:b}".format(ind)
  40.           result+= '0'*(compressed_len-len(bin_ind))+bin_ind
  41.  
  42.     return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement