Decoding Project with Python
Imagine there is new language whose only character is case sensitive X. We know that the following X’s combinations correspond to a certain latin letter:
- XXx–>a
- xXX–>e
- XxX–>H
- xxx–>o
- Xxx–>r
- XXX–>u
- xxX–>w
- xXx–>y
The goal of this project with Python is to write a program to translate into English the following message:
“XxXxxxxxX XXxXxxxXX xXxxxxXXX”
Steps
- Â Define the equivalences between letters and their corresponding X’s combinations.
- Iterate through the given message and for each character and check if it exists in the
"equivalences"dictionary. If it does, replace the character with its corresponding code; otherwise, keep the character as it is.
Solutions
## Define the dictionary of equivelences
equivalences = {
'XxX': 'H', 'xxx': 'o', 'xxX': 'w', 'XXx': 'a',
'xXX': 'e', 'Xxx': 'r', 'XXX': 'u', 'xXx': 'y'
}
message = "XxXxxxxxX XXxXxxxXX xXxxxxXXX"
translated_message = ""
i = 0
while i < len(message):
if message[i:i+3] in equivalences:
translated_message += equivalences[message[i:i+3]]
i += 3
else:
translated_message += message[i]
i += 1
print(translated_message)