-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEncode_decode.py
34 lines (33 loc) · 902 Bytes
/
Encode_decode.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
def rle_encode(data_input):
encoding = ''
count = 1
if not data_input:
return ''
for ind in range(1, len(data_input)):
if data_input[ind] == data_input[ind - 1]:
count += 1
else :
if count == 1:
encoding += data_input[ind - 1]
else:
encoding += str(count) + data_input[ind - 1]
count = 1
else:
if count == 1:
encoding += data_input[ind]
else:
encoding += str(count) + data_input[ind]
return encoding
def rle_decode(data_output):
decoding = ''
count = ''
for char in data_output:
if char.isdigit():
count += char
else :
if not count:
decoding += char
else:
decoding += int(count) * char
count = ''
return decoding