12.13. Write Code Questions¶
Some of the questions use the data file mbox
Modify the ### to find sequences with one uppercase letter followed by an underscore followed by one or more lowercase letters.
Modify the ### to find sequences with one uppercase letter followed by an underscore followed by one or more lowercase letters.
-
Complete the code below to check if a string begins with a word character. If it does, return “Found a match!”, if not return “Not matched!” Replace the ### with your code.
Define the function “match_four” that takes a string and uses regex to return True if the string starts with 4 followed by zero to many other digits and False if it does not.
Define the function “match_four” that takes a string and uses regex to return True if the string starts with 4 followed by zero to many other digits and False if it does not.
-
Define the function “match_cat” that uses regex to return True if a string matches cat, Cat, CAT, cAt, etc and False if it does not.
Define the function
match_z
to match a word containing a lowercase letterz
. Return"Found a match!"
ifz
is in the string and"Not matched!"
if there is not az
.Define the function
match_z
to match a word containing a lowercase letterz
. Return"Found a match!"
ifz
is in the string and"Not matched!"
if there is not az
.-
Define the function matchMiddle_z to match a word containing the letter z, but not at the beginning or end of a word. Return “Found a match!” if z is in the string and “Not matched!” if there is not a z.
Finish the code below to find all of the emails in “mbox-short.txt” and add them to a list and return the list. An email starts with an alphanumeric character followed by one or more non white space characters and then “@” followed by one or more non white space characters with one alphabetic character at the end.
Finish the code below to find all of the emails in “mbox-short.txt” and add them to a list and return the list. An email starts with an alphanumeric character followed by one or more non white space characters and then “@” followed by one or more non white space characters with one alphabetic character at the end.
-
Using “mbox-short.txt”, extract the email domains from each email address using regex and add them to the list “domains”. For the email noname@umich.edu, this would locate the string “umich.edu”.
Using “mbox-short.txt”, extract the number next to each email (the amount of emails each person has sent) using a regex equation, and calculate the total amount of emails sent. Save this to the variable ‘total_emails’. Use the () character to capture and return the number.
Using “mbox-short.txt”, extract the number next to each email (the amount of emails each person has sent) using a regex equation, and calculate the total amount of emails sent. Save this to the variable ‘total_emails’. Use the () character to capture and return the number.
-
Using “mbox-short.txt”, extract the usernames from each email address using regex and add them to the list “usernames”. For the email noname@umich.edu, this would locate the string “noname”.