Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
33
Task/Run-length-encoding/Python/run-length-encoding-1.py
Normal file
33
Task/Run-length-encoding/Python/run-length-encoding-1.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
def encode(input_string):
|
||||
count = 1
|
||||
prev = None
|
||||
lst = []
|
||||
for character in input_string:
|
||||
if character != prev:
|
||||
if prev:
|
||||
entry = (prev, count)
|
||||
lst.append(entry)
|
||||
count = 1
|
||||
prev = character
|
||||
else:
|
||||
count += 1
|
||||
else:
|
||||
try:
|
||||
entry = (character, count)
|
||||
lst.append(entry)
|
||||
return (lst, 0)
|
||||
except Exception as e:
|
||||
print("Exception encountered {e}".format(e=e))
|
||||
return (e, 1)
|
||||
|
||||
def decode(lst):
|
||||
q = []
|
||||
for character, count in lst:
|
||||
q.append(character * count)
|
||||
return ''.join(q)
|
||||
|
||||
#Method call
|
||||
value = encode("aaaaahhhhhhmmmmmmmuiiiiiiiaaaaaa")
|
||||
if value[1] == 0:
|
||||
print("Encoded value is {}".format(value[0]))
|
||||
decode(value[0])
|
||||
9
Task/Run-length-encoding/Python/run-length-encoding-2.py
Normal file
9
Task/Run-length-encoding/Python/run-length-encoding-2.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
from itertools import groupby
|
||||
def encode(input_string):
|
||||
return [(len(list(g)), k) for k,g in groupby(input_string)]
|
||||
|
||||
def decode(lst):
|
||||
return ''.join(c * n for n,c in lst)
|
||||
|
||||
encode("aaaaahhhhhhmmmmmmmuiiiiiiiaaaaaa")
|
||||
decode([(5, 'a'), (6, 'h'), (7, 'm'), (1, 'u'), (7, 'i'), (6, 'a')])
|
||||
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