#!/usr/bin/env python3
from functools import reduce
### 余再帰
class unfoldr:
def __init__ ( self , f, seed) :
self .f = f
self .seed = seed
def __iter__ ( self ) :
return self
def __next__( self ) :
match self .f ( self .seed ) :
case a, b:
self .seed = b
return a
case None :
raise StopIteration
def __reversed__( self ) :
return reversed ( list ( self ) )
def codeInfo( n) :
lst = [ 0 ] + list ( unfoldr( lambda x: None if x == n else ( sum ( [ 2 ** i for i in range ( 1 , 1 + x) ] ) , 1 + x) , 1 ) )
return [ *lst, 1 + lst[ -1 ] ]
# 各要素の発生回数を数える
def countDatum( data) :
def foo( d, elt) :
try :
d[ elt] += 1
except KeyError :
d[ elt] = 1
finally :
return d
# 今回は最大値から降順でソートする
return sorted ( [ [ v, k] for k, v in reduce ( foo, list ( data) , { } ) .items ( ) ] , reverse = True )
def compress( data) :
lst0 = lambda : countDatum( data) # 再利用の為サンクを作る
lst1 = codeInfo( len ( lst0( ) ) - 1 )
lst2 = lambda : zip ( [ i[ 1 ] for i in lst0( ) ] , lst1) # 再利用の為サンクを作る
d = dict ( lst2( ) )
return [ d[ key] for key in data] , { k: v for v, k in lst2( ) }
if __name__ == '__main__' :
# Wikipediaと、DとEにアテられる数値が違うが、そもそもDとEの出現率は同じだ。
# ソートによっては「安定ソート」な為、元の順番を保持しようとする。
# しかし、結果、こちらのエンコード/デコードでも「理論上は」問題がない。
print ( compress( "DAEBCBACBBBC" ) )
IyEvdXNyL2Jpbi9lbnYgcHl0aG9uMwoKZnJvbSBmdW5jdG9vbHMgaW1wb3J0IHJlZHVjZQoKIyMjIOS9meWGjeW4sApjbGFzcyB1bmZvbGRyOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIGYsIHNlZWQpOgogICAgICAgIHNlbGYuZiA9IGYKICAgICAgICBzZWxmLnNlZWQgPSBzZWVkCiAgICBkZWYgX19pdGVyX18oc2VsZik6CiAgICAgICAgcmV0dXJuIHNlbGYKICAgIGRlZiBfX25leHRfXyhzZWxmKToKICAgICAgICBtYXRjaCBzZWxmLmYoc2VsZi5zZWVkKToKICAgICAgICAgICAgY2FzZSBhLCBiOgogICAgICAgICAgICAgICAgc2VsZi5zZWVkID0gYgogICAgICAgICAgICAgICAgcmV0dXJuIGEKICAgICAgICAgICAgY2FzZSBOb25lOgogICAgICAgICAgICAgICAgcmFpc2UgU3RvcEl0ZXJhdGlvbgogICAgZGVmIF9fcmV2ZXJzZWRfXyhzZWxmKToKICAgICAgICByZXR1cm4gcmV2ZXJzZWQobGlzdChzZWxmKSkKCmRlZiBjb2RlSW5mbyhuKToKICAgIGxzdCA9IFswXSArIGxpc3QodW5mb2xkcihsYW1iZGEgeDogTm9uZSBpZiB4ID09IG4gZWxzZSAoc3VtKFsyICoqIGkgZm9yIGkgaW4gcmFuZ2UoMSwgMSArIHgpXSksIDEgKyB4KSwgMSkpCiAgICByZXR1cm4gWypsc3QsIDEgKyBsc3RbLTFdXQoKIyDlkITopoHntKDjga7nmbrnlJ/lm57mlbDjgpLmlbDjgYjjgosKZGVmIGNvdW50RGF0dW0oZGF0YSk6IAogICAgZGVmIGZvbyhkLCBlbHQpOgogICAgICAgIHRyeToKICAgICAgICAgICAgZFtlbHRdICs9IDEKICAgICAgICBleGNlcHQgS2V5RXJyb3I6CiAgICAgICAgICAgIGRbZWx0XSA9IDEKICAgICAgICBmaW5hbGx5OgogICAgICAgICAgICByZXR1cm4gZAogICAgIyDku4rlm57jga/mnIDlpKflgKTjgYvjgonpmY3poIbjgafjgr3jg7zjg4jjgZnjgosKICAgIHJldHVybiBzb3J0ZWQoW1t2LCBrXWZvciBrLCB2IGluIHJlZHVjZShmb28sIGxpc3QoZGF0YSksIHt9KS5pdGVtcygpXSwgcmV2ZXJzZSA9IFRydWUpCgpkZWYgY29tcHJlc3MoZGF0YSk6CiAgICBsc3QwID0gbGFtYmRhOiBjb3VudERhdHVtKGRhdGEpICMg5YaN5Yip55So44Gu54K644K144Oz44Kv44KS5L2c44KLCiAgICBsc3QxID0gY29kZUluZm8obGVuKGxzdDAoKSkgLSAxKQogICAgbHN0MiA9IGxhbWJkYTogemlwKFtpWzFdIGZvciBpIGluIGxzdDAoKV0sIGxzdDEpICMg5YaN5Yip55So44Gu54K644K144Oz44Kv44KS5L2c44KLCiAgICBkID0gZGljdChsc3QyKCkpCiAgICByZXR1cm4gW2Rba2V5XSBmb3Iga2V5IGluIGRhdGFdLCB7azogdiBmb3IgdiwgayBpbiBsc3QyKCl9CgppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgogICAgIyBXaWtpcGVkaWHjgajjgIFE44GoReOBq+OCouODhuOCieOCjOOCi+aVsOWApOOBjOmBleOBhuOBjOOAgeOBneOCguOBneOCgkTjgahF44Gu5Ye654++546H44Gv5ZCM44GY44Gg44CCCiAgICAjIOOCveODvOODiOOBq+OCiOOBo+OBpuOBr+OAjOWuieWumuOCveODvOODiOOAjeOBqueCuuOAgeWFg+OBrumghueVquOCkuS/neaMgeOBl+OCiOOBhuOBqOOBmeOCi+OAggogICAgIyDjgZfjgYvjgZfjgIHntZDmnpzjgIHjgZPjgaHjgonjga7jgqjjg7PjgrPjg7zjg4kv44OH44Kz44O844OJ44Gn44KC44CM55CG6KuW5LiK44Gv44CN5ZWP6aGM44GM44Gq44GE44CCCiAgICBwcmludChjb21wcmVzcygiREFFQkNCQUNCQkJDIikpCg==
compilation info
Traceback (most recent call last):
File "/usr/lib/python3.9/py_compile.py", line 144, in compile
code = loader.source_to_code(source_bytes, dfile or file,
File "<frozen importlib._bootstrap_external>", line 918, in source_to_code
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "./prog.py", line 13
match self.f(self.seed):
^
SyntaxError: invalid syntax
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.9/py_compile.py", line 150, in compile
raise py_exc
py_compile.PyCompileError: File "./prog.py", line 13
match self.f(self.seed):
^
SyntaxError: invalid syntax
stdout