Question

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), along with either a list comprehension or recursion. In either case, you should put the helper function in your ps3pr2.py file, just above the definition of your function for this problem. Note: You don’t need to worry about cases in which two or more words are tied for the most consonants.

The helper function that must be used is below:

def num_vowels(s):
    """ returns the number of vowels in the string s
        input: s is a string of 0 or more lowercase letters
    """
    if s == '':
        return 0
    else:
        num_in_rest = num_vowels(s[1:])
        if s[0] in 'aeiou':
            return 1 + num_in_rest
        else:
            return 0 + num_in_rest
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#CODE

def num_vowels(s):
""" returns the number of vowels in the string s
input: s is a string of 0 or more lowercase letters
"""
if s == '':
return 0
else:
num_in_rest = num_vowels(s[1:])
if s[0] in 'aeiou':
return 1 + num_in_rest
else:
return 0 + num_in_rest

def most_consonants(arr):
#To handle empty array
if(not arr):
return ""
# #consonants = length - #vowels
maxim = len(arr[0]) - num_vowels(arr[0])
ans = arr[0]
for word in arr[1:]:
temp = len(word) - num_vowels(word)
if(temp > maxim):
maxim = temp
ans = word
return ans

print("most_consonants(['python', 'is', 'such', 'fun']) result:",most_consonants(['python', 'is', 'such', 'fun']))
print("most_consonants(['oooooooh', 'i', 'see', 'now']) result:",most_consonants(['oooooooh', 'i', 'see', 'now']))

#OUTPUT

Add a comment
Know the answer?
Add Answer to:
Write a function called most_consonants(words) that takes a list of strings called words and returns the...
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 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 a function findEvens that takes an integer, n, as input and returns the list of...

    Write a function findEvens that takes an integer, n, as input and returns the list of even integers between 1 and n. Ex. Input: 10 Output: [2,4,6,8,10] Write a function sortList that takes in a list of strings and returns a list of those strings now sorted and lowercase. Ex. Input: [‘ABE’,’CAD’,’gaB’] Output: [‘abe’,’acd’,’abg’] Both done in Python

  • 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". The file must be named: words_in_both.py...

  • Write a function valid_integers(strings) that takes a list of strings as a parameter and returns a...

    Write a function valid_integers(strings) that takes a list of strings as a parameter and returns a list of the integers that can be obtained by converting the strings to integers using an expression like int(string). Strings which cannot be converted in this way are ignored. Hint: the statement pass is a statement that does nothing when executed. For example: Test Result strings = ['123', '-39', '+45', 'x', 'COSC121', '123+', '12-3'] print(valid_integers(strings)) [123, -39, 45]

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

  • C++ 1) Write a function named WordCount, which determines the number of words in a “C...

    C++ 1) Write a function named WordCount, which determines the number of words in a “C type” string. The function will have one parameter (the address of the string) and will return the number of words (words are separated by one or more spaces). (15 points) 2) Write a function named VowelConsonant which will have three parameters - all “C type” strings. The function will place all vowels (the characters a, e, i, o, u) from the first string into...

  • Write a function countNames(words) that returns the number of capitalized names in the list of strings...

    Write a function countNames(words) that returns the number of capitalized names in the list of strings named words. For example, countNames(['Fili', 'Oin', 'Thorin', 'and', 'Bilbo', 'are', 'characters', 'in', 'a', 'book', 'by', 'Tolkien']) should return 5.

  • language is python Write a function named subsetStrings that has two inputs, a list of strings...

    language is python Write a function named subsetStrings that has two inputs, a list of strings and an integer n. Your function should use a single list comprehension to create a new list containing the first n characters of each string in the input list if the length of the individual string is at least n (strings that are too short should be skipped). Your function should return the new list ex: inputList 'Frederic, 'powerade', 'spring break, 'pen'] and n-4...

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

  • I am using Python 3.5.2 2. Create a function called is_interesting that, given a string, returns...

    I am using Python 3.5.2 2. Create a function called is_interesting that, given a string, returns the Boolean value True if the number of vowels (a e i o u) in the string is a prime number (assume that the strings are always passed as lower-case letters to simplify). The function header should be as follows: def is_interesting(a_string) For example is_interesting(“yien”) returns True, is_interesting(“wang”) returns False

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