IT 117: Intermediate Scripting
Answers to Questions on Final Exam - Odd Columns

Each of the following questions are worth 3 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. What is the name of the module that allows you to interact with the operating system?
    os
  5. Write the Python expression you would use to get a list of all entries in your current directory.
    os.listdir('.')
  6. Write a Python expression to see if the set s1 contains 5.
    5 in s1
  7. What are three things are found in a regular expression?
    ordinary characters, meta-characters, character classes
  8. What is a greedy match?
    a match with the greatest number of characters
  9. To create a new kind of object, what must you define?
    a class
  10. What do you call the special method that is used to create an object?
    constructor
  11. In Python what must the special method used to create an object be named?
    __init__
  12. What do you call an object that is created from a class?
    an instance
  13. What does Python call the process by which an object can be stored as a file on disk?
    pickling
  14. What access mode would you use to open a pickled file for reading?
    rb
  15. What do you call the special class of methods that do things like provide a string representation of an object and convert one data type into another?
    magic methods
  16. What is the name of the method that will return a string representation of an object?
    __str__
  17. What must a recursive algorithm have in order to work?
    a condition under which the recursion will stop
  18. What do call the situation where a function calls itself directly?
    direct recursion
  19. What do you call the situation where a function calls another function, which then calls the original function?
    indirect recursion
  20. Can you rewrite any recursive function using a loop instead of recursion?
    yes
The following questions require you to write code.
Each question is worth 10 points.
  1. Write the regular expression which matches each line below and extracts the name and score from each line.
    Joe     86
    Sally   100 
    Ben     78 
    I am looking for a SINGLE regular expression that matches all lines
    (\w+)\s+(\d+)
  2. Define the function file_count, which takes no parameter.
    This function should return the number of FILES, not directories, inside the current directory.
    You will need to use os.path.isfile in this script.
    def file_count():
        count = 0
        entries = os.listdir()
        for entry in entries:
            if os.path.isfile(entry):
                count += 1
        return count
  3. Define the class Book with the following attributes
    title
    author
    publisher
    These attributes must be HIDDEN and have accessors.
    You MUST define a __str__ method.
    class Book:
        def __init__(self, title, author, publisher):
            self.__title     = title
            self.__author    = author
            self.__publisher = publisher
            
        def get_title(self):
            return self.__title
            
        def get_author(self):
            return self.__author
        
        def get_publisher(self):
            return self.__publisher
        
        def __str__(self):
            return self.get_title() + ' by ' + self.get_author() + ', ' + self.get_publisher()
  4. Write the RECURSIVE function fibonacci which has one parameter named number.
    The function should compute the value of the nth Fibonacci number using recursion.
    The Fibonacci numbers are computed as follows
    F(1) = 1
    F(2) = 1
    F(n) = F(n - 1) + F(n - 2)
    def fibonacci(number):
        if number == 1 or number == 2:
            return 1
        else:
            return fibonacci(number - 1) + fibonacci(number - 2)