run length encoding exercise

This commit is contained in:
Xevion
2019-07-17 00:27:24 -05:00
parent 7621525406
commit 2221621fb8
8 changed files with 327 additions and 0 deletions

View 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)])