Compress the String!

Sort by

recency

|

860 Discussions

|

  • + 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))
    
  • + 0 comments
    from itertools import groupby
    s = input()
    for char, grouped_iter in groupby(s):
        print(f"({len(list(grouped_iter))}, {char})", end = " ")
    
  • + 1 comment

    from itertools import groupby

    input_value = input()

    group_result = groupby(input_value)

    for key,value in group_result:

    value = sorted(list(value))
    print(f"({int(len(value))}, {int(key)})", end=" ")
    
  • + 0 comments

    Core Python code (Without Built-in function)- We will appreciate itertools module some other time :

    string= input()
    
    lst= [element for element in string]
    
    count= 1
    
    prev= lst[0]
    
    for i in range(1, len(lst)):
        if lst[i] == prev:
            count = count + 1
    
        else:
            print(f"({count}, {prev})", end= " ")
            prev= lst[i]
            count = 1
    
    print(f"({count}, {prev})")