From 04598d33f64a0d967c88622c0628e33913c5f960 Mon Sep 17 00:00:00 2001 From: v-v-d Date: Sun, 25 Aug 2019 22:38:57 +0300 Subject: [PATCH 1/2] task 1 done --- Lesson_8/1.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/Lesson_8/1.py b/Lesson_8/1.py index 1c09ac75..9b973674 100644 --- a/Lesson_8/1.py +++ b/Lesson_8/1.py @@ -1,3 +1,53 @@ """ 1. Закодируйте любую строку из трех слов по алгоритму Хаффмана. -""" \ No newline at end of file +""" +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=' ') From 9b4d635caf02452a2bf2849c5b40fc37ee2279f9 Mon Sep 17 00:00:00 2001 From: v-v-d Date: Fri, 30 Aug 2019 01:23:32 +0300 Subject: [PATCH 2/2] task 2 done --- Lesson_8/2.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Lesson_8/2.py b/Lesson_8/2.py index ab731b29..4f3f489b 100644 --- a/Lesson_8/2.py +++ b/Lesson_8/2.py @@ -2,4 +2,19 @@ 2*. Определение количества различных подстрок с использованием хэш-функции. Пусть дана строка S длиной N, состоящая только из маленьких латинских букв. Требуется найти количество различных подстрок в этой строке. -""" \ No newline at end of file +""" +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)}')