33import random
44import string
55
6+
67def random_chars () -> str :
7- '''
8+ """
89 Adds random 3 charaters to the string.
9-
10- '''
11- return '' .join (random .choices (string .ascii_letters , k = 3 ))
10+
11+ """
12+ return "" .join (random .choices (string .ascii_letters , k = 3 ))
13+
1214
1315def random_digits () -> str :
14- '''
16+ """
1517 Adds 3 random digits to the string.
16- '''
17-
18- return '' .join (random .choices (string .digits , k = 3 ))
18+ """
19+
20+ return "" .join (random .choices (string .digits , k = 3 ))
21+
1922
2023def encode (code ) -> str :
21- '''
24+ """
2225 Encodes the code by shifting the first character to the end of the original string,
2326 and adding the 3 random_characters + 3 random-digits + original string(code) + 3 random-digits + 3 random_characters.
24- unless the length of the code exceeds 3 or equals to it.'''
25-
27+ unless the length of the code exceeds 3 or equals to it."""
28+
2629 if len (code ) >= 3 :
2730 code = code [1 :] + code [0 ]
28- code = random_chars () + random_digits () + code + random_digits () + random_chars ()
31+ code = (
32+ random_chars () + random_digits () + code + random_digits () + random_chars ()
33+ )
2934 else :
3035 code = code [::- 1 ]
31- code = random_chars () + random_digits () + code + random_digits () + random_chars ()
36+ code = (
37+ random_chars () + random_digits () + code + random_digits () + random_chars ()
38+ )
3239 return code
3340
41+
3442def decode (code ) -> str :
35- '''
43+ """
3644 decodes the encoded string by removing the randomly added characters and reversing the shift
37-
38- '''
39-
45+
46+ """
47+
4048 code = code [6 :- 6 ]
4149 if len (code ) >= 3 :
4250 code = code [- 1 ] + code [:- 1 ]
4351 else :
4452 code = code [::- 1 ]
4553 return code
4654
55+
4756if __name__ == "__main__" :
4857 code = input ("Enter the code: " )
4958 encoded = encode (code )
50- decoded = decode (encoded )
59+ decoded = decode (encoded )
5160 print (f"Original → { code } " )
5261 print (f"Encoded → { encoded } " )
53- print (f"Decoded → { decoded } " )
62+ print (f"Decoded → { decoded } " )
0 commit comments