forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.py
36 lines (32 loc) · 939 Bytes
/
sol.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
class RunLengthEncoding:
@staticmethod
def encode(string):
string = list(string)
char = string.pop(0)
times = 1
encoding = ""
while string:
this_char = string.pop(0)
if this_char==char:
times += 1
else:
encoding += "{0}{1}".format(times, char)
times = 1
char = this_char
encoding += "{0}{1}".format(times, char)
return encoding
@staticmethod
def decode(string):
string = list(string)
decoding = ""
while string:
times = int(string.pop(0))
char = string.pop(0)
decoding += times*char
return decoding
if __name__=='__main__':
rle = RunLengthEncoding()
string = "AAAABBBCCDAA"
encoded = rle.encode(string)
decoded = rle.decode(encoded)
print('-->'.join((string, encoded, decoded)))