1 Final Lab for CS 110x
1.1 Preparation For Exam3
1.1.1 Eliminate Evens And Return List
1.1.2 Eliminate In Place
1.1.3 Dealing With Tuples
1.2 Debugging Challenge
1.3 What To Submit
1.4 Version : 2014/ 03/ 06

CS 110X Mar 05 2014 : LAB SEVEN

There are no shortcuts to any place worth going
Beverly Sills

1 Final Lab for CS 110x

1.1 Preparation For Exam3

I am trying to prepare you for the final Exam3, to be held on Friday March 6th. As you know, there will be five questions on the exam.

The ones that are most difficult are the questions asking students to complete a coding assignment. When facing these sorts of questions, you have to ask yourself four questions:

In this lab you are going to answer three programming problems, each of which is a good example for the kind of questions that I would ask on Exam3.

Download the template file lab_7.py and use it for your solution. This is the file that you will submit via turnin.

1.1.1 Eliminate Evens And Return List

Write a function, removeEvens(list) that takes a list and returns a new list containing only the odd values from the original list. You can assume that the list is non-empty. Note that the original list used in the invocation must remain unchanged.

The sample output to this function appears below. Note that the original value, list, is unchanged by this invocation.

>>> list = [2, 3, 4, 5, 6, 7] >>> val = removeEvens(list) >>> val [3, 5, 7] >>> list [2, 3, 4, 5, 6, 7]

Pay close attention to what the function must do, what it must return, and what information it needs to complete its task.

1.1.2 Eliminate In Place

Write a function, remove(list, filter) that removes from list all values that exist in another list, filter. Nothing is returned, but list is modified in place.

>>> list = [2, 5, 3, 4, 2, 6, 7, 3] >>> remove(list, [2, 3]) >>> list [5, 4, 6, 7]

Pay close attention to what the function must do, what it must return, and what information it needs to complete its task.

Make sure that you actually modify list in place. No need to use return, and of course, no need to use print.

1.1.3 Dealing With Tuples

Write a function, separate(list) that returns a tuple of two lists, one containing all even numbers in list and one containing all odd numbers in the list. The original list is unchanged.

>>> separate([2, 3, 4, 5, 6, 7]) ([2, 4, 6], [3, 5, 7]) >>> separate([1,3,5,7,9]) ([], [1, 3, 5, 7, 9]) >>>

Note that you likely don’t have to do anything special in case the original list is composed entirely of even or odd numbers. If you code it properly, this behavior should happen automatically.

1.2 Debugging Challenge

You might not have time to finish this debugging challenge, but please attempt it when you can; nothing to submit here, although I have included the defective code in the lab_7.py file.

You have been asked to write a function, checksum(list) that returns True if the last element in the list equals the sum total of all previous element in the list; otherwise it returns False. You know that the list contains numeric values, but you don’t know anything about how many elements it might contain.

Here is the code written so far:

def checksum(list): if len(list): print ’False’ else: total = 0 for idx in range(list): total = list + [idx] if total == list[idx]: return True return False

However, here is the sample output of the program, with my annotations

>>> checksum([1, 2, 3, 6]) False ### SHOULD HAVE BEEN True >>> checksum([3, 9, 5]) False ### THIS ONE IS False >>> checksum([7, 7]) False ### SHOULD HAVE BEEN True

There are SIX defects in the code. Please don’t rewrite the whole thing from scratch; rather, try to identify what each line should be doing, but rather isn’t doing. Note that one line does contain two defects.

If this question were on an exam, you would be asked to find four defects.

1.3 What To Submit

Be sure to submit your lab using Turnin, using the Lab07 designation.

1.4 Version : 2014/03/06

(c) 2014, George Heineman