Skip to content

Commit 1c119cf

Browse files
hanhan
authored andcommitted
encode decode
1 parent 20c1067 commit 1c119cf

File tree

1 file changed

+68
-0
lines changed
  • encode-and-decode-strings

1 file changed

+68
-0
lines changed

β€Žencode-and-decode-strings/han.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
class Solution:
3+
"""
4+
@param: strs: a list of strings
5+
@return: encodes a list of strings to a single string.
6+
"""
7+
def encode(self, strs):
8+
return '-'.join([
9+
'.'.join([
10+
str(ord(c))
11+
for c in s
12+
])
13+
for s in strs
14+
])
15+
16+
"""
17+
@param: str: A string
18+
@return: decodes a single string to a list of strings
19+
"""
20+
def decode(self, str):
21+
return [
22+
''.join([
23+
chr(int(c))
24+
for c in s.split('.')
25+
])
26+
for s in str.split('-')
27+
]
28+
29+
30+
# μ•„λž˜λŠ” κ°„λ‹¨ν•œ ν…ŒμŠ€νŠΈ μ½”λ“œ
31+
import random
32+
33+
def generate_random_string(min_length=5, max_length=20):
34+
# Choose a random string length between min_length and max_length
35+
string_length = random.randint(min_length, max_length)
36+
37+
# Define the character categories
38+
alphabets = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
39+
numbers = '0123456789'
40+
hanguls = 'κ°€λ‚˜λ‹€λΌλ§ˆλ°”μ‚¬μ•„μžμ°¨μΉ΄νƒ€νŒŒν•˜'
41+
emojis = ['πŸ˜€', 'πŸ˜‚', 'πŸ˜‰', '😍', '😎', '😭', '😊', '😜', '😒', '😱']
42+
43+
# Probability of selecting from each category
44+
weights = [0.25, 0.25, 0.25, 0.25]
45+
46+
# Combine all categories into a single list
47+
all_chars = list(alphabets + numbers + hanguls) + emojis
48+
49+
# Select characters based on the weights and create the final string
50+
random_chars = random.choices(all_chars, k=string_length)
51+
random_string = ''.join(random_chars)
52+
53+
return random_string
54+
55+
def generate_multiple_random_strings():
56+
strings_list = []
57+
# Generate a random number of strings, between 1 and 10
58+
number_of_strings = random.randint(1, 10)
59+
60+
for _ in range(number_of_strings):
61+
random_string = generate_random_string()
62+
strings_list.append(random_string)
63+
64+
return strings_list
65+
66+
strs = generate_multiple_random_strings()
67+
print(strs)
68+
print(strs==Solution().decode(Solution().encode(strs)))

0 commit comments

Comments
Β (0)