IT 117: Intermediate Scripting - Even Columns
Answers to Midterm Questions

Each of the following questions are worth 4 points.
  1. Write the Python expression you would use to get the pathname of your current directory.
    os.getcwd()
  2. What three things are found in a regular expression?
    ordinary characters, meta-characters, character classes
  3. Write the Python statement to create an empty set named s1.
    s1 = set()
  4. If we have the sets A and B with the elements below, what are the elements formed by the union of A and B?
    A = {1, 2, 3}
    B = {3, 4, 5}
    {1, 2, 3, 4, 5}
  5. Write the Python expression you would use to get the number of entries in a dictionary named students.
    len(students)
  6. Can a value appear more than once in a set?
    no
  7. Can the key in a dictionary entry be any data type?
    no. you can only use values that are immutable, that cannot be changed
  8. What regular expression would you write if you wanted to match a SINGLE lowercase character?
    [a-z]
  9. Write a Python expression to see if the set s1 DOES NOT contain 12.
    12 not in s1
  10. What string value does Python think of as True in an if statement?
    anything that is not the empty string
  11. What does the \ do in a regular expression?
    turns off the special meaning of the character that comes after it
  12. Write the Python expression that gives the value of the first command line argument.
    sys.argv[1]
  13. Write a Python expression that returns the largest element in the set A.
    max(A)
  14. What is the name of the module that allows you to interact with the operating system?
    os
  15. What are the entries in a dictionary?
    key - value pairs
The following questions require you to write code.
Each question is worth 10 points.
  1. Write the regular expression which will match each line in the scores file below.
    It should also extract the values for the full name and the score.
    Bill Smith   100
    Jane Doe      85     
    John Parker    9
    I am looking for a SINGLE regular expression that matches all lines
  2. Define the function python_files_count which takes a directory pathname as its only parameter.
    The function should go the directory and count the number of files with a .py extension.
    It should return that count.
    def python_files_count(dir_path):
        count = 0
        os.chdir(dir_path)
        for entry in os.listdir():
            if ".py" in entry:
                count += 1
        return count	
  3. Define a function named common_emails.
    This function has two parameters, both of them file objects.
    The file objects point to files containing email addresses.
    The function should create a set of email addresses for each file.
    It should return a set of the email addresses which are found in both files.
    Be sure to remove linefeed characters when creating the sets of email addresses.
    def common_emails(file_1, file_2):
        emails_1 = set()
        for line in file_1:
            emails_1.add(line.strip())
        emails_2 = set()
        for line in file_2:
            emails_2.add(line.strip())
        return emails_1.intersection(emails_2)
  4. Define the function username_name_create which takes file a file object as its only parameter.
    The function should read in a file with entries like this
    wombat    James Smith
    helio     Mongo Fleet
    gormo     Jane Jones
    ...
    It should use these values to create a dictionary where the keys are usernames and the values are full names.
    The full name should be the first name, a space and the last name.
    It should then return this dictionary.
    def username_name_create(file):
        dict = {}
        for line in file:
            username, first_name, last_name = line.split()
            dict[username] = first_name + " " + last_name
        return dict