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

Each of the following questions are worth 3 points.

  1. What do you call a function that calls itself?
    a recursive function
  2. What two things must be true about a recursive algorithm for it to work?
    there must be a condition under which recusion will stop
    the recursive call must approach this condition
    			
  3. What do call the situation where a function calls itself directly?
    direct recursion
  4. What do you call the situation where a function calls another function, which then calls the original function?
    indirect recursion
  5. Can you rewrite any recursive function using a loop instead of recursion?
    yes
  6. 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
  7. What is the name of the method that will return a string representation of an object?
    __str__
  8. To create a new kind of object, what must you define?
    a class
  9. What do you call the special method that is used to create an object?
    constructor
  10. In Python what must the special method used to create an object be named?
    __init__
  11. What do you call an object that is created from a class?
    an instance
  12. Every method in a class must take a certain parameter. What is this parameter?
    self
  13. What two kinds of things do you define inside a class?
    variables and methods
  14. Write the regular expression that is equivalent to * on the Unix command line.
    .*
  15. What is a greedy match?
    a match with the greatest number of characters
  16. What are three things are found in a regular expression?
    ordinary characters, meta-characters, character classes
  17. What does the . (dot) in a regular expression match?
    one occurrence of any character, except the newline
  18. What does the + in a regular expression match?
    1 or more occurrences of the character that comes before it
  19. Write the Python expression that gives the value of the first command line argument.
    sys.argv[1]
  20. Write the Python statement you would use to stop execution of a script.
    sys.exit()
The following questions require you to write code.
Each question is worth 10 points.
  1. Write the regular expression you would use to both match and extract the value of an IP address from lines like the following
    205.236.184.22 
    2.2.3.4
    13.26.34.202
    ...
    (\d{1,3}\.\d{1,3}\.\d{1,3})
  2. Define the function python_count that takes a single parameter, dir_path, a path to a directory.
    The script should create a list of the entries in dir_path.
    Then it should loop through this list and count the number of entries with a ".py" extension.
    This should be done using the string method endswith.
    The function should return this count.
    def python_count(dir_path):
        file_list = os.listdir(dir_path)
        count = 0
        for file in file_list:
            if file.endswith('.py'):
                count += 1
        return count
  3. Define the class Vehicle with the hidden fields listed below.
    You must create a constructor, getters for each field and an __str__ method.
    The __str__ should return a string containing the values of each field.
    man 
    model
    year
    class Vehicle:
        def __init__(self, man, model, year):
            self.__man   = man
            self.__model = model 
            self.__year  = year
        
        def get_man(self):
            return self.__man
        
        def get_model(self):
            return self.__model
        
        def get_year(self):
            return self.__year
        
        def __str__(self):
            return self.__man + ", " + self.__model + ", " + self.__year
  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)