Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
5-liner: blob solution in Creative category for Huffman Encode by przemyslaw.daniel
def huffman_encode(s):
for _ in(q:=[(s.count(x),x,{x:''})for x in set(s)])[1:]:
(a,b,c),(d,e,f),*q=sorted(q,key=lambda x:x[:2])
q+=[(a+d,b+e,{k:'01'[k in f]+v for k,v in(c|f).items()})]
return ''.join(q[0][2][x] or '0'for x in s)
Dec. 14, 2022
Comments: