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
23 changes: 23 additions & 0 deletions Урок 8. Практическое задание/task_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,26 @@

Итог: 6 подстрок
"""
import hashlib

s1 = 'qwerty'
s = s1[:]
l = []
n = len(s)
print(f'Строка: {s1}\nПодстроки:')

while n != 0:
for i in range(1, len(s) + 1):
if s[0:i] != s1 and s[0:i] not in l:
l.append(s[0:i])
print(s[0:i])
n -= 1
s = s[1:]

r = set([hashlib.sha1(el.encode('utf-8')).hexdigest() for el in l])
print(f'Итог: {len(r)} подстрок')
print(r)

del l, s1, s, n
# Писал сам, в итоге немного задваивается при длине строки равной 4, но в других местах такого нет,
# да и задваивание решается проверкой или добавлением в множество.
Copy link
Owner

Choose a reason for hiding this comment

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

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

53 changes: 53 additions & 0 deletions Урок 8. Практическое задание/task_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,56 @@
Результат:
00 11 11 101 010 00 011 011 101 010 00 11 11 1000 1001
"""
from collections import deque, Counter


def haffman_code(s):
c = Counter(s)
d = deque(sorted(c.items(), key=lambda el: el[1]))
if len(d) != 1:
while len(d) > 1:
weight = d[0][1] + d[1][1]
two = {
0: d.popleft()[0],
1: d.popleft()[0]
}

for i in range(len(d)):
if d[i][1] >= weight:
d.insert(i, (two, weight))
break
else:
d.append((two, weight))
else:
weight = d[0][1]
two = {
0: d.popleft()[0],
1: None
}
d.append((two, weight))
return d[0][0]


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


s = 'Удивительно, но работает!'
# s = "beep boop beer!"

res = dict()
tree = haffman_code(s)
rec_haf(tree)
for el in s:
print(res[el], end=' ')
print()
for key, items in res.items():
print(f'{key} - {items}')

# Также писал сам, но пришлось довольно часто заглядывать, в итоге не сильно отошел от примера,
# но разобрался со всем, даже на листочке самостоятельно расписал как
# дерево строится для лучшего понимания
Copy link
Owner

Choose a reason for hiding this comment

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

этим примером мы закрепили многие темы курса
большой большой плюс за собственную реализацию