Question
(1) Implement the function contains that takes as input two strings. The function checks if the second string occurs in the first string. If the
(1) Implement the function contains that takes as input two strings. The function checks if the second string occurs in the first string. If the second string does occur, the function returns True; if not, it returns False. Here are two assert statements to check your code, but you should include at least two more:
assert contains('banana', 'ana')
assert not contains('racecar', 'ck')
[Note: It's possible to code this with one simple expression in the return statement.]
(2) Implement the function sentence_stats that takes in a string as a parameter and prints out these statistics about the string: its length in characters, its length in words, and the average length of each word. (We'll say that a word is any substring that's delimited by white space, after punctuation has been eliminated. You might find it helpful to write a function that translates all punctuation marks to blanks.)
>>> sentence_stats('I love UCI')
Characters: 10
Words: 3
Average word length: 2.66666666665
>>> sentence_stats('***The ?! quick brown fox: jumps over the lazy dog.')
Characters: 52
Words: 9
Average word length: 3.888888888888889
[Note: The two examples above show expressions (calls to sentence_stats()) evaluated in the Python Shell window, which you can tell by the >>> prompt. You can do this if you've previously run the .py file containing the definition of sentence_stats() (or if you've typed that definition into the Python Shell window by hand, which isn't recommended because there's no easy way to edit out any typos). You could also produce the same results by defining and calling the function in your .py file and then running that file. (We're not using assert statements here because sentence_stats() has side effects, namely printing.)]
(3) Implement the function initials that takes as input a string representing a full name (e.g., Robert B. Qwerty) and returns the initials of the name in all capital letters (e.g., RBQ).
assert initials('Bill Cody') == 'BC'
assert initials('Guido van Rossum') == 'GVR'
assert initials('alan turing') == 'AT'
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started