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

Each of the following questions are worth 4 points.
  1. What are the entries in a dictionary?
    key - value pairs
  2. Write the Python expression that gives the value of the first command line argument.
    sys.argv[1]
  3. What is the name of the module that allows you to interact with the Python interpreter?
    sys
  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. What is the name of the module that allows you to interact with the operating system?
    os
  6. What does the . (dot) in a regular expression match?
    one occurrence of any character, except the newline
  7. Can a value appear more than once in a set?
    no
  8. Write the Python expression you would use to get a list of all entries in your current directory.
    os.listdir('.')
  9. What string value does Python think of as False in an if statement?
    the empty string
  10. Write a Python expression to see if the set s1 contains 5.
    5 in s1
  11. What is a greedy match?
    a match with the greatest number of characters
  12. What does the + in a regular expression match?
    1 or more occurrences of the character that comes before it
  13. What are three things are found in a regular expression?
    ordinary characters, meta-characters, character classes
  14. Write the Python statement you would use to stop execution of a script.
    sys.exit()
  15. Are dictionaries sequences?
    no
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
    (\w+\s\w+)\s+(\d+)
  2. Define a function named dir_count.
    This function has one parameter, a path to a directory.
    The function should go to the directory specified by the path.
    It should then loop through all the entries in the directory and count the number of directories it finds.
    You will need to use the function os.path.isdir().
    The function should return the number of directories it finds.
  3. Define the function temps_set_create which takes a file object as its only parameter.
    The function should read in a file with the minimum and maximum temperatures for each date.
    The file will have entries like this
    1/1/2021    38    41
    1/2/2021    39    45
    1/3/2021    40    55
    ...
    The function should create and return a set of all the MINIMUM temperatures read from this file.
    The minimum temperature is the first temperature on each line.
  4. Define a function named word_frequency.
    This function has one parameter, a file object.
    This function should read in a file with one word on each line, like the lines below.
       zebra
       baseball
       zebra
    The function should return a dictionary where the keys are the words and the values are the number of times the words appear in the file. You should use the following algorithm
    create an empty dictionary
    for each line in the file
       strip the linefeed from the line
       if the line is already in the dictionary:
          get the count for that word
          increment the count by 1
          assign this new value to the word
       else
          make an entry for the word with the value 1
     return the dictionary
    def word_frequency(file):
        words = {}
        for word in file:
            word = word.strip()
            if word in words:
                words[word] += 1
            else:
                words[word] = 1
        return words