Skip to content

Commit 4115d68

Browse files
committed
modified the file
1 parent 714b8b9 commit 4115d68

1 file changed

Lines changed: 43 additions & 29 deletions

File tree

strings/Secret_language.py

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,69 @@
66

77
def random_chars() -> str:
88
"""
9-
Adds random 3 charaters to the string.
9+
Generate a random string of 3 ASCII letters.
1010
11+
>>> import random
12+
>>> random.seed(42)
13+
>>> random_chars()
14+
'ZoX'
1115
"""
12-
return "".join(random.choices(string.ascii_letters, k=3))
16+
return ''.join(random.choices(string.ascii_letters, k=3))
1317

1418

1519
def random_digits() -> str:
1620
"""
17-
Adds 3 random digits to the string.
18-
"""
21+
Generate a random string of 3 digits.
1922
20-
return "".join(random.choices(string.digits, k=3))
23+
>>> import random
24+
>>> random.seed(42)
25+
>>> random_digits()
26+
'638'
27+
"""
28+
return ''.join(random.choices(string.digits, k=3))
2129

2230

23-
<<<<<<< HEAD
2431
def encode(code: str) -> str:
25-
'''
26-
=======
27-
def encode(code) -> str:
2832
"""
29-
>>>>>>> cb237ed2095a60e62d73afd8b17c1207cc1fb4f9
30-
Encodes the code by shifting the first character to the end of the original string,
31-
and adding the 3 random_characters + 3 random-digits + original string(code) + 3 random-digits + 3 random_characters.
32-
unless the length of the code exceeds 3 or equals to it."""
33+
Encode a string by shifting the first character to the end and
34+
wrapping it with random padding of 3 letters and 3 digits on each side.
35+
36+
Reference: https://en.wikipedia.org/wiki/Caesar_cipher
3337
38+
>>> import random
39+
>>> random.seed(42)
40+
>>> encode('hello')
41+
'ZoX638elloh415mJu'
42+
43+
>>> import random
44+
>>> random.seed(42)
45+
>>> encode('hi')
46+
'ZoX638ih415mJu'
47+
"""
3448
if len(code) >= 3:
3549
code = code[1:] + code[0]
36-
code = (
37-
random_chars() + random_digits() + code + random_digits() + random_chars()
38-
)
50+
code = random_chars() + random_digits() + code + random_digits() + random_chars()
3951
else:
4052
code = code[::-1]
41-
code = (
42-
random_chars() + random_digits() + code + random_digits() + random_chars()
43-
)
53+
code = random_chars() + random_digits() + code + random_digits() + random_chars()
4454
return code
4555

46-
<<<<<<< HEAD
47-
def decode(code: str) -> str:
48-
'''
49-
=======
5056

51-
def decode(code) -> str:
57+
def decode(code: str) -> str:
5258
"""
53-
>>>>>>> cb237ed2095a60e62d73afd8b17c1207cc1fb4f9
54-
decodes the encoded string by removing the randomly added characters and reversing the shift
55-
59+
Decode an encoded string by stripping the random padding and
60+
reversing the character shift.
61+
62+
>>> import random
63+
>>> random.seed(42)
64+
>>> decode(encode('hello'))
65+
'hello'
66+
67+
>>> import random
68+
>>> random.seed(42)
69+
>>> decode(encode('hi'))
70+
'hi'
5671
"""
57-
5872
code = code[6:-6]
5973
if len(code) >= 3:
6074
code = code[-1] + code[:-1]
@@ -69,4 +83,4 @@ def decode(code) -> str:
6983
decoded = decode(encoded)
7084
print(f"Original → {code}")
7185
print(f"Encoded → {encoded}")
72-
print(f"Decoded → {decoded}")
86+
print(f"Decoded → {decoded}")

0 commit comments

Comments
 (0)