12.12. Write Code Questions

Some of the questions use the following data file.

Data file: mbox-short4.txt
gopal.ramasammycook@gmail.com 1
louis@media.berkeley.edu 3
cwen@iupui.edu 5
antranig@caret.cam.ac.uk 1
rjlowe@iupui.edu 2
gsilver@umich.edu 3
david.horwitz@uct.ac.za 4
wagnermr@iupui.edu 1
zqian@umich.edu 4
stephen.marquard@uct.ac.za 2
ray@media.berkeley.edu 1
  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-short4.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-short4.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-short4.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-short4.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-short4.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-short4.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