CS8, Summer 2010

lab11
not for credit—just practice for the final exam


Goals for this lab

 

Coding practice with codingbat

If you haven't tried them yet, this is a great time to try the exercises at http://codingbat.com/python if you haven't already. These are all great review, and great exam practice.

A look at the Summer 2009 final exam

The final exam from Summer 2009 is available at these links: (html pdf)

Here's an important note:

Reviewing problems 1,2,4,5 from the Summer 2009 exam

Question 1

Here's question 1 from the Summer 2009 Final exam:

  1. (20 pts) Write a function called countInts. The function should take one parameter called theList. If the value passed in for theList is not a list, the function should return the string "error". Otherwise, the function should examine each item on the list, and count the number of items that are integers.

    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 exam, it 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("countInts test1",countInts([]),0)
    check_expect("countInts test2",countInts([1,2,3]),3)
    check_expect("countInts test3",countInts(['A','B','C']),0)
    check_expect("countInts test4",countInts([1,'A',2,3]),3)
    check_expect("countInts test5",countInts(['A',2,3,4,5]),4)
    check_expect("countInts test6",countInts([2,3,4,5,'1']),4)
    check_expect("countInts test7",countInts(1),"error")
    You do not need to write the check_expect function, or recopy the test cases.


My invitation to you: first try writing this function on paper—not on the computer.

Then, once you have your paper version, try typing it in to the computer. Here is a link to a test harness for this problem—i.e. a file that already has the definition of check_expect, a stub, and the test cases all ready to go—all you would need to do is type in your function definition to check your answer:

As you check your answer, check two things:

Once you have written this function so that it compiles and passes the test cases, try to anticipate other similar questions that I might ask. For example, this function asks you to count all the int values in a list. What else might I ask you to do on the exam that would be similar? Here are some examples:

For each of those, you can try to write your own test cases and stub first, by making a new copy of the test harness for this question, and modifying it as needed. Then, try to write the function, first on paper, then checking your answer on the computer.

This is a great way to really learn programming—and to study for the final exam.

Question 2

Here's question 2 from last year's exam. This question is a little different from Question 1—it doesn't ask you to write code, but instead asks you to figure out what some "broken" code would do.

Keep in mind:

  1. (18 pts) On lab07, you were asked to replace a stub 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 onlyEvens(listOfInts):
        """
        returns a list containing only the even numbers from a list of ints
    (I've left out the rest of the docstring to save space on the exam)
    """ if type(listOfInts)!=list: return False newList = [] for item in listOfInts: if type(item)!=int: return newList if listOfInts[0] % 2 == 0: newList = newList + [item] return newList
    Your job: for each of the test cases shown, indicate whether the test passes or fails by placing a check in front of either the word passed or failed. In addition, if the test fails, fill in the blank with what the function returns instead of what was expected.

    The first two are done for you as an example of what I'm looking for.

    Test case For each row, check either the passed or failed column,
    If you check the failed column, fill in the blank
    with what you got instead of the expected value.
    check_expect("onlyEvens test 1",onlyEvens('1'),False)
    ✓_ passed

    ____failed: expected False, but got:


    check_expect("onlyEvens test 2",onlyEvens(['a','b']),False)
    ___ passed

    _✓_ failed: expected False, but got:     [ ]    

    check_expect("onlyEvens test 3",onlyEvens([]),[])
    ___ passed
    ____failed: expected [], but got:

    check_expect("onlyEvens test 4",onlyEvens([1,2,3,4,5]),[2,4])
    ___ passed

    ____failed: expected [2,4], but got:

    check_expect("onlyEvens test 5",onlyEvens([1]),[])
    ___ passed

    ____failed: expected [], but got:

    check_expect("onlyEvens test 6",onlyEvens([1,3]),[]) ___ passed

    ____failed: expected [], but got:

    check_expect("onlyEvens test 7",onlyEvens([3,2]),[2])
    ___ passed

    ____failed: expected [2], but got:

    check_expect("onlyEvens test 8",onlyEvens([2,3,4]),[2,4]) ___ passed

    ____failed: expected [2,4], but got:


So, this question requires you to "be the computer", and figure out what the broken code above would do for various test cases.

Here, my invitation to you is to try this on paper first.

Then, you can check your answers with the following code, which contains the "broken" function definition given, as well as the test cases. You can see if the output matches what you expected the output would be.

http://www.cs.ucsb.edu/~pconrad/cs8/10F/labs/lab11/code/09M_final_q2_testHarness.py

If you can do that, and you understand why each test case either passes or fails on this broken code, and why it produces the output that it does, then you are probably reasonably well prepared for this type of question.

Either way, it may be good to practice further with various ways that I could "change up" this question for this year's final exam:

As a side note—this question is similar to some of the questions we reflected on in lecture on Wednesday 09/01/2010, when we looked at the code for name2score

Question 4

Here's question 4 from the Summer 2009 Final exam:

  1. (20 pts) Write a Python function called letList with a single parameter called listOfStr that takes a list of strings, and returns a new list, where each string is replaced with the first letter of that string. If any item in the list is not a string or is an empty string, or if the parameter to the function is not a list, return the boolean value False. See the test cases for examples.

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

    Your function should pass these test cases:

    check_expect("letList test 1",letList('1'),"error")     
    check_expect("letList test 2",letList(['a','b']),['a','b'])
    check_expect("letList test 3",letList([]),[])   
    check_expect("letList test 4",letList(['Univ','Calif']),['U','C'])   
    check_expect("letList test 5",letList(['Computer','Science']),['C','S'])
    check_expect("letList test 6",
                 letList(['Fouled','Up','Beyond','All','Recognition']),
                 ['F','U','B','A','R'])  
    check_expect("letList test 7",letList(['Spaghetti']),['S'])   
    check_expect("letList test 8",letList(['for','the','win']),['f','t','w'])  
    check_expect("letList test 9",letList(['for','the', 42,'doh!']),False)
    check_expect("letList test 10",letList(['for','the', '','doh!']),False)      
          
    
    You do not need to write the check_expect function, or recopy the test cases.



This question is similar to Question 1, in that it provides test cases, and asks you to write a correct function definition.

So you can practice in exactly the same way. For you convenience, here is a test harness for this question:

 

Question 5

Here's question 5 from the Summer 2009 Final exam:

  1. (10 pts) Write a Python function wheresWaldo that takes a parameter aList, and returns -1 if the parameter is not a list, is an empty list, or if the string "Waldo" does not appear in the list. If the string "Waldo" does appear in the list, return the index of the first place that Waldo appears. See the test cases for examples.

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

    Hint: use a for loop such as for i in range(len(aList)): to search for an occurrence of Waldo.

    Another Hint: we didn't say anything about requiring everything in the list to be a string. Look carefully at test case 10 and make sure your answer passes it as well.

    check_expect("wheresWaldo test 1",wheresWaldo('1'),-1)     
    check_expect("wheresWaldo test 2",wheresWaldo(["he's","not","here"]),-1)
    check_expect("wheresWaldo test 3",wheresWaldo([]),-1)   
    check_expect("wheresWaldo test 4",wheresWaldo(['My','friend','Waldo']),2)   
    check_expect("wheresWaldo test 5",wheresWaldo(['Hi','Waldo']),1)
    check_expect("wheresWaldo test 6",wheresWaldo(['Have','you','met','Waldo']),3)  
    check_expect("wheresWaldo test 7",wheresWaldo(['Waldo']),0)   
    check_expect("wheresWaldo test 8",wheresWaldo(['Waldo','Waldo','Waldo']),0)  
    check_expect("wheresWaldo test 9",wheresWaldo(["Here's","a","Waldo","there's","a","Waldo"]),2)
    check_expect("wheresWaldo test 10",wheresWaldo(["lowercase","waldo","can't","be","found"]),-1)
    check_expect("wheresWaldo test 11",wheresWaldo([2,4,6,8,"who","do","we","appreciate","Waldo"]),8)
    
    You do not need to write the check_expect function, or recopy the test cases.


This question is similar to Questions 1 and 4, in that it provides test cases, and asks you to write a correct function definition.

So you can practice in exactly the same way. For you convenience, here is a test harness for this question:

 

Additional Studying

In addition to reviewing the questions on this lab (lab11), you should also review:


There is no evaluation, grading, or due date for this lab

This lab is only for practice for the final exam.


Copyright 2010, Phillip T. Conrad, CS Dept, UC Santa Barbara. Permission to copy for non-commercial, non-profit, educational purposes granted, provided appropriate credit is given; all other rights reserved.