Difference between revisions of "Morse Code Translator"

From WLCS
 
(One intermediate revision by the same user not shown)
Line 7: Line 7:
 
'''References:'''
 
'''References:'''
 
* [http://en.wikipedia.org/wiki/Morse_code Morse code]
 
* [http://en.wikipedia.org/wiki/Morse_code Morse code]
* [[Media:Strings_Python.pptx]]
+
* [https://docs.google.com/presentation/d/1hv7kOqNVPtZ_e2XqgYuRQSvwn2jOEUgYRFipTMFstTI/edit?usp=sharing Strings (Python) slides]
 
* [http://openbookproject.net/thinkcs/python/english2e/ch07.html Strings]
 
* [http://openbookproject.net/thinkcs/python/english2e/ch07.html Strings]
 
* [http://openbookproject.net/thinkcs/python/english2e/ch03.html Functions]
 
* [http://openbookproject.net/thinkcs/python/english2e/ch03.html Functions]
Line 13: Line 13:
 
* [http://openbookproject.net/thinkcs/python/english2e/ch06.html Iteration (while loops)]
 
* [http://openbookproject.net/thinkcs/python/english2e/ch06.html Iteration (while loops)]
  
'''Translator Guidelines:'''
+
'''Translator Guidelines (slightly different from the official standard):'''
 
* Dots (.), dashes (-), and spaces ( ) will be used for the code
 
* Dots (.), dashes (-), and spaces ( ) will be used for the code
 
* A single space will separate Morse code letters (e.g. "cat" = "-.-. .- -")
 
* A single space will separate Morse code letters (e.g. "cat" = "-.-. .- -")

Latest revision as of 08:07, 13 January 2020

Objectives:

  • You will create a string translator that converts Morse code to a alpha-numeric characters
  • You will use while loops to traverse (walk-through) a string
  • You will use if-statements to check for Morse code strings
  • You will use functions to retrieve corresponding alpha-numeric characters

References:

Translator Guidelines (slightly different from the official standard):

  • Dots (.), dashes (-), and spaces ( ) will be used for the code
  • A single space will separate Morse code letters (e.g. "cat" = "-.-. .- -")
  • Words are separated by 3 spaces (e.g. "fat cat" = "..-. .- -   -.-. .- -")

Directions: Part 1: Plaintext -> Morse code

  1. Create a file named plain2morse.py
  2. Bring up the Morse Code alphabet
  3. Define a function named getMorseLetter(ch) that takes one parameter. Inside the definition of getMorseLetter(), you will check to see what letter ch is, and then return the corresponding Morse code string. See the example code below and complete it with the rest of the alphabet
    • At the end of the getMorseLetter(ch) function, return ch in case none of the letters matched
def getMorseLetter(ch):
    if ch == "a":
        return ".-"
    if ch == "b":
        return "-..."
    # add the rest of your alphabet here
  1. After the function definition, prompt the user to enter a string and store it in a variable plain
  2. Initialize a new variable named morse to an empty string ""
  3. Write a loop that traverses the plain string
    1. Inside the loop, translate the current letter (plain[x]) by using the getLetter(plain[x]) AND add it to morse (HINT: you are building the morse string letter by letter)
  4. After the loop, morse should contain a translation of every letter. Print out morse
  5. Submit to repl.it

Part 2: Morse code -> plaintext (with a function)

  1. Create a new file named morse2plain.py
  2. Define a function named getPlainLetter(m) which will return the plaintext letter/number that corresponds to the Morse code letter m
  3. After the function definition, prompt the user for a Morse code string and store it in a mStr
  4. Create a string variable plainStr that starts with an empty string ""
    • plainStr will store our translated plaintext string
  5. Create a string variable mLetter that starts with an empty string ""
    • You will use mLetter in the loop below to store all the dots and dashes for a single Morse code letter (because a single letter can be represented with one or more dots and dashes)
  6. Write a loop that traverses every character of mStr
    1. If the current character is not a space, then add it to mLetter
    2. Otherwise, if the current character is a space, then translate the symbols using getPlainLetter(mLetter), add the translated letter to plainStr, and reset mLetter
  7. After the loop, print your translated plainStr word

Part 2: Morse code -> plaintext (with dictionaries)

  1. Create a new file named morse2plain.py
  2. Define a dictionary named morse2plain which will return the plaintext letter/number that corresponds to the Morse code letter
  3. After the dictionary, prompt the user for a Morse code string and store it in a mStr
  4. Create a string variable plainStr that starts with an empty string ""
    • plainStr will store our translated plaintext string
  5. Create a string variable mLetter that starts with an empty string ""
    • You will use mLetter in the loop below to store all the dots and dashes for a single Morse code letter (because a single letter can be represented with one or more dots and dashes)
  6. Write a loop that traverses every character of mStr
    1. If the current character is not a space, then add it to mLetter
    2. Otherwise, if the current character is a space, then translate the symbols using morse2plain[mLetter], add the translated letter to plainStr, and reset mLetter
  7. After the loop, print your translated plainStr word

Additional Features:

  • Add the ability to translate multiple words of Morse code to plaintext