From 074f1971ac41aedad7dd47db7c29844edf67b619 Mon Sep 17 00:00:00 2001 From: Ty3TPe9P Date: Mon, 20 Mar 2023 23:04:44 +0300 Subject: [PATCH 1/2] Create bebey.md --- hw2/bebey.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 hw2/bebey.md diff --git a/hw2/bebey.md b/hw2/bebey.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/hw2/bebey.md @@ -0,0 +1 @@ + From d45781f8e531c77f9ed47aea844c013b9cd90af7 Mon Sep 17 00:00:00 2001 From: Ty3TPe9P Date: Mon, 20 Mar 2023 23:05:15 +0300 Subject: [PATCH 2/2] Add files via upload --- hw2/lzw_decoder.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 hw2/lzw_decoder.py diff --git a/hw2/lzw_decoder.py b/hw2/lzw_decoder.py new file mode 100644 index 0000000..fd37800 --- /dev/null +++ b/hw2/lzw_decoder.py @@ -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