Compress the String!

  • + 0 comments

    My shortest solution…

    from itertools import *
    print(*list((len(list(g)), int(k)) for k, g in groupby(input())))
    

    A solution without groupby()…

    o = p = []
    for c in map(int, input()):
      if c == p:
        n += 1
        continue
      o += [(n, p)] if p != [] else []
      p = c
      n = 1
    print(*(o + [(n, p)]))