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
61 changes: 61 additions & 0 deletions lesson_2/main1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import math
import sys

def index_to_bytes(value, bits):
n = bits // 8
if bits % 8 != 0:
n += 1
return value.to_bytes(n, 'little')


if not (2 <= len(sys.argv) <= 3):
print('Usage: python', sys.argv[0], 'input_file (output_file)')
exit(-1)

with open(sys.argv[1], 'rb') as file:
data = file.read()

dictionary = {(i,): i for i in range(256)}
sequence = list()

outpath = sys.argv[2] if len(sys.argv) == 3 else sys.argv[1] + '.lzw'
# cond ? a : b
# a if cond else b
# if len(sys.argv) == 3:
# outpath = sys.argv[2]
# else:
# outpath = sys.argv[1] + '.lzw'
with open(outpath, 'wb') as file:
for sym in data:
sequence.append(sym)
key = tuple(sequence)
if key in dictionary:
continue

n = math.ceil(math.log2(len(dictionary)))
index_value = dictionary[key[:-1]]
enc_value = index_to_bytes(index_value, n)
file.write(enc_value)

value = len(dictionary)
dictionary[key] = value
# print(value, '->', key)

sequence = sequence[-1:]

n = math.ceil(math.log2(len(dictionary)))
index_value = dictionary[tuple(sequence)]
enc_value = index_to_bytes(index_value, n)
file.write(enc_value)

# [1, 2, 3]
# [3]

# a
# a[b] -> [a] (ab) -> dict
# b[e] -> [b] (be) -> dict
# e

# a
# ab
# ab[c] -> [ab] (abc) -> dict
45 changes: 45 additions & 0 deletions lesson_2/main1_dec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import sys
import math


if not (2 <= len(sys.argv) <= 3):
print('Usage: python', sys.argv[0], 'input_file (output_file)')
exit(-1)

dictionary = {i: (i,) for i in range(256)}

inpath = sys.argv[1]
outpath = sys.argv[2] if len(sys.argv) > 2 else sys.argv[1][:-4][:sys.argv[1].find('.')] + '_dec' + \
sys.argv[1][:-4][sys.argv[1].find('.'):]

sequence = []

with open(inpath, 'rb') as file_in:
with open(outpath, 'wb') as file_out:
bites = math.ceil(math.log2(len(dictionary)))
bytes = bites // 8
if bytes % 8 == 0:
bytes += 1
data = file_in.read(bytes)
sym = int.from_bytes(data, 'little')
file_out.write(bytearray(dictionary[sym]))
sequence.append(dictionary[sym][0])
sequence.append(sym)
sequence = list(dictionary[sym])
while True:
bites = math.ceil(math.log2(len(dictionary)))
bytes_in_cycle = bites // 8
bytes_in_cycle += 1
sym = file_in.read(bytes_in_cycle)
if not sym:
break
sym = int.from_bytes(sym, 'little')
if sym in dictionary:
file_out.write(bytearray(dictionary[sym]))
sequence.append(dictionary[sym][0])
dictionary[len(dictionary)] = tuple(sequence)
sequence = list(dictionary[sym])
else:
sequence.append(sequence[0])
file_out.write(bytearray(sequence))
dictionary[len(dictionary)] = tuple(sequence)