# lab11 not for credit—just practice for the final exam

## Goals for this lab

• This lab is not for credit or extra credit.
• It is only an opportunity to study for the final exam.
• If you are in lab on Friday 12/03/2010:
• If you have not finished lab10, please work on that first--there is no late submission of this one after 5pm today, so even if it is not completed by 4:45pm, turn in what you have at 4:45pm so that you make the 5pm deadline.
• Once you are finished with lab10, use this lab to study for the final

## 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:

• Questions 1-5 and 7 cover material we definitely covered this quarter in lecture, labs and homework.
• Question 6 is based on material we went over in lecture on 11/30 about function calls and the stack.
• Question 8 covers material that we did NOT discuss this quarter, and that I would not expect you to be able to answer.

## 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.

• Why on paper? Because in order to really prepare for an exam, you need to prepare "as if" it were an exam—that means, creating the same conditions, etc.
• You may like to use the pdf link to actually print a paper copy of last year's exam, rather than using the copy of the question above—that's up to you.

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:

• First, check whether you made any syntax errors—for example, did you remember to do things such as the following correctly?
• indent the code correctly when you wrote it on paper?
• use colons (:) after the first line of for loops and if statements, and after the keyword else?
• use "" around literal strings
• in general, write your code in a way that it doesn't give you any immediate errors when you try to run it?
• Then, see if the code passes the test cases
• If it doesn't, try to understand why.

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:

• return the sum of all the ints in the list
• return the average of all the ints in the list
• return a count of all the strings in the list (everything of type str)
• concatenate all the strings together into one big string and return it
• concatenate all the strings in the list, but separate them all by spaces or commas
• A trick with this one is to make sure that you don't end up with an extra space after the final item in the list.
• count all the even (or odd, or positive, or negative) integers in the list
• find the sum (or average, or min, or max) of all the even (or odd, or positive, or negative) integers in the list

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:

• the "lab07" referred to in the question is the lab07 from Summer 2009, not the Summer 2010 lab07
• in Summer 2010, the onlyEvens function was actually part of lab05.
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.

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:

• First, what would a correct version of this function look like? Try to write a correct version.
• Second, make up some test cases of your own and try to predict what the output would be before you run the code.
• Third, go through your old labs where you have correct function definitions and test cases.
• Try "breaking" the function definition on purpose in various ways.
• Each time, see if you can predict what will happen for each of the test cases.
• The most interesting types of questions here are ones where you "break" the function, but then some of the test cases still pass, while others fail. See if you can come up with those kinds of situations.

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

• If you took good notes, you may remember some of the questions we considered—such as "what happens if you indent the "return False" in the right place, vs. indenting it in the wrong place so that it is inside the for loop, or inside the if statement.
• You can review that code here: code/09.01

### 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:

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

• all labs, especially lab08, lab09, lab10
• I could ask questions about labs00 through lab07 also, so review those too
• But the emphasis will be on the labs that didn't appear on the first two midterms, namely lab08,lab09,lab10
• all homeworks—emphasis is on H09 through H15, but review H00 through H08 too, as well as worksheet W01.
• all lecture notes
• all reading assignments that appeared in homeworks.

## 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.