forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rle_compression.py
58 lines (51 loc) · 1.49 KB
/
rle_compression.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
"""
Run-length encoding (RLE) is a simple compression algorithm
that gets a stream of data as the input and returns a
sequence of counts of consecutive data values in a row.
When decompressed the data will be fully recovered as RLE
is a lossless data compression.
"""
def encode_rle(input):
"""
Gets a stream of data and compresses it
under a Run-Length Encoding.
:param input: The data to be encoded.
:return: The encoded string.
"""
if not input: return ''
encoded_str = ''
prev_ch = ''
count = 1
for ch in input:
# Check If the subsequent character does not match
if ch != prev_ch:
# Add the count and character
if prev_ch:
encoded_str += str(count) + prev_ch
# Reset the count and set the character
count = 1
prev_ch = ch
else:
# Otherwise increment the counter
count += 1
else:
return encoded_str + (str(count) + prev_ch)
def decode_rle(input):
"""
Gets a stream of data and decompresses it
under a Run-Length Decoding.
:param input: The data to be decoded.
:return: The decoded string.
"""
decode_str = ''
count = ''
for ch in input:
# If not numerical
if not ch.isdigit():
# Expand it for the decoding
decode_str += ch * int(count)
count = ''
else:
# Add it in the counter
count += ch
return decode_str