-
Notifications
You must be signed in to change notification settings - Fork 17
/
encode-and-decode-strings.py
96 lines (65 loc) · 2.23 KB
/
encode-and-decode-strings.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from typing import List
def number_to_str(number: int) -> str:
base = 256
result = []
for _ in range(4): # length of chunk length field
result.append(chr(number % base))
number = number // base
if number > 0:
raise RuntimeError("Number is too large")
return "".join(reversed(result))
def str_to_number(string: str) -> int:
base = 256
result = 0
for char in string:
result = result * base + ord(char)
return result
class Codec:
def encode(self, strs: List[str]) -> str:
result_list: List[str] = []
for string in strs:
result_list.append(number_to_str(len(string)) + string)
return "".join(result_list)
def decode(self, s: str) -> List[str]:
result_list: List[str] = []
pos = 0
while pos < len(s):
chunk_length = str_to_number(s[pos : pos + 4])
start = pos + 4
end = start + chunk_length
result_list.append(s[start : end])
pos = end
return result_list
class CodecEscape:
def encode(self, strs: List[str]) -> str:
"""Encodes a list of strings to a single string.
"""
result_list = []
for string in strs:
for char in string:
if char == "\\":
result_list.append(char)
result_list.append(char)
result_list.append("\\n")
return "".join(["".join(r for r in result_list)])
def decode(self, s: str) -> List[str]:
"""Decodes a single string to a list of strings.
"""
result_list: List[List[str]] = [[]]
pos = 0
while pos < len(s):
if pos < len(s) - 1 and s[pos] == "\\":
if s[pos + 1] == "\\":
result_list[-1].append("\\")
elif s[pos + 1] == "n":
result_list.append([])
pos += 1
else:
result_list[-1].append(s[pos])
pos += 1
if not result_list[-1]:
result_list.pop()
return ["".join(r) for r in result_list]
# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(strs))