Tasks
(20 points) Complete the homework as indicated in the source code.
Download the homework page
Download the source file hw05.py and save it on your computer as <EMAIL>hw05.py where <EMAIL> is your Albion email (ABC12) — don't put any spaces in your file name. The file contains the stubs of several Python functions. Save it directly to your folder with your other lab files and open it in the Python environment. Complete the stub methods to perform the indicated function. Verify your code by running the program with the included test cases (don't remove or change them). Add additional test cases as needed.
In most cases, your function will not print any values. Rather, your function will return values to the calling function and any values will be used there, including possibly printing.
Seek help as soon as you are experiencing difficulty with this assignment.
Do not wait until the deadline to seek help!
Notes
Morse Code
Morse code is a technique for encoding text into dots and dashes, also called dits and dahs. These symbols can be converted into short and long signals (audio, electric, light, etc) and transmitted. This coding system is named after Samuel F.B. Morse, who helped develop it for use in telegraph systems in the 1800s. Common letters, such as E (.), generally have shorter code lengths than uncommon letters such as Q (--.-). For this assignment, we will focus on letters, but codes also exist for digits and other characters.
Fun note: The instrumental song YYZ by the Canadian band Rush uses the Morse code for YYZ (-.-- -.-- --..). YYZ is the International Air Transport Association (IATA) airport code for the Toronto Pearson International Airport.
morseLetter (9 points)
Complete the function called morseLetter that takes a string representing a single character
and returns the Morse code equivalent.
See code for additional information and test cases.
morseWord (5 points)
Complete the function called morseWord that takes a string representing a word (a sequence of letters)
and returns the Morse code equivalent.
See code for additional information and test cases.
morseText (5 points)
Complete the function called morseText that takes a string representing a sentence
and returns the Morse code equivalent.
See code for additional information and test cases.
morseText (1 points)
Modify the main function to output your name in Morse code.
Code
# YOUR NAME HERE
# HW 5
import string
# recall
# string.ascii_uppercase
# string.ascii_lowercase
# convert letter (a-z or A-Z) to morse code
# return empty string if not a single letter
# Note: morseCode[0] represents A or a, etc
# ord("A") is 65
# ord("a") is 97
# do not use Python's replace function
def morseLetter(letter):
morseCode = [".-", "-...", "-.-.", "-..", ".", # A-E
"..-.", "--.", "....", "..", ".---", # F-J
"-.-", ".-..", "--", "-.", "---", # K-O
".--.", "--.-", ".-.", "...", "-", # P-T
"..-", "...-", ".--", "-..-", "-.--", "--.."] # U-Z
return None
# convert a word to morse code
# ignore any non-letters
# separate each morse coded letter with a space
# use your morseLetter() function
# no leading or trailing spaces
def morseWord(word):
return None
# convert text to morse code
# ignore any non-letters
# separate each morse coded word with a space
# use your morseWord() function
# no leading or trailing spaces
def morseText(text):
return None
def main():
print("string constants:")
print(string.ascii_uppercase)
print(string.ascii_lowercase)
print()
print("morseLetter")
print(morseLetter("a"), " should be .-")
print(morseLetter("M"), " should be --")
print(morseLetter("Z"), " should be --..")
print(morseLetter("."), "should return the empty string", sep="")
print(morseLetter("albion"), "should return the empty string", sep="")
# Add aditional test cases as needed here
print()
print("morseWord")
print(morseWord("SOS"), " should be ... --- ...")
# Listen to YYZ by Rush https://www.youtube.com/watch?v=XCCN5umy9lU
print(morseWord("YYZ"), " should be -.-- -.-- --..")
print(morseWord("computer"), " should be -.-. --- -- .--. ..- - . .-.")
# Add aditional test cases as needed here
print()
print("morseText")
text = "Computer Science"
print(text)
print("-.-. --- -- .--. ..- - . .-./... -.-. .. . -. -.-. .")
print(morseText(text)) # Your output
text = "Samuel F. B. Morse"
print(text)
print("... .- -- ..- . .-../..-./-.../-- --- .-. ... .")
print(morseText(text)) # Your output
text = "Madam, I'm Adam."
print(text)
print("-.-. --- -- .--. ..- - . .-./... -.-. .. . -. -.-. .")
print(morseText(text)) # Your output
text = "Pack my box with five dozen liquor jugs."
print(text)
print(".--. .- -.-. -.-/-- -.--/-... --- -..-/.-- .. - ..../..-. .. ...- ./"
+"-.. --- --.. . -./.-.. .. --.- ..- --- .-./.--- ..- --. ...")
print(morseText(text)) # Your output
text = "YOUR NAME HERE" # Replace with your name
print(text)
print(morseText(text)) # Your output
print()
# Add aditional morseText() test cases as needed here
main()
Deliverables
Send me ([email protected]) your Python .py file as an attachment to an
email message with Homework 5 as the subject line.