Skip to content

Commit b0b4bb7

Browse files
committed
encode-decode solved
1 parent 01566bc commit b0b4bb7

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

leetcode-master/arrays_and_hashing/encode_decode_strings.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,32 @@
1212
Input: [“we”, “say”, “:”, “yes”] Output: [“we”, “say”, “:”, “yes”] Explanation: One possible encode method is: “we:;say:;:::;yes”
1313
"""
1414

15+
#solution by rubix-coder
16+
import random
17+
class SolutionEncodeDecode:
18+
19+
def encode(strs):
20+
encoded_str = []
21+
for s in strs:
22+
encoded_str.append(f"{random.randint(0,len(s))}{s}{random.randbytes(3)}{random.choice(strs)}")
23+
return encoded_str
24+
25+
def decode(strs):
26+
decoded_str = []
27+
for s in strs:
28+
decoded_str.append(f"{s.split("b'")[0][1:]}")
29+
return decoded_str
30+
""
31+
prompts = ["lint","code","love","you","algorithms","ML","!", "rubix", "coder"]
32+
33+
34+
35+
for _ in range(20,200):
36+
msg = [random.choice(prompts) for _ in range(1,random.randint(2,len(prompts)))]
37+
encoded = SolutionEncodeDecode.encode(strs=msg)
38+
print(f"encoded msg -> {encoded}")
39+
decode = SolutionEncodeDecode.decode(encoded)
40+
print(f"decoded msg -> {decode}\n############")
1541

1642
class Solution:
1743

@@ -34,8 +60,8 @@ def decode(self, str):
3460
return result
3561

3662

37-
s = Solution()
38-
input_list = ["sachin", "jose", "python", "learning", "leet", "code"]
39-
encode_str = s.encode(input_list)
40-
print(encode_str)
41-
print(s.decode(encode_str))
63+
# s = Solution()
64+
# input_list = ["python", "learning", "leet", "code"]
65+
# encode_str = s.encode(input_list)
66+
# print(encode_str)
67+
# print(s.decode(encode_str))

0 commit comments

Comments
 (0)