IT 117: Intermediate Scripting - Odd 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. Can the key in a dictionary entry be any data type?
    no. you can only use values that are immutable, that cannot be changed
  3. What string value does Python think of as False in an if statement?
    the empty string
  4. Can a value appear more than once in a set?
    no
  5. 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}
  6. 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}
  7. Write a Python statement to create the set s2 containing the values 1, 2 and 3, using a set literal.
    s2 = {1, 2, 3}
  8. Write a statement that adds 5 to the set s1.
    s1.add(5)
  9. Write the Python expression you would use to get a list of all entries in your current directory.
    os.listdir('.')
  10. Write the Python expression that gives the value of the first command line argument.
    sys.argv[1]
  11. What are three things are found in a regular expression?
    ordinary characters, meta-characters, character classes
  12. What does the . (dot) in a regular expression match?
    one occurrence of any character, except the newline
  13. What does the * in a regular expression match?
    0 or more occurrences of the character that comes before it
  14. What does the + in a regular expression match?
    1 or more occurrences of the character that comes before it
  15. What is a greedy match?
    a match with the greatest number of characters
The following questions require you to write code.
Each question is worth 10 points.
  1. Define the function temps_dict_create which takes file, 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.
    You should use the split method on each line to get the date and temperature.
    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
  2. Define a function set_create, which takes file, a file object, as its only parameter.
    The file contains words, one to a line.
    The function should read in each line and strip the line feed.
    The file looks like this
    atom
    barn
    disk
    storm
    It should then add the word to a set.
    After reading all the words in a file, it should return the set.
    def set_create(file):
        s = set()
        for word in file:
            s.add(word.strip())
        return s
  3. 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
  4. Write the regular expression which will match each line below.
    It should also extract the score and full name from each line.
    Assume there are no whitespace characters after the name.
    100    Bill Smith
    85     Jane Doe
    9      John Parker
    I am looking for a SINGLE regular expression that matches all lines.
    "(\d{1,3})\s+(.+)"