From f8fa6d86f3b7ed5ade5044423c00c3f64968c14d Mon Sep 17 00:00:00 2001 From: Vladimir Date: Mon, 6 Jul 2020 13:54:18 +0500 Subject: [PATCH] DZ8 --- .idea/.gitignore | 2 + .idea/Python_Algos.iml | 12 +++++ .../inspectionProfiles/profiles_settings.xml | 6 +++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ .../task_1.py" | 16 +++++++ .../task_2.py" | 46 +++++++++++++++++++ 8 files changed, 100 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/Python_Algos.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..5c98b428 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/Python_Algos.iml b/.idea/Python_Algos.iml new file mode 100644 index 00000000..7c9d48f0 --- /dev/null +++ b/.idea/Python_Algos.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..a2e120dc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..cd986194 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git "a/\320\243\321\200\320\276\320\272 8. \320\237\321\200\320\260\320\272\321\202\320\270\321\207\320\265\321\201\320\272\320\276\320\265 \320\267\320\260\320\264\320\260\320\275\320\270\320\265/task_1.py" "b/\320\243\321\200\320\276\320\272 8. \320\237\321\200\320\260\320\272\321\202\320\270\321\207\320\265\321\201\320\272\320\276\320\265 \320\267\320\260\320\264\320\260\320\275\320\270\320\265/task_1.py" index 045a8cc9..2e544bee 100644 --- "a/\320\243\321\200\320\276\320\272 8. \320\237\321\200\320\260\320\272\321\202\320\270\321\207\320\265\321\201\320\272\320\276\320\265 \320\267\320\260\320\264\320\260\320\275\320\270\320\265/task_1.py" +++ "b/\320\243\321\200\320\276\320\272 8. \320\237\321\200\320\260\320\272\321\202\320\270\321\207\320\265\321\201\320\272\320\276\320\265 \320\267\320\260\320\264\320\260\320\275\320\270\320\265/task_1.py" @@ -16,3 +16,19 @@ Итог: 6 подстрок """ +from hashlib import sha1 + +STR = 'Python3' +my_set = set() + +for first_sym in range(len(STR)): + if first_sym == 0: + length_sym = len(STR) - 1 + else: + length_sym = len(STR) + for last_sym in range(length_sym): + if last_sym >= first_sym: + substring = sha1(STR[first_sym:last_sym + 1].encode('utf-8')).hexdigest() + my_set.add(substring) + +print(f'В строке - {STR} - {len(my_set)} подстрок') diff --git "a/\320\243\321\200\320\276\320\272 8. \320\237\321\200\320\260\320\272\321\202\320\270\321\207\320\265\321\201\320\272\320\276\320\265 \320\267\320\260\320\264\320\260\320\275\320\270\320\265/task_2.py" "b/\320\243\321\200\320\276\320\272 8. \320\237\321\200\320\260\320\272\321\202\320\270\321\207\320\265\321\201\320\272\320\276\320\265 \320\267\320\260\320\264\320\260\320\275\320\270\320\265/task_2.py" index 96b7bdec..fe5f65d4 100644 --- "a/\320\243\321\200\320\276\320\272 8. \320\237\321\200\320\260\320\272\321\202\320\270\321\207\320\265\321\201\320\272\320\276\320\265 \320\267\320\260\320\264\320\260\320\275\320\270\320\265/task_2.py" +++ "b/\320\243\321\200\320\276\320\272 8. \320\237\321\200\320\260\320\272\321\202\320\270\321\207\320\265\321\201\320\272\320\276\320\265 \320\267\320\260\320\264\320\260\320\275\320\270\320\265/task_2.py" @@ -8,3 +8,49 @@ Результат: 00 11 11 101 010 00 011 011 101 010 00 11 11 1000 1001 """ + +from collections import Counter, deque + +S = "beep boop beer!" +# S = "bbbbbbb" + + +def haffman_tree(string): + cnt = Counter(string) + sort_cnt = deque(sorted(cnt.items(), key=lambda x: x[1])) + if len(sort_cnt) > 1: + while len(sort_cnt) > 1: + weight = sort_cnt[0][1] + sort_cnt[1][1] + union = ({0: sort_cnt.popleft()[0], 1: sort_cnt.popleft()[0]}, weight) + for i, elem in enumerate(sort_cnt): + if elem[1] < weight: + continue + sort_cnt.insert(i, union) + break + else: + sort_cnt.append(union) + else: + # Тут можно немного упростить + return {0: None, 1: sort_cnt[0][0]} + return sort_cnt[0][0] + + +code_list = {} + + +def crawl_tree(sort_cnt, path=''): + if isinstance(sort_cnt, dict): + crawl_tree(sort_cnt[0], path=f'{"0"}{path}') + crawl_tree(sort_cnt[1], path=f'{"1"}{path}') + else: + code_list[sort_cnt] = path + + +def coding(string, res=''): + crawl_tree(haffman_tree(string)) + for sym in string: + res = f'{res} {code_list[sym]}' + return res + + +print('Результат:', coding(S))