IT 117: Intermediate Scripting - Even Columns
Answers to Mid-term Questions

Each of the following questions are worth 4 points.
  1. What is a greedy match?
    a match with the greatest number of characters
  2. Is the difference between the sets A and B the same as the difference between the sets B and A
    no
  3. Write the Python statement you would use to stop execution of a script.
    sys.exit()
  4. Can dictionary entries be accessed by their position?
    no. entries in a dictionary have no definite position
  5. What regular expression would you write if you wanted to match ONE occurrence of either the character "x", or "y", or "z"?
    [xyz]
  6. What three things are found in a regular expression?
    ordinary characters, meta-characters, character classes
  7. What are the entries in a dictionary?
    key - value pairs
  8. Write a Python expression to see if the set s1 contains 5.
    5 in s1
  9. Write the Python statement you would use to change your current directory to your parent directory.
    os.chdir('..')
  10. Can a value appear more than once in a set?
    no
  11. If we have the sets A and B with the elements below, what are the elements formed by the intersection of A and B?
    A = {1, 2, 3}
    B = {3, 4, 5}
    {3}
  12. What string value does Python think of as False in an if statement?
    the empty string
  13. Write the Python expression you would use to get the value of your PATH variable.
    os.environ['PATH']
  14. Write the Python statement to create an empty set named s1.
    s1 = set()
  15. What integer value does Python think of as True in an if statement?
    anything other than 0
The following questions require you to write code.
Each question is worth 10 points.
  1. 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.
    The function should return the number of directories it finds.
    You do not have to check if the path actually points to a directory.
    def dir_count(path):
        os.chdir(path)
        total_dirs = 0
        entries = os.listdir()
        for entry in entries:
            if os.path.isdir(entry):
                total_dirs += 1
        return total_dirs
  2. 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.
    def temps_set_create(file):
        temps = set()
        for line in file:
            fields = line.split()
            temps.add(fields[1])
        return temps
  3. Define the function temps_dict_create which takes a file object as its only parameter.
    The function should read in a file with entries like this
    1/1/2021 38
    1/2/2021 39
    1/3/2021 40
    ...
    It should use these values to create a dictionary where the keys are dates and the values are temperatures.
    It should then return this dictionary.
    def temps_dict_create(file):
        temps = {}
        for line in file:
            date, temp = line.split()
            temps[date] = temp
        return temps
  4. Each line below has a username and the name of the person associated with the username.
    Write a regular expression that will match lines like the ones below and extract the username and full name.
    gsmithers   George Smithers
    dblack      David Black
    adams       Samantha Adams
    I am looking for a SINGLE regular expression that matches all lines.
    (\w+)\s+(.+)