mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-06 21:15:01 -06:00
run length encoding exercise
This commit is contained in:
7
python/run-length-encoding/run_length_encoding.py
Normal file
7
python/run-length-encoding/run_length_encoding.py
Normal file
@@ -0,0 +1,7 @@
|
||||
import re
|
||||
|
||||
def encode(string):
|
||||
return ''.join(map(lambda item : '{}{}'.format(len(item.group(0)), item.group(0)[0]) if len(item.group(0)) > 1 else item.group(0), re.finditer(re.compile(r'(.)\1{0,}'), string)))
|
||||
|
||||
def decode(string):
|
||||
return ''.join([sub[-1] * int(sub[:-1]) if len(sub) >= 2 else sub for sub in re.findall(re.compile(r'(\d*.)'), string)])
|
||||
Reference in New Issue
Block a user