Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/Run-length-encoding/Python/run-length-encoding-3.py
Normal file
22
Task/Run-length-encoding/Python/run-length-encoding-3.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue