-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.py
45 lines (30 loc) · 1.27 KB
/
script.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
'''
This problem was asked by Amazon.
Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated successive
characters as a single count and character. For example, the string AAAABBBCCDAA would be encoded as 4A3B2C1D2A.
Implement run-length encoding and decoding. You can assume the string to be encoded have no digits and consists solely
of alphabetic characters. You can assume the string to be decoded is valid.
'''
def run_length_encoding(chars):
result = ''
previous_letter = ''
letter_count = 0
for letter in chars:
letter_count += 1
if letter != previous_letter and previous_letter != '' and letter_count != 10:
result += f'{letter_count - 1}{previous_letter}'
letter_count = 1
elif letter_count == 10:
result += f'{letter_count - 1}{previous_letter}'
letter_count = 1
previous_letter = letter
result += f'{letter_count}{previous_letter}'
return result
run_length_encoding('AAAAAAAAAAABBAACCDDDD')
def decoding(chars):
chars_list = [el for el in chars]
res = ''
for letter in range(0, len(chars_list), 2):
res += int(chars_list[letter]) * f'{chars_list[letter + 1]}'
return res
decoding('9A2A2B2A2C4D')