-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamz_Implement_RL_Encoding_Decoding_Strings.py
More file actions
50 lines (37 loc) · 1.42 KB
/
amz_Implement_RL_Encoding_Decoding_Strings.py
File metadata and controls
50 lines (37 loc) · 1.42 KB
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
46
47
48
49
50
#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.
# Solution:
def rlencode_string(words):
encoded_chars = list()
count = 0
prev_char = None
for character in words:
if character == prev_char or not prev_char:
count += 1
else:
encoded_chars.append(str(count))
encoded_chars.append(prev_char)
count = 1
prev_char = character
if count:
encoded_chars.append(str(count))
encoded_chars.append(prev_char)
return "".join(encoded_chars)
def rldecode_string(words):
decoded_chars = list()
index = 0
while index < len(words):
decoded_chars.append(int(words[index]) * words[index + 1])
index += 2
return "".join(decoded_chars)
assert rlencode_string("") == ""
assert rlencode_string("AAA") == "3A"
assert rlencode_string("AAAABBBCCDAA") == "4A3B2C1D2A"
assert rldecode_string("") == ""
assert rldecode_string("3A") == "AAA"
assert rldecode_string("4A3B2C1D2A") == "AAAABBBCCDAA"