Skip to main content

Section 12.13 Write Code Questions

Some of the questions use the data file mbox

Activity 12.13.1.

Modify the ### to find sequences with one uppercase letter followed by an underscore followed by one or more lowercase letters.
Solution.
Modify the ### to find sequences with one uppercase letter followed by an underscore followed by one or more lowercase letters.

Activity 12.13.2.

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.

Activity 12.13.3.

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.
Solution.
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.

Activity 12.13.4.

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.

Activity 12.13.5.

Define the function match_z to match a word containing a lowercase letter z. Return "Found a match!" if z is in the string and "Not matched!" if there is not a z.
Solution.
Define the function match_z to match a word containing a lowercase letter z. Return "Found a match!" if z is in the string and "Not matched!" if there is not a z.

Activity 12.13.6.

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.
The following file is used in the last four problems. It has an email on each line followed by a space and then a number.

Activity 12.13.7.

Finish the code below to find all of the emails in โ€œmbox-short.txtโ€ and add them to a list email_list and then print 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.
Solution.
Finish the code below to find all of the emails in โ€œmbox-short.txtโ€ and add them to a list email_list and then print 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.

Activity 12.13.8.

Using โ€œmbox-short.txtโ€, extract the email domains from each email address using regex and add them to the list domains. For the email [email protected], this would locate the string โ€œumich.eduโ€.

Activity 12.13.9.

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 in the variable total_emails. Use () to capture and return the number.
Solution.
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 in the variable total_emails. Use () to capture and return the number.

Activity 12.13.10.

Using โ€œmbox-short.txtโ€, extract the usernames from each email address using regex and add them to the list usernames. For the email [email protected], this would locate the string โ€œnonameโ€.
You have attempted of activities on this page.