Create a compress function in Python?

后端 未结 19 1023
感动是毒
感动是毒 2021-01-05 03:37

I need to create a function called compress that compresses a string by replacing any repeated letters with a letter and number. My function should return the shortened vers

19条回答
  •  有刺的猬
    2021-01-05 04:34

    x="mississippi"
    res = ""
    count = 0
    while (len(x) > 0):
        count = 1
        res= ""
        for j in range(1, len(x)):
            if x[0]==x[j]:
                count= count + 1
            else:
                res = res + x[j]
        print(x[0], count, end=" ")
        x=res
    

提交回复
热议问题