-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Code
More file actions
55 lines (49 loc) · 1.88 KB
/
Copy pathPython Code
File metadata and controls
55 lines (49 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import random
import string
def random_characters(length=3):
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=length))
def encode(message):
encoded_words = []
for word in message.split():
if len(word) >= 3:
first_letter = word[0]
rest_of_word = word[1:]
coded_word = random_characters() + rest_of_word + first_letter + random_characters()
encoded_words.append(coded_word)
else:
encoded_words.append(word[::-1]) # Reverse the string
return ' '.join(encoded_words)
def decode(encoded_message):
decoded_words = []
for word in encoded_message.split():
if len(word) < 3:
decoded_words.append(word[::-1]) # Reverse the string
else:
# Remove 3 random characters from start and end
trimmed_word = word[3:-3]
if trimmed_word:
last_letter = trimmed_word[-1]
decoded_word = last_letter + trimmed_word[:-1]
decoded_words.append(decoded_word)
else:
decoded_words.append(word) # Handle empty string case
return ' '.join(decoded_words)
print("\n=== Secret Code Language ===")
while True:
print("\n1. Encode a Message")
print("2. Decode a Message")
print("3. Exit")
choice = input("Select an option (1-3): ").strip()
if choice == '1':
message = input("Enter the message to encode: ")
coded_message = encode(message)
print(f"\nEncoded message: {coded_message}")
elif choice == '2':
encoded_message = input("Enter the message to decode: ")
decoded_message = decode(encoded_message)
print(f"\nDecoded message: {decoded_message}")
elif choice == '3':
print("Exiting the program. Goodbye!")
break
else:
print("Invalid option! Please choose a valid option (1-3).")