Compress the String!

Sort by

recency

|

863 Discussions

|

  • + 0 comments

    from itertools import groupby

    def run_length_encoding(s): encoded_parts = [] for key, group in groupby(s): count = len(list(group)) encoded_parts.append(f'({count}, {key})') return ' '.join(encoded_parts)

    input_string = input() output_string = run_length_encoding(input_string) print(output_string)

  • + 0 comments
    s = input()
    
    for  key, grp in groupby(s):
        print(f"({len(list(grp))}, {key})", end=" ")
    
  • + 0 comments
    import itertools
    print (*((len(list(g)), int(k)) for k,g in itertools.groupby(input())))
    
  • + 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)]))
    
  • + 0 comments
    from itertools import groupby
    result = [f"({len(list(group))}, {char})" for char,group in groupby(input())]
    print (" ".join(result))