|
| 1 | + # Write a python program to translate a message into secret code language. |
| 2 | + # Use the rules below to translate normal English into secret code language |
| 3 | +# Coding: |
| 4 | + # if the word contains atleast 3 characters, remove the first letter and append it at the end |
| 5 | + # now append three random characters at the starting and the end |
| 6 | + # else: |
| 7 | + # simply reverse the string |
| 8 | +# Decoding: |
| 9 | + # if the word contains less than 3 characters, reverse it |
| 10 | + # else: |
| 11 | + # remove 3 random characters from start and end. Now remove the last letter and append it to the beginning |
| 12 | + # Your program should ask whether you want to code or decode |
| 13 | + |
| 14 | + #short method |
| 15 | +# def encode(word): |
| 16 | +# return word[::-1] if len(word) < 3 else "abc" + word[1:] + word[0] + "xyz" |
| 17 | + |
| 18 | +# def decode(word): |
| 19 | +# return word[::-1] if len(word) < 3 else word[-4] + word[3:-4] |
| 20 | + |
| 21 | +# mode = input("Choose 'encode' or 'decode': ").strip().lower() |
| 22 | +# message = input("Enter your message: ").strip() |
| 23 | +# result = ' '.join((encode if mode == "encode" else decode)(word) for word in message.split()) |
| 24 | +# print("Result:", result) |
| 25 | + |
| 26 | +def encode(word): |
| 27 | + if len(word) < 3: |
| 28 | + return word[::-1] # Reverse the word but -1 HELLO _1,-1 krte krte o,l,l,e,h |
| 29 | + else: |
| 30 | + return "abc" + word[1:] + word[0] + "xyz" # Modify the word |
| 31 | + |
| 32 | +def decode(word): |
| 33 | + if len(word) < 3: |
| 34 | + return word[::-1] # Reverse the word |
| 35 | + else: |
| 36 | + return word[-4] + word[3:-4] |
| 37 | + |
| 38 | +mode = input("Enter 'code' to encode or 'decode' to decode: ") |
| 39 | +message = input("Enter the message: ") |
| 40 | + |
| 41 | +words = message.split() #convert kr dega list me "hello world"-----["hello", "world"] |
| 42 | +result = [] |
| 43 | +for word in words: |
| 44 | + if mode == "encode": |
| 45 | + result.append(encode(word)) |
| 46 | + elif mode == "decode": |
| 47 | + result.append(decode(word)) |
| 48 | + else: |
| 49 | + print("Invalid mode. Please choose 'encode' or 'decode'.") |
| 50 | + break |
| 51 | +print("Result:", ' '.join(result)) #Joins words in the result list into a single string, with a space (' ') between each word. |
0 commit comments