Skip to content
Open

lab2 #54

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
23 changes: 4 additions & 19 deletions lesson_2/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import math
import sys


def index_to_bytes(value, bits):
n = bits // 8
if bits % 8 != 0:
Expand All @@ -19,12 +20,6 @@ def index_to_bytes(value, bits):
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)
Expand All @@ -39,23 +34,13 @@ def index_to_bytes(value, bits):

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
for s in dictionary:
if dictionary[s] > 255:
print(s, "->", dictionary[s])
76 changes: 76 additions & 0 deletions lesson_2/unmain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import math
import sys


def amount_bytes(bits):
n = bits // 8
if bits % 8 != 0:
n += 1
return n


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

path = sys.argv[1]
outpath = sys.argv[1][:-4] + ".txt"

with open(path, "rb") as file:
data = file.read()

dictionary = {i: [i] for i in range(256)}
pair = list()
num = math.ceil(math.log2(len(dictionary)))
num = amount_bytes(num)
sym_a = b""
first_el = True
byte_num= 0
with open(outpath, 'wb') as file:
while byte_num< len(data):
if first_el:
pair.append(list(dictionary[data[byte_num]]))
first_el = False
num = 2
byte_num+= 1
continue
if num > 0:
sym_a += data[byte_num].to_bytes(1, "little")
num -= 1
byte_num+= 1
continue
pair.append(list())
print(len(dictionary))
for j in dictionary[int.from_bytes(sym_a, "little")]:
pair[1].append(j)
dictionary[len(dictionary)] = list()
sym_a = b""
for i in pair[0]:
for j in dictionary[i]:
dictionary[len(dictionary) - 1].append(j)
dictionary[len(dictionary) - 1].append((dictionary[pair[1][0]][0]))
num = math.ceil(math.log2(len(dictionary)))
num = amount_bytes(num)
for i in pair[0]:
for j in dictionary[i]:
file.write(j.to_bytes(1, "little"))
pair = pair[1:]

pair.append(list())
for j in dictionary[int.from_bytes(sym_a, "little")]:
pair[1].append(j)
dictionary[len(dictionary)] = list()
sym_a = b""
for i in pair[0]:
for j in dictionary[i]:
dictionary[len(dictionary) - 1].append(j)
dictionary[len(dictionary) - 1].append((dictionary[pair[1][0]][0]))
num = math.ceil(math.log2(len(dictionary)))
num = amount_bytes(num)
for i in pair:
for z in i:
for j in dictionary[z]:
file.write(j.to_bytes(1, "little"))
for e in dictionary:
if e > 255:
print(dictionary[e], "->", e)