Skip to content

Commit ea72a2c

Browse files
committed
added new file of secret_language
1 parent 4e9b21e commit ea72a2c

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

strings/Secret_language.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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}")

0 commit comments

Comments
 (0)