简体   繁体   中英

Run Length Encoding in python

i got homework to do "Run Length Encoding" in python and i wrote a code but it is print somthing else that i dont want. it prints just the string(just like he was written) but i want that it prints the string and if threre are any characthers more than one time in this string it will print the character just one time and near it the number of time that she appeard in the string. how can i do this?

For example:

the string : 'lelamfaf"

the result : 'l2ea2mf2

def encode(input_string):
        count = 1
        prev = ''
        lst = []
        for character in input_string:
            if character != prev:
                if prev:
                    entry = (prev, count)
                    lst.append(entry)
                    #print lst
                count = 1
                prev = character
            else:
                count += 1
        else:
            entry = (character, count)
            lst.append(entry)
        return lst    


def decode(lst):
        q = ""
        for character, count in lst:
            q += character * count
        return q    


def main():
        s = 'emanuelshmuel'
        print decode(encode(s))    

if __name__ == "__main__":
        main()

Three remarks:

  1. You should use the existing method str.count for the encode function.
  2. The decode function will print count times a character, not the character and its counter.
  3. Actually the decode(encode(string)) combination is a coding function since you do not retrieve the starting string from the encoding result.

Here is a working code:

def encode(input_string):
    characters = []
    result = ''
    for character in input_string:
        # End loop if all characters were counted
        if set(characters) == set(input_string):
            break
        if character not in characters:
            characters.append(character)
            count = input_string.count(character)
            result += character
            if count > 1:
                result += str(count)
    return result

def main():
        s = 'emanuelshmuel'
        print encode(s)
        assert(encode(s) == 'e3m2anu2l2sh')
        s = 'lelamfaf'
        print encode(s)
        assert(encode(s) == 'l2ea2mf2')

if __name__ == "__main__":
        main()

Came up with this quickly, maybe there's room for optimization (for example, if the strings are too large and there's enough memory, it would be better to use a set of the letters of the original string for look ups rather than the list of characters itself). But, does the job fairly efficiently:

text = 'lelamfaf'
counts = {s:text.count(s) for s in text}

char_lst = []
for l in text:
    if l not in char_lst:
        char_lst.append(l)
        if counts[l] > 1:
            char_lst.append(str(counts[l]))

encoded_str = ''.join(char_lst)
print encoded_str

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM