Run-length encoding of a list. Use the result of problem P09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as lists (N E) where N is the number of duplicates of the element E.
import itertools
def run_length_encode(lst):
# could just call "group" from last solution
groups = [list(x[1]) for x in itertools.groupby(lst)]
return [(len(g), g[0]) for g in groups]
Hello niice blog
ReplyDelete