Python Encoder Decoder Program


Please design a Python program that can encode and decode text message by replacing certain letters using the coding rules specified in the codebook table below

Original Letter (Before Encode) Encoded Letter (After Encode)
a z
b y
c x
d w
e v
z a
y b
x c
w d
v e

After all the letters in the codebook table are replaced based on the coding rules, the sentences will be sliced in the middle (n/2) to produce two substrings (substring1 (front) and substring2 (rear)). If the length of the sentence string is not divisible by 2, substring2 will be 1 character longer than substring1. Then substring2 will be switched to front and substring1 will be switched to back for the encoded message.

For example, if we choose to encode string “abzyx”, we first replace all the letters according to coding rules.  “abzyx” will be changed to “zyabc” (“a” replaced by “z”, “b” replaced by “y”, “z” replaced by “a”, “y” replaced by “b”, “x” replaced by “c”). Then we slice “”zyaec” into two substrings substring1 = “zy” and substring2 = “abc”. Then substring2 “abc” will be switched to front and substring1 “zy” will be switched to back in order to form the encoded message. The encoded message for “abzyx” is “abczy”. In decoding the message, we reverse the encoding process.  If we input the encoded message “abczy” into our program, we will get the original text message “abzyx” as the output.

In running your program, the user will first be notified to enter either “Y” or “N”. “Y” means that the user wants to encode a message. “N” means the user wants to decode a message.

PICTURE 1

If the user enters something other than “Y” and “N”, the program will output “Wrong Input” and terminate.

PICTURE 2

If the user enters “Y”, the program will ask user to input the message to be encoded

PICTURE 3

Then the user enters the message to be encoded and the program will print out the encoded message. For example, if the user enters “a cat is using the xerox printer” the program will return the decoded message “hv cvroc printvrz xzt is using t”

PICTURE 4

If the user enters “N”, the program will ask user to input the message to be decoded

PICTURE 5

Then the user enters the message to be decoded and the program will print out the decoded message. For example, if the user enters “hv cvroc printvrz xzt is using t” the program will return the decoded message “a cat is using the xerox printer”

Once you finished writing the program, you can test it using sentence “a cat is using the xerox printer”. For simplicity purpose we assume all the input sentences are in lower case.

(Hint: the replace() method in String is not suitable for solving this problem. i.e. : if you first call the replace() method to replace all “a” with “z”, then you call the replace() method again to replace all “z” with “a”, all the “a”s that have been changed to “z”s before will be changed back to “a”s. Consider read the string character by character instead)

Leave a Reply

Your email address will not be published. Required fields are marked *



  • File Format: Python .py
  • Version: 2.7