-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrle.py
More file actions
27 lines (26 loc) · 673 Bytes
/
Copy pathrle.py
File metadata and controls
27 lines (26 loc) · 673 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def rle(string):
""" Convert each multiple
char in a row by the number of
occurence follow by the char.
Recursively
Output : print
"""
if len(string) == 0:
print ("")
return ""
curLetter = string[0]
count = 0
while string[0] == curLetter:
count += 1
string = string.replace(curLetter, "", 1)
if len(string) == 0:
break
if count == 1:
print(curLetter, end="")
else:
print (str(count) + curLetter, end="")
if len(string) > 1:
rle(string)
else:
print(string)
rle("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB")