forked from Testudinate/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.py
More file actions
45 lines (35 loc) · 989 Bytes
/
decode.py
File metadata and controls
45 lines (35 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import heapq
from collections import Counter,namedtuple
class N(namedtuple("N",["left","right"])):
def opt(self,code,acc):
self.left.opt(code, acc +"0")
self.right.opt(code,acc +"1")
class le(namedtuple("le",["char"])):
def opt(self, code, acc):
code[self.char] = acc or "0"
def s_encode(s):
h = []
for ch, fr in Counter(s).items():
h.append((fr,len(h),le(ch)))
heapq.heapify(h)
cnt = len(h)
while len(h) > 1:
fr1, _cnt1, left = heapq.heappop(h)
fr2, _cnt2, right = heapq.heappop(h)
heapq.heappush(h,(fr1 + fr2, cnt, N(left, right)))
cnt += 1
code ={}
if h:
[(_fr, _cnt, root)]=h
code = {}
root.opt(code,"")
return code
def main():
s = input()
code = s_encode(s)
en_coded = "".join(code[ch] for ch in s)
print(len(code), len(en_coded))
for ch in sorted(code):
print("{}: {}".format(ch,code[ch]))
print(en_coded)
main()