File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Encoding & Decoding
2+
3+ import random
4+ import string
5+
6+ def random_chars () -> str :
7+ '''
8+ Adds random 3 charaters to the string.
9+
10+ '''
11+ return '' .join (random .choices (string .ascii_letters , k = 3 ))
12+
13+ def random_digits () -> str :
14+ '''
15+ Adds 3 random digits to the string.
16+ '''
17+
18+ return '' .join (random .choices (string .digits , k = 3 ))
19+
20+ def encode (code ) -> str :
21+ '''
22+ Encodes the code by shifting the first character to the end of the original string,
23+ 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+
26+ if len (code ) >= 3 :
27+ code = code [1 :] + code [0 ]
28+ code = random_chars () + random_digits () + code + random_digits () + random_chars ()
29+ else :
30+ code = code [::- 1 ]
31+ code = random_chars () + random_digits () + code + random_digits () + random_chars ()
32+ return code
33+
34+ def decode (code ) -> str :
35+ '''
36+ decodes the encoded string by removing the randomly added characters and reversing the shift
37+
38+ '''
39+
40+ code = code [6 :- 6 ]
41+ if len (code ) >= 3 :
42+ code = code [- 1 ] + code [:- 1 ]
43+ else :
44+ code = code [::- 1 ]
45+ return code
46+
47+ if __name__ == "__main__" :
48+ code = input ("Enter the code: " )
49+ encoded = encode (code )
50+ decoded = decode (encoded )
51+ print (f"Original → { code } " )
52+ print (f"Encoded → { encoded } " )
53+ print (f"Decoded → { decoded } " )
You can’t perform that action at this time.
0 commit comments