Question

Write a function called longest_morse_code_words which takes in a list of strings. The output of longest_morse_code_words...

Write a function called longest_morse_code_words which takes in a list of strings. The output of longest_morse_code_words should be a list of the strings that were passed in, but ordered by the length of their Morse Code equivalent in descending order. If the length of Morse Code is equal, order the words alphabetically. For convenience, the full table for the 26 letters of the English alphabet is given below:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

Here's an example:

words = ["gin", "zen", "gig", "msg"] longest_morse_code_words(words) # The Morse Code equivalent of the words above is ["--...-.", "--...-.", "--...--.", "--...--."]. Ordering by length, it's [7, 7, 8, 8]. When we alphabetize based off of length in descending order, the output should be # ['gig', 'msg', 'gin', 'zen']
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:


codes = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

words = ["gin", "zen", "gig", "msg"]


def longest_morse_code_words(words):
result = []
for word in words:
code = ""
for ch in word:
code = code + (codes[ord(ch)-97])
result.append(code)
print(result)
  
len_result=[]
for i in result:
len_result.append(len(i))
  
sorted_list = [words for _,words in sorted(zip(len_result,words), reverse=True)]
  
print(sorted_list)

longest_morse_code_words(words)

Strings are printed in descending order of codes according to length of codes

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

Add a comment
Know the answer?
Add Answer to:
Write a function called longest_morse_code_words which takes in a list of strings. The output of longest_morse_code_words...
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
  • Write a function called most_consonants(words) that takes a list of strings called words and returns the...

    Write a function called most_consonants(words) that takes a list of strings called words and returns the string in the list with the most consonants (i.e., the most letters that are not vowels). You may assume that the strings only contain lowercase letters. For example: >>> most_consonants(['python', 'is', 'such', 'fun']) result: 'python' >>> most_consonants(['oooooooh', 'i', 'see', 'now']) result: 'now' The function that you write must use a helper function (either the num_vowels function from lecture, or a similar function that you...

  • write a function firstLetterWords(words) that takes as a parameter a list of strings named words and...

    write a function firstLetterWords(words) that takes as a parameter a list of strings named words and returns a dictionary with lower case letters as keys. But now associate with each key the list of the words in words that begin with that letter. For example, if the list is ['ant', 'bee', 'armadillo', 'dog', 'cat'], then your function should return the dictionary {'a': ['ant', 'armadillo'], 'b': ['bee'], 'c': ['cat'], 'd': ['dog']}.

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Define a function called max_association(), which recieves 2 lists, lst1, which is a list of strings,...

    Define a function called max_association(), which recieves 2 lists, lst1, which is a list of strings, and lst2, which is a list of integers. The function must return a list of strings associated to the highest integers in lst1. Assume both lists have the same length As an example, the following code fragment: L1 = ['a','b','c','d','e','f','g','h','i','j'] L2 = [1,2,3,1,3,2,3,1,2,1] result = max_association(L1,L2) print(result) should produce the output: ['c', 'e', 'g']

  • PYTHON define a function called fav_colours(): which receives a list of strings of lowercasecolours: the list...

    PYTHON define a function called fav_colours(): which receives a list of strings of lowercasecolours: the list some words will repeat, one colour or many, long list or short, Will always be lowercase - return a list of lists containing unique colours and integer numbers describing the amount of times those colours repeated As an example, the following code fragment: colours = ['blue', 'red', 'red', 'red', 'green'] colours , numbers = fav_colours(colours) print(colours) print(numbers) should produce the output: ['blue', 'red', 'green']...

  • More Practice: Writing Lists& Strings . Write a function average_ evens that takes a list of...

    More Practice: Writing Lists& Strings . Write a function average_ evens that takes a list of numbers as parameters, and adds all the even numbers together and returns their average using a list Example function call: print (average_evens (I-2, -3, 4, 0, 1, 2,3 Outputs: 0 Write a function match that takes two strings as a parameter and returns how many letters the strings have in common. You should treat upper and lower case letters as the same letter ('A,...

  • solve with python Write a recursive function recStringWithLenCount() that takes a one-dimensional list of strings as...

    solve with python Write a recursive function recStringWithLenCount() that takes a one-dimensional list of strings as a parameter and returns the count of strings that have at least the length of second parameter passed into the function that are found in the list. Recall that you can determine whether an item is a string by writing type(item) == str. The only list functions you are allowed to use are len(), indexing (lst[i] for an integer i), or slicing (lst[i:j] for...

  • Write a function named words_in_both that takes two strings as parameters and returns a set of...

    Write a function named words_in_both that takes two strings as parameters and returns a set of the words contained in both strings. You can assume all characters are letters or spaces. Capitalization shouldn't matter: "to", "To", "tO", and "TO" should all count as the same word. The words in the set should be all lower-case. For example, if one string contains "To", and the other string contains "TO", then the set should contain "to". You can use Python's split() funciton,...

  • 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...

  • 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...

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