12.13. Write Code Questions

Some of the questions use the data file mbox

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

  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.

  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.

    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.

  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.

  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.

    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.

  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.

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

  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 noname@umich.edu, this would locate the string “umich.edu”.

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

  10. 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”.

You have attempted of activities on this page