23 lines
655 B
Python
23 lines
655 B
Python
from re import sub
|
|
|
|
def encode(text):
|
|
'''
|
|
Doctest:
|
|
>>> encode('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW')
|
|
'12W1B12W3B24W1B14W'
|
|
'''
|
|
return sub(r'(.)\1*', lambda m: str(len(m.group(0))) + m.group(1),
|
|
text)
|
|
|
|
def decode(text):
|
|
'''
|
|
Doctest:
|
|
>>> decode('12W1B12W3B24W1B14W')
|
|
'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
|
|
'''
|
|
return sub(r'(\d+)(\D)', lambda m: m.group(2) * int(m.group(1)),
|
|
text)
|
|
|
|
textin = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
|
|
assert decode(encode(textin)) == textin
|