Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. def compress(uncompressed):
  2.     dict_size = 256
  3.     dictionary = {chr(i): i for i in range(dict_size)}
  4.  
  5.     w = ""
  6.     result = []
  7.     for c in uncompressed:
  8.         wc = w + c
  9.         if wc in dictionary:
  10.             w = wc
  11.         else:
  12.             result.append(dictionary[w])
  13.             dictionary[wc] = dict_size
  14.             dict_size += 1
  15.             w = c
  16.  
  17.     if w:
  18.         result.append(dictionary[w])
  19.     return result
  20.  
  21.  
  22. def decompress(compressed):
  23.     from io import StringIO
  24.  
  25.     dict_size = 256
  26.     dictionary = {i: chr(i) for i in range(dict_size)}
  27.  
  28.     result = StringIO()
  29.     w = chr(compressed.pop(0))
  30.     result.write(w)
  31.     for k in compressed:
  32.         if k in dictionary:
  33.             entry = dictionary[k]
  34.         elif k == dict_size:
  35.             entry = w + w[0]
  36.         else:
  37.             raise ValueError('Bad compressed k: %s' % k)
  38.         result.write(entry)
  39.  
  40.         dictionary[dict_size] = w + entry[0]
  41.         dict_size += 1
  42.  
  43.         w = entry
  44.     return result.getvalue()
  45.  
  46.  
  47. with open('test.json', 'r') as f:
  48.     data = f.read()
  49.  
  50. removal_list = [' ', '\t', '\n']
  51. for s in removal_list:
  52.     data = data.replace(s, '')
  53.     print(data)
  54.  
  55. compressed = compress(data)
  56. print(compressed)
  57. print(compressed.__sizeof__())
  58.  
  59. decompressed = decompress(compressed)
  60. print(decompressed)
  61. print(decompressed.__sizeof__())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement