Question: 1. Write a function called most_consonants(words) that takes a list of strings called wordsand returns the string in the list with the most consonants (i.e.,
1. Write a function called most_consonants(words) that takes a list of strings called wordsand 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']) 'python' >>> most_consonants(['oooooooh', 'i', 'see', 'now']) 'now'
The function that you write should use the num_vowels function from lecture as a helperfunction, along with either a list comprehension or recursion. Copy num_vowels into your ps3pr2.py file, adjusting the indentation as needed.
Note: You don't need to worry about cases in which two or more words are tied for the most consonants.
The final function of this problem does not require the use of either recursion or a list comprension. Rather, it will allow you to practice using conditional logic and variable assignment to gradually build up a return value.
You will end up with something that looks like this:
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(words): """ your docstring goes here """ ## within your function, call num_vowels as needed!
2.Write a function price_string(cents) that takes as input a positive integer centsrepresenting a price given in cents, and that constructs and returns a string in which the price is expressed as a combination of dollars and cents.
In general, the format of the returned string should be 'd dollars, c cents', where d is the whole number of dollars in the price and c is the remaining cents. For example:
>>> price_string(452) '4 dollars, 52 cents' >>> price_string(871) '8 dollars, 71 cents'
However, there are two additional rules that you must observe:
If either component of the price is 0, it should be omitted:
>>> price_string(27) '27 cents' >>> price_string(300) '3 dollars'
(Because we assume that the input cents is positive, it will never be the case that both of the components are 0!)
If either component of the price is 1, you should omit the 's' from the word for that component:
>>> price_string(201) '2 dollars, 1 cent' >>> price_string(117) '1 dollar, 17 cents' >>> price_string(101) '1 dollar, 1 cent'
Important Guidelines
You should begin by copying the following template into your file:
def price_string(cents): """ docstring goes here """ d = _______________ # compute whole number of dollars c = _______________ # compute remaining cents price = '' # initial value of the price string ## add code below to build up the price string return price
You should begin by replacing the two blanks with expressions that will compute the values of d (the whole number of dollars) and c (the remaining cents), based on the value of the parameter cents.
Next, you should add code to determine the appropriate value of the variable price, which is the string that the function will return. We have given you a line assigns an empty string to price as its initial value. You should use conditional execution and string concatenation to gradually build up the final value of this variable. You will need to use assignment statements that look like this:
price = price + ...
You can use the built-in str() function to turn numbers into strings. For example, str(d) will turn the number represented by the variable d into a string.
Your function must limit itself to the single return statement that we have given you at the very end of the function. You will lose points if you use more than one returnstatement in this function.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
