Skip to content

Алгоритм кода Хафмана для строк и файлов#16

Open
EStarikov wants to merge 1 commit intomainfrom
huffman_code
Open

Алгоритм кода Хафмана для строк и файлов#16
EStarikov wants to merge 1 commit intomainfrom
huffman_code

Conversation

@EStarikov
Copy link
Owner

No description provided.

@EStarikov EStarikov requested a review from chernishev November 23, 2025 17:40
else:
d[char] = 1
leaves = [Leaf(char, freq) for char, freq in d.items()]
leaves.sort(key=lambda x: (x.freq, -ord(x.ch)))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Достаточно просто
leaves.sort(key=lambda x: x.freq)

Comment on lines +31 to +37
i = 0
while i < len(leaves):
if leaves[i].freq < parent.freq:
i += 1
else:
break
leaves.insert(i, parent)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Заменить это обычным добавлением в список и его сортировкой sort. Так делать не надо, тк у тебя тут получается линейное время сортировки в теории, да и просто громоздко.

leaves.insert(i, parent)
code = {}
leaves[0].walk(code, "")
t = ""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

содержательные имена переменных.

code = {}
leaves[0].walk(code, "")
t = ""
for x in s:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

содержательные имена переменных.



def decode(t, code):
tt = ""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

содержательные имена переменных.

Comment on lines +58 to +70
def file_encode(filename1, filename2):
try:
with open(filename1, 'r') as f:
s = f.read()
t, code = encode(s)
except UnicodeDecodeError:
with open(filename1, 'rb') as f:
binary_data = f.read()
s = binary_data.decode('utf-8')
t, code = encode(s)
with open(filename2, 'w') as f:
f.write(t)
return filename2, code

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

в данной функции необходимо записывать в файл и таблицу.

Comment on lines +73 to +79
def file_decode(filename1, filename2, code):
with open(filename1, 'r') as f:
s = f.read()
t = decode(s, code)
with open(filename2, 'w') as f:
f.write(t)
return filename2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Эта функция соответсвенно должна считывать и обрабатывать из файла таблицу, а не спрашивать её у пользователя.
весь смысл в том, чтобы можно было вызвать последовательно file_encode() и file_decode() без передачи между ними вспомогательной информации, кроме как названия файлов.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants