Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hw2/bebey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

33 changes: 33 additions & 0 deletions hw2/lzw_decoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import math
from sys import argv

if len(argv) == 1:
print('Usage: python ' + argv[0] + ' inp_file [out_file]')
exit(-1)

with open(argv[1], 'rb') as inp_file:
enc_str = inp_file.read()

byte_cnt = 0
dictionary = {i: [i] for i in range(256)}
out = b''

with open(argv[2] if len(argv) == 3 else argv[1][:-4], 'wb') as out_file:
cur_str, cur_sl = dictionary[enc_str[0]], 0
out_file.write(bytes(cur_str))
prev_str = cur_str
for entry in enc_str[1:]:
byte_cnt += 1
cur_sl = cur_sl + entry * 256 ** (byte_cnt - 1)
if byte_cnt < math.ceil(math.log2(len(dictionary) + 1) / 8):
continue
byte_cnt = 0
try:
dictionary[cur_sl]
except KeyError:
dictionary[len(dictionary)] = prev_str + [prev_str[0]]
cur_str, cur_sl = dictionary[cur_sl], 0
out_file.write(bytes(cur_str))
if prev_str + cur_str[:1] not in dictionary.values():
dictionary[len(dictionary)] = prev_str + cur_str[:1]
prev_str = cur_str