-
Notifications
You must be signed in to change notification settings - Fork 0
/
rle.py
39 lines (36 loc) · 1.07 KB
/
rle.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
class RLE:
def rle_encode(data):
encoding = ''
previous_character = ''
counter = 1
if not data:
return ''
for character in data:
if character != previous_character:
if previous_character:
encoding += str(counter) + previous_character
counter = 1
previous_character = character
else:
counter += 1
else:
encoding += str(counter) + previous_character
return encoding
def rle_decode(data):
decode = ''
counter = ''
for character in data:
if character.isdigit():
counter += character
else:
decode += character * int(counter)
counter = ''
return decode
# Example Encode:
# encoded_val = RLE.rle_encode('AAAAAAFDDCCCCCCCAEEEEEEEEEEEEEEEEE')
# print(encoded_val)
# 6A1F2D7C1A17E
# Example Decode
# decoded_val = RLE.rle_decode(encoded_val)
# print(decoded_val)
# AAAAAAFDDCCCCCCCAEEEEEEEEEEEEEEEEE