-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrle.py
More file actions
executable file
·74 lines (51 loc) · 1.85 KB
/
rle.py
File metadata and controls
executable file
·74 lines (51 loc) · 1.85 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
"""Run-length encoding and decoding functions."""
def main():
"""Demo usage of functions."""
rle = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
encoded = encode(rle)
decoded = decode(encoded)
print("Test Vector: " + rle)
# Expected output: 12WB12W3B24WB14W
print("Encoded Result: " + formatOutput(encoded))
# Expected output: WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
print("Decoded Result: " + decoded)
def encode(sequence):
"""Encode a sequence of characters and return the result as a list of tuples (data value and number of observed instances of value).
Keyword arguments:
sequence -- a sequence of characters to encode represented as a string.
"""
count = 1
result = []
for x,item in enumerate(sequence):
if x == 0:
continue
elif item == sequence[x - 1]:
count += 1
else:
result.append((sequence[x - 1], count))
count = 1
result.append((sequence[len(sequence) - 1], count))
return result
def decode(sequence):
"""Decodes the sequence and returns the result as a string.
Keyword arguments:
sequence -- a list of tuples (data value and number of observed instances of value).
"""
result = []
for item in sequence:
result.append(item[0] * item[1])
return "".join(result)
def formatOutput(sequence):
"""Returns a print friendly version of the encoded data.
Keyword arguments:
sequence -- list of tuples (data value and number of observed instances of value).
"""
result = []
for item in sequence:
if (item[1] == 1):
result.append(item[0])
else:
result.append(str(item[1]) + item[0])
return "".join(result)
if __name__ == "__main__":
main()