-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrle.py
More file actions
executable file
·33 lines (27 loc) · 730 Bytes
/
rle.py
File metadata and controls
executable file
·33 lines (27 loc) · 730 Bytes
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
def RLE(input_string):
if not input_string:
return ""
encoded_string = ""
count = 1
prev_char = input_string[0]
for char in input_string[1:]:
if char == prev_char:
count += 1
else:
encoded_string += str(count) + prev_char
count = 1
prev_char = char
encoded_string += str(count) + prev_char
return encoded_string
def RLE_decode(encoded_string):
if not encoded_string:
return ""
decoded_string = ""
count = ""
for char in encoded_string:
if char.isdigit():
count += char
else:
decoded_string += char * int(count)
count = ""
return decoded_string