-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStr_util.py
More file actions
33 lines (26 loc) · 732 Bytes
/
Str_util.py
File metadata and controls
33 lines (26 loc) · 732 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
28
29
30
from itertools import groupby
def runLengthEncode(S:str)->list:
"""
RUN-LENGTH-ENCODE"
aaabbbcc->[("a",3"),("b",3),("c",2)]
"""
ret=[]
for key,val in groupby(S):
ret.append((key,int(len(list(val)))))
return ret
def __runLengthEncodeTest():
tests=["aangaweoiaeorwhjoweijgoajogjeoaiwjogjeaowijgo","11111111","1","121212"]
for test in tests:
ret = runLengthEncode(test)
SS=""
for key,val in ret:
SS+=key*val
prevKey=None
for key,_ in ret:
assert not prevKey==key
prevKey=key
assert SS==test
return True
if __name__=="__main__":
if __runLengthEncodeTest():
print("runLengthEncode OK")