Skip to content

Commit c952876

Browse files
committedDec 12, 2022
encode and decode string today
1 parent c658892 commit c952876

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
 

‎src/encode_decode_string.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def encode(self, strs):
3+
res = ""
4+
for s in strs:
5+
res += str(len(s)) + "#" + s
6+
return res
7+
8+
def decode(self, str):
9+
res, i = [], 0
10+
while i < len(str):
11+
j = i
12+
while str[j] != "#":
13+
j += 1
14+
length = int(str[i:j])
15+
res.append(str[j + 1 : j + 1 + length])
16+
i = j + 1 + length
17+
return res

‎tests/test_encode_decode_string.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from encode_decode_string import Solution
2+
import logging
3+
4+
logger = logging.getLogger(__name__)
5+
6+
testcase = [
7+
{
8+
"testcase": ["neet", "code", "like", "you"],
9+
"result": ["neet", "code", "like", "you"],
10+
}
11+
]
12+
13+
14+
def test_encode_decode_string():
15+
for tc in testcase:
16+
strs = tc["testcase"]
17+
middle = Solution().encode(strs)
18+
logger.info(print(middle))
19+
ans = Solution().decode(middle)
20+
logger.info(print(ans))
21+
assert ans == tc["result"]

0 commit comments

Comments
 (0)
Please sign in to comment.