tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
29
Task/Run-length-encoding/Python/run-length-encoding-1.py
Normal file
29
Task/Run-length-encoding/Python/run-length-encoding-1.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
def encode(input_string):
|
||||
count = 1
|
||||
prev = ''
|
||||
lst = []
|
||||
for character in input_string:
|
||||
if character != prev:
|
||||
if prev:
|
||||
entry = (prev,count)
|
||||
lst.append(entry)
|
||||
#print lst
|
||||
count = 1
|
||||
prev = character
|
||||
else:
|
||||
count += 1
|
||||
else:
|
||||
entry = (character,count)
|
||||
lst.append(entry)
|
||||
return lst
|
||||
|
||||
|
||||
def decode(lst):
|
||||
q = ""
|
||||
for character, count in lst:
|
||||
q += character * count
|
||||
return q
|
||||
|
||||
#Method call
|
||||
encode("aaaaahhhhhhmmmmmmmuiiiiiiiaaaaaa")
|
||||
decode([('a', 5), ('h', 6), ('m', 7), ('u', 1), ('i', 7), ('a', 6)])
|
||||
Loading…
Add table
Add a link
Reference in a new issue