pythonでrun-length encoding
Prologの練習問題サイトにrun-length encodingがあったのでやってみます。
PrologSite
問題はこれ。
1.10 (*) Run-length encoding of a list.
Use the result of problem 1.09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as terms [N,E] where N is the number of duplicates of the element E.Example:
?- encode([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X).
X = [ [4,a],[1,b],[2,c],[2,a],[1,d][4,e] ]
import itertools def encode(seq): return ((len(list(group)), c) for c, group in itertools.groupby(seq))
さすがにこれだけだとおもしろくないので、次の問題も。
1.11 (*) Modified run-length encoding.
Modify the result of problem 1.10 in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as [N,E] terms.Example:
?- encode_modified([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X).
X = [ [4,a],b,[2,c],[2,a],d,[4,e] ]
import itertools def encode(seq): return ((len(list(group)), c) for c, group in itertools.groupby(seq)) def encode_modified(seq): return (x[1] if x[0] == 1 else x for x in encode(seq))
itertools.groupbyは連続する値ごとに区切った値をタプルで取得できます。
itertoolsはほんと強力ですね・・・