Tre Rossi
Tre Rossi

Reputation: 11

how do i get my run length encoding script to still fuction after 10 characters

so I have a program

def encode(s):
    counts = {}
    for c in s:
        if counts.get(c) is None:
            counts[c] = s.count(c)
    return "".join(k+str(v) for k,v in counts.items())

def decode(s):
    return "".join((map(lambda tup:  tup[0] * int(tup[1]), zip(s[0:len(s):2], s[1:len(s):2]))))

the thing is, if i were to run encode('aaaaaaaaaa') which is 10 a's, i get 'a10', which is what i want. but when i use decode('a10'), it just returns 'a'. this boggles my mind and i cant figure out how to fix it.

Upvotes: 0

Views: 44

Answers (1)

norok2
norok2

Reputation: 26886

The issue is that your decode function works with single digit repetition only.

For example, 'a9' will be decoded correctly. With 'a10', what is happening is that it gets to repeat 'a' only once for the 1 in 10. Decoding a20 will cause a to be repeated twice, and the same for a200.


One way to solve the problem is to write a generator that handles multi-char numbers appropriately (of course numbers cannot be encoded), e.g.:

def get_char_len_pair(s):
    ch = None
    for c in s:
        if c.isdigit():
            n += c
        else:
            if ch is not None:
                yield ch, int(n)
            ch = c
            n = ''
    if ch is not None:
        yield ch, int(n)

and use that in your str.join() call:

def decode(s):
    return "".join((map(lambda x: x[0] * x[1], get_char_len_pair(s))))


decode('a20b10')
# 'aaaaaaaaaaaaaaaaaaaabbbbbbbbbb'

Upvotes: 1

Related Questions