CS8—Final Exam
E03, 10M, Phill Conrad, UC Santa Barbara
09/08/2010

Name: ________________________________________________________


Umail Address: __________________________________@ umail.ucsb.edu

Please write your name only on this page. That allows me to grade your exams without knowing whose exam I am grading.

This exam is closed book, closed notes, closed mouth, cell phone off,
except for:

There are 100 points worth of questions on the exam, and you have 85 minutes to complete the exam. I don't expect you to need all of that time, though you are welcome to use it if you do.

A hint for allocating your time—on your first pass through the exam:

If you do that, you'll complete your exam in 50 minutes. You'll then have another 35 minutes to:


  1. (20 pts) Write a function called sumPositive. The function should take one parameter called theList. If the value passed in for theList is not of type list, the function should return the boolean value False. Otherwise, the function should examine each item on the list, and add up all the items that are positive integers, and return that sum as the result of the function.

    Note that for this problem it is not an error for the list to contain things that are not integers—it is only an error if the value of the parameter is not a list.

    See the test cases below for more clarification of how the function should operate.

    For purposes of this this exam, is is not required to put a "docstring" or header comment on your function.

    (Hints/Reminders: the Python type for integer is int, and the Python built-in function type returns the type of its argument.).

    check_expect("sumPositive test1",sumPositive([]),0)
    check_expect("sumPositive test2",sumPositive([1,2,3]),6)
    check_expect("sumPositive test3",sumPositive(['A','B','C']),0)
    check_expect("sumPositive test4",sumPositive([1,'A',2,3]),6)
    check_expect("sumPositive test5",sumPositive(['A',2,-3,-4,5]),7)
    check_expect("sumPositive test6",sumPositive([2,3,-4,5,'1']),10)
    check_expect("sumPositive test7",sumPositive(1),False)
    You do not need to write the check_expect function, or recopy the test cases.


  2. (15 pts) As part of lab08, you were asked to replace a stub for removeDups() with a correct implementation that passes the test cases given at the bottom of this page.

    What appears below is an incorrect implementation—it passes some of the test cases, but does not pass the others.

        
    def removeDups(theList):
        """
        returns a list in which all duplicate elements in theList have been removed
    
        if thelist is not a list, return an empty list []
        """
        
        if (type(theList)!=list or theList==[]):
          return False
    
        acc = [theList[0]]
    
        for i in range(1,len(theList)):
            if theList[i]!=theList[i-1]:
               acc.append(theList[i])
    
        return acc    
    
    Your job: for each of the test cases shown, indicate whether the test passes or fails by placing a check either in the passed column, or the failed column.

    You earn 3 points for each correct answer.

    Test case Passed Failed
    check_expect("Test 1",
                 removeDups(['This','is','a','test']) ,
                 ['This','is','a','test'])
       
    check_expect("Test 2",removeDups([]) ,[])
       
    check_expect("Test 3",
                 removeDups(['This','This','is','is','a','a','test','test']),
                 ['This','is','a','test']) 
       
    check_expect("Test 4",removeDups([1,2,3,4,4,3,2,1]) ,[1,2,3,4])    
    check_expect("Test 5",removeDups([1,2,3]) ,[1,2,3])
       


  3. (20 pts) This is a simple "matching" style problem.

    As part of lab06, you were asked to replace a stub for isListOfIntegers() with a correct implementation that passed various tests. As a reminder, isListOfIntegers() is a function that takes one parameter, and returns True if that parameter is a list consisting only of integers—or if it is an empty list.

    Below are four versions of isListOfIntegers—versions 1,2,3,4. They are identical except for the lines highlighted in bold. Only one of the four versions is correct—the other four are wrong (in different ways).

    The test cases output for these four versions are provided on the handout that comes along with this exam but they've been reordered and labeled with A, B, C and D.

    Your job—match the output to the implementation. You earn 5 pts for each correct match.

    A hint: as you know, indentation in Python is crucial to what the code means.

    Enter your answers here (just a letter A,B,C, or D beside each number)

    1) _____ 2) ______ 3) ______ 4 _______

    1

    def isListOfIntegers(theList):
        """
        indicates whether theList contains only int
        Note: empty list should return True   
        """
        if (not isList(theList)):
          return False
        
        for item in theList:
          if not type(item)==int:
             return False
    
        return True                 
                   

    2

    def isListOfIntegers(theList):
        """
        indicates whether theList contains only int
        Note: empty list should return True   
        """
        if (not isList(theList)):
          return True
        
        for item in theList:
          if not type(item)==int:
             return True
    
        return False                 
                   

    3

    def isListOfIntegers(theList):
        """
        indicates whether theList contains only int
        Note: empty list should return True   
        """
        if (not isList(theList)):
          return False
        
        for item in theList:
          if not type(item)==int:
               return False
          return True
                   

    4

    def isListOfIntegers(theList):
        """
        indicates whether theList contains only int
        Note: empty list should return True   
        """
        if (not isList(theList)):
          return False
        
        for item in theList:
          if not type(item)==int:
               return True
          return False
                   




  4. What is the output of each of the following when typed at the Python prompt?


    1. (3 pts)
      >>> 3 + 4



    2. (3 pts)
      >>> "3" + "4"



    3. (3 pts)
      >>> '3' + '4'



    4. (3 pts)
      >>> [3] + [4]



    5. (3 pts)
      >>> 3 % 4



    6. (3 pts)
      >>> 3 / 4



    7. (3 pts)
      >>> 3 // 4



    8. (3 pts)
      >>> 4 // 3



  5. Throughout the course, we've been using various Unix commands to manipulate files and directories.

    1. (2 pts) What unix command can you type to show the current directory that you are working in?


    2. (2 pts) Sometimes in the labs, there is an instruction to create a directory such as ~/cs8/lab04
      What is the significance of the ~ symbol in this directory name?

      (Note: I'm not asking what its called—its called a tilde, or informally a squiggle.
      I'm asking you what it means—what it stands for.)


    3. (2 pts) What is the Unix command used to create the directory ~/cs8/lab04


    4. (2 pts) Suppose you meant to saved one of your programs as lab04.py, but instead you saved it as lag04.py (the g and b keys are close to each other.)

      As discussed in lecture, what unix command can you type to rename lag04.py, giving it the name you intended?



  6. (5 pts) Throughout the course, we've been testing our functions using the check_expect function.

    What is the method we've been using to test the check_expect function itself to try to make sure it doesn't have any bugs in it?





  7. Some old-school textbooks say to write your code first, and then test it.
    Some software developers are now suggesting instead to write test cases firstbefore writing the code.

    What are two different advantages of this approach (writing the test cases before the code?)

    1. (4 pts) Describe one advantage







    2. (4 pts) Now describe another, different advantage








End of Exam—Total points: ?

Test case output to go with problem 3

A

Running 
test_isListOfIntegers:
   passed: isListOfIntegers('Fred')...
   passed: isListOfIntegers(3)...
 FAILED:isListOfIntegers([3])...  
          check: False expect: True
 FAILED:isListOfIntegers([3.4])...  
        check: True expect: False
   passed: isListOfIntegers([2,3,4,5.6,7])...
   passed: isListOfIntegers([2,3,'oops',5])...
 FAILED:isListOfIntegers([2,3,4,5,6,7])...  
check: False expect: True passed: isListOfIntegers([2,3,[4]])... FAILED:isListOfIntegers([])... check: None expect: True ======================================= Failed: 4 tests

B

Running 
test_isListOfIntegers:
   passed: isListOfIntegers('Fred')...
   passed: isListOfIntegers(3)...
   passed: isListOfIntegers([3])...
   passed: isListOfIntegers([3.4])...
 FAILED:isListOfIntegers([2,3,4,5.6,7])...  
        check: True expect: False
 FAILED:isListOfIntegers([2,3,'oops',5])...  
        check: True expect: False
   passed: isListOfIntegers([2,3,4,5,6,7])...
 FAILED:isListOfIntegers([2,3,[4]])...  
        check: True expect: False
 FAILED:isListOfIntegers([])...  
        check: None expect: True
=======================================
Failed: 4 tests
              

C

Running 
test_isListOfIntegers:
   passed: isListOfIntegers('Fred')...
   passed: isListOfIntegers(3)...
   passed: isListOfIntegers([3])...
   passed: isListOfIntegers([3.4])...
   passed: isListOfIntegers([2,3,4,5.6,7])...
   passed: isListOfIntegers([2,3,'oops',5])...
   passed: isListOfIntegers([2,3,4,5,6,7])...
   passed: isListOfIntegers([2,3,[4]])...
   passed: isListOfIntegers([])...
=======================================
Failed: 0 tests        
              

D

Running 
test_isListOfIntegers:
 FAILED:isListOfIntegers('Fred')...  
        check: True expect: False
 FAILED:isListOfIntegers(3)...  
        check: True expect: False
 FAILED:isListOfIntegers([3])...  
        check: False expect: True
 FAILED:isListOfIntegers([3.4])...  
        check: True expect: False
 FAILED:isListOfIntegers([2,3,4,5.6,7])...  
        check: True expect: False
 FAILED:isListOfIntegers([2,3,'oops',5])...  
        check: True expect: False
 FAILED:isListOfIntegers([2,3,4,5,6,7])...  
        check: False expect: True
 FAILED:isListOfIntegers([2,3,[4]])...  
        check: True expect: False
 FAILED:isListOfIntegers([])...  
        check: False expect: True
=======================================
Failed: 9 tests          
              

Here's the test function:

def test_isListOfIntegers():
    
  print("test_isListOfIntegers:")
  failed = 0
  failed += check_expect("isListOfIntegers('Fred')",isListOfIntegers('Fred'),False)
  failed += check_expect("isListOfIntegers(3)",isListOfIntegers(3),False)
  failed += check_expect("isListOfIntegers([3])",isListOfIntegers([3]),True)
  failed += check_expect("isListOfIntegers([3.4])",isListOfIntegers([3.4]),False)
  failed += check_expect("isListOfIntegers([2,3,4,5.6,7])",isListOfIntegers([2,3,4,5.6,7]),False)
  failed += check_expect("isListOfIntegers([2,3,'oops',5])",isListOfIntegers([2,3,'oops',5]),False)
  failed += check_expect("isListOfIntegers([2,3,4,5,6,7])",isListOfIntegers([2,3,4,5,6,7]),True)
  failed += check_expect("isListOfIntegers([2,3,[4]])",isListOfIntegers([2,3,[4]]),False)
  failed += check_expect("isListOfIntegers([])",isListOfIntegers([]),True)
  return failed