Count the number of characters in a string and displaying the frequency count

Question:

Word Problem:

Create a function that applies a compression technique to a string and returns the resultant compressed string. More formally, a block is a substring of identical symbols that is as long as possible. A block will be represented in compressed form as the length of the block followed by the symbol in that block. The encoding of a string is the representation of each block in the string in the order in which they appear in the string. Given a sequence of characters, write a program to encode them in this format.

Example Input:

print(rleEncode(‘WWWWWWWBWWWWWWWBBW’))

Example Output:

‘W7B1W7B2W1’

So far, I created a counter and a for loop that will loop through every character in the sting, I don’t know how to finish it

def rleEncode(s: str) -> str:
    count = 0
    index = ""
    for i in range(len(s)):
        if s[i]  in index:
            index.append(i)
            count += 1
    return count, index

Asked By: Bran

||

Answers:

I think this prob. what you’re looking for? In pure Python:

from itertools import groupby
s = '...your string....'
ans = ''

for k, g in groupby(s):
    ans += ''.join(k + str(len(list(g))))

    
print(ans)
'W7B1W7B2W1'

Here is another purely, pure function solution

w/o even using Python lib – groupby. As you can see it’s more lines of code… and some logic to determine where to start/stop new counts.

def encode(s: str) -> str:
    count = 1
    res = ''

    # the first character
    res += s[0]

    # loop, skipping last one
    for i, char in enumerate(s[:-1]):
        if s[i] == s[i+1]:              # current == next char.
            count += 1                  # increment count
        else:                           # char changing
            if count >= 1:
                res += str(count)       # convert int to string and add
            res += s[i+1]
            count = 1                   # reset the count
    #  finally the last one
    if count >= 1:                      # if the char is single ONE.
        res += str(count)
    return res

print(encode(s))                        #  W7B1W7B2W
print(encode('ABBA'))                   #  A1B2A1 
Answered By: Daniel Hao
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.