Questions about Python. (Compress strings)

Asked 2 years ago, Updated 2 years ago, 12 views

I was studying while reading books I've been trying to fix the code here and there, but it doesn't work I don't know Please help me, masters.

Displays the number of iterations when a
 string is entered and the same character is repeated consecutively.
Input example: aaabbccccca
Output example: a3b2c6a1

To make the test easier, I put it in the code instead of typing it directly. You want to display characters as many times as they are repeated, but you want to use a recursive function.

lttr = []
lttr = "aaabbcccccca"
cnt = 0

def listing():
    if cnt < len(lttr):
        if lttr[cnt] == lttr[cnt + 1]:
            cnt = +1
            if lttr[cnt] != lttr[cnt + 1]:
                print(lttr[cnt] % (d + 1) % cnt)
            listing(cnt)
        else:
            print(lttr[cnt] + "1")
            cnt = +1
            listing(cnt)

I modified it a little so that the return value is not needed in the existing code.

python

2022-09-22 10:44

1 Answers

First of all, we made a wrong recursive function.

def listing(cnt):
    if cnt < len(lttr):
        if lttr[cnt] == lttr[cnt + 1]:
            cnt = +1
            return cnt
            if lttr[cnt] != lttr[cnt + 1]:
                print(lttr[cnt] % (d + 1) % cnt)
            listing(cnt)

Some of the question codes. From return cnt to listing(cnt) here, it can never be done. Because in return cnt, the function returns a value and leaves.


2022-09-22 10:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.