Program of the Day #18

In this program, you will implement a Morse code translator. Morse code is a system for representing messages that could be sent over electronic communications systems - e.g., wires and radios, before computers were around. Each character in Morse code is represented using a sequence of dots and dashes, representing a short or long tone (or change in voltage on a wire). For example, the character “A” is encoded as .- (dot dash), and “B” is encoded as -... (dash dot dot dot).

The text file P18_code.txt contains a tranlation key: each line contains a character and its corresponding Morse code. Your task is to write a program that can translate strings to and from their Morse code representation. The main program’s behavior is as follows:

The program takes the filename of the text file translation key as its first argument, and a string to translate as its second argument. Recall that you can pass a string with spaces as a single argument by enclosing it in quotes.

You can see the list of valid characters for “regular text” in the translation key file. Any characters that are not in this file should be encoded as a ?; likewise, any Morse code sequence that’s not recognized should be translated into regular text as a ?.

Since Morse codes are multiple characters, we need to have some space between them to know when one character code ends and the next begins. In real systems using Morse code, this was done by adding a pause (the length of three dots) between characters. Since we are dealing in text, we will use a space character to separate codes.

Here are some example runs:

>>> %Run P18_morse.py P18_code.txt ABC
.- -... -.-.
>>> %Run P18_morse.py P18_code.txt '.- -... -.-.'
ABC
>>> %Run P18_morse.py P18_code.txt 'O K'
---  -.-
>>> %Run P18_morse.py P18_code.txt '---  -.-'
O K
What about spaces in the input string?

We can take the following approach to allow spaces in a regular text input string to translate into and out of Morse code intact:

To help you implement this program, we have provided four helper functions with detailed specifications in the skeleton code.

Other Practice Problems

  1. Implement the following function. Notice that this function returns a new list and leaves in_list unmodified.

    def copy_list(in_list):
        """ Return a new list object containing the same elements as in_list.
            Precondition: in_list's contents are all immutable. """
    
    # Example usage:
    a = [1, 2, 3, 4]
    b = copy_list(a)
    b[0] = 0
    print(a[0], b[0])
    # prints 1, 0
  2. Implement the following function. Notice that this function does not return a new list, but modifies the given list in place. Because the list object being modified is the same one as the caller passed in, we do not need to return another reference to the same object.

    def cull(a):
       """ Remove a randomly chosen half of the elements from the given list """
    
    # Example usage:
    a = [1, 2, 3, 4]
    cull(a)
    print(a)
    # prints [2, 3] (or any other choice of 2 elements from the original list)