IT 117: Intermediate Scripting
Answers to Mid-term Questions

Each of the following questions are worth 4 points.
  1. Write the Python statement to create an empty dictionary named students.
    students = {}
  2. Write the Python statement to create an entry in the students for a student with student ID '0456234' whose name is 'John Smith'.
    students['0456234'] = 'John Smith'
  3. Write the Python expression you would use to test whether the students dictionary does not contain an entry for a student with the ID '0497234'.
    '0497234' not in students
  4. Write the Python statement you would use to delete the entry in the students dictionary with key value '0456234'.
    del students['0456234']
  5. Can you use lists as keys in a dictionary?
    no. you can only use things that are immutable, that cannot be changed
  6. Can a value appear more than once in a set?
    no
  7. Write the Python statement to create an empty set named s1.
    s1 = set()
  8. Write a Python statement to create the set s2 containing the values 1, 2 and 3, using a set literal.
    s2 = {1, 2, 3}
  9. Write the Python statement you would use to change your current directory to your parent directory.
    os.chdir('..')
  10. What does the + in a regular expression match?
    1 or more occurrences of the character that comes before it
  11. What does the ? in a regular expression match?
    0 or 1 occurrences of the character that comes before it
  12. What meta-characters do you use to surround a part of a regular expression that you want to extract from the matched string?
    ( )
  13. What method on a match object is used to extract the value of a string in a matched line using the meta-characters mentioned above?
    group
  14. What regular expression would you write if you wanted to match a single lowercase character?
    [a-z]
  15. What regular expression would you write if you wanted to match anywhere between 3 and 5 occurrences of the letter "q"?
    q{3,5}
The following questions require you to write code.
Each question is worth 10 points.
  1. Write a regular expression which will match a line with an IPv4 address somewhere in the line. An IPv4 address has three octal numbers, which run from 0 to 7, each one containing one, two or three digits, each one followed by a . except the last.
    .*[0-7]{1,3}\.[0-7]{1,3}\.[0-7]{1,3}\.[0-7]{1,3}
  2. Write an if statement that checks to see that the user has run the script with at least one command line argument. If the user does not, the script should print a usage message and quit. The usage message should contain the name of the script and should indicate that an argument FILENAME is required. Be sure to import any modules you need to do this.
    import sys
    import os.path
    
    if len(sys.argv) < 2:
        print("Usage:", os.path.basename(sys.argv[0]) , "FILENAME")
        sys.exit()
  3. Write the function word_count which takes a file object as its only parameter. This function should create a word frequency dictionary from the file where each key is a word in the file, and each value is the number of times that word appeared in the file. The function should return the dictionary it created. Assume that each line in the file only has one word.
    def word_count(file):
        words = {}
        for line in file:
            word = line.strip()
            if word in words:
                words[word] += 1
            else:
                words[word] = 1
        return words
  4. Write the function unique_words which takes a file object as its only parameter. This function should create a set of unique words in the file and return this set when it is done. Assume that each line in the file only has one word.
    def unique_words(file):
        words = set()
        for line in file:
            word = line.strip()
            words.add(word)
        return words