I will start a new series on how to approach writing a Python application when you are given a set of parameters. With these parameters, we will step through how to write out the code, execute the code, troubleshoot the code, and see how we can refactor the code to make it cleaner and more concise. The first project in this series is a Morse Code Encoder.
Before we start coding, let’s first understand what Morse Code is. Morse Code was named after Samuel Morse, who invented the telegraph and is a way to send messages via the telegraph with a series of dots (.) and dashes (-). A dot would be one press of the handle, whereas a dash would be equivalent to pressing the dot three times. An image of a telegraph is below:
Instructions
We will create a Python application that will take in the word or phrase we want to send via Morse Code, and it will convert it into how we should send it via a telegraph. We can create this any way we want if the application returns the Morse Code output.
Letter to Morse Code Snippet
The first thing we want to get into our code is character-to-code mapping. We must find the best way to store this information to retrieve it relatively easily. We could look at two data types for this application: a list or a dictionary. A list would not work, as we couldn’t correlate the character to the code. We could create a list and nest dictionaries inside of the list, but this would be overkill, and it would be difficult to try to retrieve the value of the key we pass in.
The easiest way to implement this part of the code is with a dictionary. When we are looking at taking a word or phrase and outputting Morse Code, we can use the character as the dictionary key to return the value, which in this instance would be the code for that character. The code we would want to implement for this part is:
char_to_code = {
'A': ".-",
'B': "-...",
'C': '-.-.',
'D': '-..',
'E': '.',
'F': '..-.',
'H': '....',
'I': '..',
'K': '.---',
'L': '.-..',
'M': '--',
'N': '-.',
'O': '---',
'P': '.--.',
'Q': '--.-',
'R': '.-.',
'S': '...',
'T': '-',
'U': '..-',
'V': '...-',
'W': '.--',
'X': '-..-',
'Y': '-.--',
'Z': '--..',
'1': '.----',
'2': '..---',
'3': '...--',
'4': '....-',
'5': '.....',
'6': '-....',
'7': '--...',
'8': '---..',
'9': '----.',
'0': '-----',
}
Code Logic
We have our dictionary of characters for Morse Code. First, we want input from the individual running the application on what they want to encode into Morse Code. This can be accomplished with a simple input method assigned to a variable:
code = input("Enter what you want to encode as Morse Code: ").upper()
To make our code more predictable and not have to worry about ensuring the individual running the code always puts in an uppercase character, we can call the string method .upper()
to ensure that any input by the user gets stored in the capitalized variable, regardless of how it was entered.
The next thing we want to look at is how to parse the input into searching the dictionary for the code we are looking for.
- A string that contains the user’s input
- A dictionary that contains our character-to-code mapping
We want to take each character in the string and match it somehow to the character in the dictionary key. Strings can be iterated over, allowing us to start pulling each string character out. One of the tools in our tool bag we can use is a for-loop. With a for-loop, we can go through each letter from the input and do something with it.
Now that we have access to every character in the string, how can we use that to get the value from the key in the dictionary? We can use each character to pull back the value represented by the key, which, in this instance, is the character.
code = input("Enter what you want to encode as Morse Code: ").upper()
for char in code:
encoded_mc = char_to_code[char]
print(f"Your Encoded Morse Code is: {encoded_mc}")
If we were to run this application, we would return the last letter we are decoding.
Enter what you want to encode as Morse Code: Hello
Your Encoded Morse Code is: ---
This is because as we iterate through the input, the variable encoded_mc
gets written over with each pass of the for-loop. We need a way to store what we pull from the dictionary; this is where a list comes in. Every time the for-loop is run, we want to append the code pulled from the dictionary to it
code = input("Enter what you want to encode as Morse Code: ").upper()
encoded_mc = []
for char in code:
encoded_mc.append(char_to_code[char])
print(f"Your Encoded Morse Code is: {encoded_mc}")
When we run the application, we return everything we iterated over in a list.
Let’s Run the Application!
Enter what you want to encode as Morse Code: Hello
Your Encoded Morse Code is: ['....', '.', '.-..', '.-..', '---']
This completes the assignment of being able to pass characters into an application and return Morse Code, but what if we wanted to take it further? How can we return a string versus a list? How would we decode Morse code back to characters to understand what we are being sent? Can we do anything to make the application more readable or optimize it?
In what ways can you think of optimizing the application or rewriting it?