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
52 changes: 51 additions & 1 deletion Lesson_8/1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
"""
1. Закодируйте любую строку из трех слов по алгоритму Хаффмана.
"""
"""
from collections import Counter, deque


def get_haffman_tree(s):
unique_symbols = Counter(s)
sorted_symbols = deque(sorted(unique_symbols.items(), key=lambda item: item[1]))

if len(sorted_symbols) > 1:
while len(sorted_symbols) > 1:
combined_weight = sorted_symbols[0][1] + sorted_symbols[1][1]
combined_node = {0: sorted_symbols.popleft()[0],
1: sorted_symbols.popleft()[0]}

for i, symbol in enumerate(sorted_symbols):
if combined_weight > symbol[1]:
continue
else:
sorted_symbols.insert(i, (combined_node, combined_weight))
break
else:
sorted_symbols.append((combined_node, combined_weight))

else:
combined_weight = sorted_symbols[0][1]
combined_node = {0: sorted_symbols.popleft()[0], 1: None}
sorted_symbols.append((combined_node, combined_weight))

return sorted_symbols[0][0]


code_table = dict()


def get_haffman_code(tree, path=''):
if not isinstance(tree, dict):
code_table[tree] = path
else:
get_haffman_code(tree[0], path=f'{path}0')
get_haffman_code(tree[1], path=f'{path}1')


s = "на доме чемодан"

haffman_tree = get_haffman_tree(s)
get_haffman_code(haffman_tree)

print(code_table)

for i in s:
print(code_table[i], end=' ')
17 changes: 16 additions & 1 deletion Lesson_8/2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,19 @@
2*. Определение количества различных подстрок с использованием хэш-функции.
Пусть дана строка S длиной N, состоящая только из маленьких латинских букв.
Требуется найти количество различных подстрок в этой строке.
"""
"""
import random
import hashlib

n = random.randint(10, 50)
first_ascii_num = 97
last_ascii_num = 122

s = ''.join([chr(random.randint(first_ascii_num, last_ascii_num)) for _ in range(n)])

substrings = set()
for i in range(len(s)):
for j in reversed(range(i + 1, len(s))):
substrings.add(hashlib.sha256(s[i:j].encode('UTF-8')).hexdigest())

print(f'Количество различных подстрок в строке "{s}": {len(substrings)}')