-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexe_186_run_length_encoding.py
62 lines (47 loc) · 1.64 KB
/
exe_186_run_length_encoding.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Write a recursive function that implements the run-length compression technique
described in Run-Lenght Decoding Exercise.
Your function will take a list or a string as its only argument.
It should return the run-length compressed list as its only result.
Include a main program that reads a string from the user, compresses it, and displays the run-length encoded result.
Hint: You may want to include a loop inside the body of your recursive function.
"""
# START Definition of FUNCTION
def encodeList(list_to_encode):
# BASE CASE
if list_to_encode == []:
return []
elif len(list_to_encode) == 1:
return [list_to_encode[0], 1]
# RECURSIVE CASES
else:
tmp = []
tmp.append(list_to_encode[0])
tmp.append(1)
i = 1
while list_to_encode[i] == tmp[0]:
tmp[1] += 1
i += 1
if i == (len(list_to_encode)):
break
return tmp + encodeList(list_to_encode[i:len(list_to_encode)])
# END Definition of FUNCTION
# START MAIN PROGRAM
def main():
# LIST TESTING
all_list_to_encode = [
["A", "A", "A", "A", "A", "A", "B", "B",
"B", "B", "B", "C", "C", "C", "C", "D"],
["A", "A", "A", "B", "B", "B", "C", "C", "C", "D", "D"],
["A", "A", "A", "C", "C", "C"],
["A", "C", "C", "C"],
["A"]
]
# Computing and displaying the RESULTS
for list_to_encode in all_list_to_encode:
print("ORIGINAL DECODED LIST -> ", end="")
print(list_to_encode)
print("ENCODED LIST -> ", end="")
print(encodeList(list_to_encode))
if __name__ == "__main__":
main()