Question

Please write code using Python.

Rubric
Assignment Solution Total: 100 pts
Program successfully retrieves and validates user input 15 pts
Morse Code Function Created 10 pts
Function Uses Parameters to get input message 15 pts
Function successfully converts passed message to Morse Code
and returns the Morse Code equivalent of message 45 pts
Program calls Morse Code function and outputs the result of
the function (i.e. the actual Morse Code) 15 pts

Main Function Requirement
Part 2 MUST have a main function defined.

Prompt:

Morse Code, created by Samuel Morse, is a method used as far back as the early 19th century to encode
alphanumeric characters and common punctuation marks into a series of dots and dashes. These dots
and dashes can be communicated by various means, such as a flashlight turning on and off in a night sky
or across electical wires as electricity flow being switched. This lead to the invention of the first
electrical communication device, the telegraph, a device that could transmit electrical pulses across a
network of wires.
In this assignment, we are going to apply concepts we learned over the past few month to produce a
program that takes in a message as user-specified input and generates a series of dots and dashes that
will represent its Morse Code encoding. Appendix A has a table of Morse Code encodings for the
characters we will use in this program.
The way this program will work is that it will utilize two parallel lists, like we saw in the
famous_scientists program in a previous lab. One list will contain the letters, numbers, and punctuation
marks that can be converted to Morse Code, and the other list will contain the Morse Code string for
each matching character(i.e. the string will contain the series of dots and dashes that represent the
Morse Code for that character). I have prepared a library of functions that you may use to quickly
generate these lists (will be provided along with assignment document). Use these funtions to return
the character list and the morse code list.
The program will ask the user for the message as input. Since morse code encoding has no distinction
between upper and lower case characters, make sure to convert the message to all lowercase
characters (Link: https://www.tutorialspoint.com/python/string_lower.htm). Once you have inputted
the message from the user, you must then use the contains_valid_chars function to make sure that the
message has no invalid characters (i.e. characters not present in the Morse Code table we are using in
Appendix A). Repeatedly ask the user for the message if invalid characters are entered by the user.
Once the user message is validated, pass the input to a function that you will create called
conv_to_morse. conv_to_morse will generate the Morse table using the function from the module I
provided (i.e. the generate_char_table and generate_morse_table) and, for each character in the
message, determine the index of that character in the char_table and then use the same index to find
the morse_code string of that character. Append each morse_code string to a string called
morse(should be initialized to an empty string) to produce the entire Morse code message. Return the
Morse code message to main, where main will output the message.

Appendix A - Morse Code Table .- A B C --- -... -.-. -.. Morse N O P Q R -- - ..- --. W .-- J K .--- -.- L X L - ..- -- :-:-:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Summary :

python code is provided below with suggested function (check notes ).

Output is also provided for valid and invalid inputs .

Notes :

i) As some of the functions not avaible , assumed the table with hash , this can be easily converted ti index table by using two table one which stores at nth location the char and map that nth to another table which provides morse code .

ii) I'v utilized delimiter ' ' as shown online just for better read the output , however this can be made to '' if required .

######################### python code ######################

import re
##
## Define Morse Hash for each char
##
def conv_to_morse(ichar) :
    morseHash = {
        'A' : '•−',
        'B' : '−•••',
        'C' : '−•−•',
        'D' : '−••',
        'E' : '•',
        'F' : '••−•',
        'G' : '−−•',
        'H' : '••••',
        'I' : '••',
        'J' : '•−−−',
        'K' : '−•−',
        'L' : '•−••',
        'M' : '−−',
        'N' : '−•',
        'O' : '−−−',
        'P' : '•−−•',
        'Q' : '−−•−',
        'R' : '•−•',
        'S' : '•••',
        'T' : '−',
        'U' : '••−',
        'V' : '•••−',
        'W' : '•−−',
        'X' : '−••−',
        'Y' : '−•−−',
        'Z' : '−−••',
        '0' : '−−−−−',
        '1' : '•−−−−',
        '2' : '••−−−',
        '3' : '•••−−',
        '4' : '••••−',
        '5' : '•••••',
        '6' : '−••••',
        '7' : '−−•••',
        '8' : '−−−••',
        '9' : '−−−−•',
        '.' : '•−•−•−',
        ',' : '−−••−−',
        ':' : '−−−•••',
        '?' : '••−−••',
        "'" : '•−−−−•',
        '-' : '−••••−',
        '??' : '•−••−•',
        ' ' : '     ',
        }
  
    if ( ichar in morseHash.keys()) :
        return morseHash[ichar]
    else :
        print(" Invalid MorseCode input ",ichar)
        return ""
  
  
def getMorseCode( input_str ):
  
    morse = ""
    morse_delimiter = " "
    qnum = 0
    for ch in (input_str ):
        ch0 = ch.upper()
      
        if ch0 == '?' :
            qnum = qnum + 1
        else :
            if qnum == 1 :
                morse = morse + morse_delimiter + conv_to_morse('?')
            elif qnum > 1 :
                morse = morse + morse_delimiter + conv_to_morse('??')
            qnum = 0
      
        if qnum < 1 :   
            morse = morse + morse_delimiter + conv_to_morse(ch0)
          
    if qnum == 1 :
        morse = morse + morse_delimiter + conv_to_morse('?')
    elif qnum > 1 :
        morse = morse + morse_delimiter + conv_to_morse('??')
          
    return morse

def contains_valid_chars(input_str ):
  
    iflag = False
    validLetters = [":","\.",",","\?","'", "-", " ", "[a-zA-Z0-9]"]
    tmp_str = '(?:% s)' % '|'.join(validLetters)
    ilen = 0
    for ch in (input_str) :
        if re.match(tmp_str,ch) :
            ilen = ilen + 1
        else :
            break
    
    
    if ( ilen != len(input_str) ) :
        print (" Input contains invalid Chars, please try again \n")
        iflag = False
    else :
        iflag = True
  
    return iflag

def main() :

    flag = False
    while( not flag) :
        istr = input("Enter String for Morse Code : ")
        flag = contains_valid_chars(istr)
      
    morseCode = getMorseCode(istr)
  
    print("Morse Coded Str : " , morseCode )
  
      
if __name__ == '__main__':
    main()

###################### End Code ############################

######################## Output ##############################

usrODESKTOP-VUM526N:/mnt/c/EclipseWorkspaces/csse 120/code/src$ python3 MoreCode. py Enter String for Morse Code : ABCDEFGHIJ

Add a comment
Know the answer?
Add Answer to:
Please write code using Python. Rubric Assignment Solution Total: 100 pts Program successfully retrieves and validates...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please make sure all requirements are met and as well have comments to explain the program...

    Please make sure all requirements are met and as well have comments to explain the program throughout. C++ Programming Morse code is a code where each letter of the English alphabet, cach digit, and various punctuation characters are represented by a series of dots and dashes. Table 10-9 shows part of the code. Write a program that asks the user to enter a string, and then converts that string to Morse code. nai The und Character Code Character Code Code...

  • msp430 launchpad 2553 C programming Write a program for the microcontroller that flashes the Morse code pattern of a string. The Morse code is a system used to transmit characters and numbers through...

    msp430 launchpad 2553 C programming Write a program for the microcontroller that flashes the Morse code pattern of a string. The Morse code is a system used to transmit characters and numbers through light or sound signals. Each character is mapped to a series of ‘dots’ and ‘dashes’ as shown below. For example, the letter A is (dot-dash), the letter B is (dash-dot-dot-dot) etc. To show a Morse letter on the microcontroller, the LED blinks for a short duration to...

  • Program Description: A Java program is to be created to produce Morse code. The Morse code...

    Program Description: A Java program is to be created to produce Morse code. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon, and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented...

  • Program Description: A C# app is to be created to produce Morse code. The Morse code...

    Program Description: A C# app is to be created to produce Morse code. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon, and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented...

  • Write a program(Python language for the following three questions), with comments, to do the following:   ...

    Write a program(Python language for the following three questions), with comments, to do the following:    Ask the user to enter an alphabetic string and assign the input to a variable str1. Print str1. Check if str1 is valid, i.e. consists of only alphabetic characters. If str1 is valid Print “<str1> is a valid string.” If the first character of str1 is uppercase, print the entire string in uppercase, with a suitable message. If the first character of str1 is...

  • Python code Write a program that will ask the user to input two strings, assign the...

    Python code Write a program that will ask the user to input two strings, assign the strings to variables m and n If the first or second string is less than 10 characters, the program must output a warning message that the string is too short The program must join the strings m and n with the join function The output must be displayed. Please name the program and provide at least one code comment (10)

  • QUESTION 6 Please match questions (definitions) with answers - A computer designed to be used by...

    QUESTION 6 Please match questions (definitions) with answers - A computer designed to be used by one person at a time, commonly connected to a LAN and run multi-user operating systems - Main residential program that coordinates all hardware and software activity and provide interface for User (GUI). A collection of information stored as one unit with one name. 1. Algorithm 2. File 3. Operating System 4. End User workstation - Sequence of connected instructions that describes behavior of any...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set...

    PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set consists primarily of numerous, long runs of repeated characters. For example, AAAAAAAAAA is a run of 10 A’s. We could encode this run using a notation like *A10, where the * is a special flag character that indicates a run, A is the symbol in the run, and 10 is the length of the run. As another example, the string KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG would be encoded...

  • QUESTION 6 15 marks In this question you have to write a C++ program to convert...

    QUESTION 6 15 marks In this question you have to write a C++ program to convert a date from one format to another. You have to write a complete program consisting of amain () function and a function called convertDate(). The function receives a string of characters representing a date in American format, for example December 29, 1953. The function has to convert this date to the international format. For example: If the string December 29, 1953 is received, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT