Skip to main content

Section 12.11 Mixed-Up Code Questions

Checkpoint 12.11.1.

Create a function called greeting_search that takes in a list of strings, lst, as a parameter and returns True if the list has “Good Morning!” in it and False otherwise. For example, greeting_search(["Hello!", "Good Morning!", "good morning!"]) should return True.

Checkpoint 12.11.2.

Create a function called search_h that takes in a list of strings, lst, and uses regular expressions to add all the words starting with an “H” or an “h” to a new list and returns that new list. For example, search_h(["Hello!", "hello.", "Morning!", "hi"]) should return ['Hello!', 'hello.', 'hi'].

Checkpoint 12.11.3.

Create a function called search_o that takes in a list of strings, lst, and uses regular expressions to add all the words that have a lowercase “o” to a new list and returns that new list. For example, search_o(['Hello', 'Good Morning!', 'hi']) should return ['Hello', 'Good Morning!'].

Checkpoint 12.11.4.

Create a function called match_word(word, str) that returns True if the string str contains the word word, but not if it is part of another word. For example, match_word('is', "This was bad") would return False and match_word('is', "This is good") would return True.

Checkpoint 12.11.5.

Create a function called test_pattern that takes in one string parameter, sequence, and uses regular expressions to see if sequence has the pattern of at least one letter, number, or underscore, at least one space, and at least one letter, number, or underscore again. Return "Match!" if sequence matches the pattern, and "Not a match!" otherwise. For example, test_pattern('Sincerely1 Molly') should return "Match!".

Checkpoint 12.11.6.

Create a function called first_price that takes in a parameter string and uses regular expressions to find and return the first price in the string if there is one. A price must have at least one digit after a ‘$' symbol and can optionally have a period followed by two digits. If there isn't a price in the string, return 'No price'. For example, first_price('We just received $2098.10 for cookies and $209 for brownies.') should return "$2098.10".

Checkpoint 12.11.7.

Create a function called search_email that takes in a string parameter, string, and uses regular expressions to return a list with all the emails in the string. The format for the email is at least one letter, number or underscore, the “@” symbol, and the email domain (which includes a period). For example, search_email('His email is pyth_on@umich.edu and her email is java@css.') should return ['pyth_on@umich.edu'].

Checkpoint 12.11.8.

Create a function called start_from` that takes in a list of strings, lst, and adds into a new list each item that starts with “From:” and is followed by one or more characters and an “@” sign. For example, start_from(['From: Kelly@umich.edu','From: Kelly@',': Kelly@']) should return ['From: Kelly@umich.edu', 'From: Kelly@'].

Checkpoint 12.11.9.

Create a function called grab_domain that takes in a list of strings, lst, as a parameter and returns a new list with the domains of the emails, if they exist. For example, grab_domain(['python@umich.edu','This is Kelly@umich.gov','123','java@css','jav12a@css.com']) would return ['umich.edu', 'umich.gov', 'css.com'].

Checkpoint 12.11.10.

Create a function called vowels_in_mid that takes in one string parameter, string, and returns string if there is an area in the string that contains two to four vowels, and it does not start nor end with a vowel. Otherwise, return 'Does not exist'. For example, vowels_in_mid('chEEEEEYYEErry') should return 'chEEEEEYYEErry'.