-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathsolution_29.py
36 lines (26 loc) · 1011 Bytes
/
solution_29.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def coding_problem_29(rle):
"""
Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated
successive characters as a single count and character. Implement run-length encoding and decoding. You can assume
the string to be encoded have no digits and consists solely of alphabetic characters. You can assume the string to
be decoded is valid.
Examples:
>>> coding_problem_29('AAAABBBCCDAA')
'4A3B2C1D2A'
>>> coding_problem_29('4A3B2C1D2A')
'AAAABBBCCDAA'
"""
if rle.isalpha(): # no numbers, encode
encoded = ''
while rle:
idx = 0
while idx < len(rle) and rle[0] == rle[idx]:
idx += 1
encoded += str(idx) + rle[0]
rle = rle[idx:]
return encoded
else: # decode
return ''.join(c * int(n) for n, c in zip(rle[::2], rle[1::2]))
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)