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
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/Python_Algos.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Урок 8. Практическое задание/task_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)} подстрок')
Copy link
Owner

Choose a reason for hiding this comment

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

задание выполнено

46 changes: 46 additions & 0 deletions Урок 8. Практическое задание/task_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Owner

Choose a reason for hiding this comment

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

этим примером мы закрепили многие темы курса