The program handin21 will only submit files in the cs21/homework/8 directory.
Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page
Some of the problems will have optional components that allow you to
further practice your skills in Python. Optional portions will not be
graded, but may be interesting for those wanting some extra challenges.
You should not use any of the methods in the list class (such as insert, append, remove, etc).
Below are some test cases for the multiplyList function you will write. You should add some more test cases. They need not be exhaustive, but they should check more than just a few basic test cases. In subsequent questions, you will write most or all of the test cases yourself. You should put your tests in a separate function called test() which is started below. The assert statement checks to be sure that the statement is true, and if not, raises an AssertionError.
def test(): """ Tests the functions in this program. Be sure to verify that the answer your are expecting is the answer you get by using the assert statement. """ #Here are some tests for question 2 to get you started. res = multiplyList([1, 2, -5, 3]) assert res == -30 res = multiplyList([]) assert res == 1 print 'All tests passed'
Write a new version of multiplyList(lst) called newMultiplyList(lst) which defines the product of an empty list to be 0. Be sure to write your own test cases. For example:
newMultiplyList([1, 2, -5, 3]) # returns -30 newMultiplyList([]) # returns 0
addOne([]) # returns [] addOne([1, 2, -5, 3]) # returns [2, 3, -4, 4]
# inserts 'pea' before the first 'pod' in ['a','green','pod'] # should return ['a', 'green', 'pea', 'pod'] insertBeforeFirst('pod', 'pea', ['a','green','pod']) # inserts 'pea' before the first 'pod' in ['pod','pod','pod'] # should return res == ['pea', 'pod', 'pod', 'pod'] insertBeforeFirst('pod', 'pea', ['pod','pod','pod']) # since there is no occurrence of 'pod', we do not insert 'pea' # should return ['a','green','leaf'] insertBeforeFirst('pod', 'pea', ['a','green','leaf'])
# should return ['a', 'b', 'c', 'd', 'd'] collapseFirstPair(['a', 'b', 'c', 'c', 'd', 'd'])
Here is an example:
# should return ['a', 'b', 'c', 'd', 'e', 'f'] collapseAllPairs(['a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e', 'f', 'f'])
# should return ['blue', 'fish', 'red', 'fish', 'blue'] swap('red', 'blue', ['red', 'fish', 'blue', 'fish', 'red'])
listReplace([('carrots', 'peas'), ('fork', 'spoon')],\ ['eat', 'your', 'carrots', 'with', 'a', 'fork']) # returns ['eat', 'your', 'peas', 'with', 'a', 'spoon'] listReplace([('carrots', 'peas'), ('peas', 'potatoes')],\ ['eat', 'your', 'carrots', 'and', 'peas']) # returns ['eat', 'your', 'peas', 'and', 'potatoes'] # NOTE: THE ANSWER IS NOT: ['eat', 'your', 'potatoes', 'and', 'potatoes']
Suppose you wanted to write a function addTwo similar to the function addOne you wrote above. There are three ways you could do this:
def addThree(lst): return addN(lst, 3)This is a much nicer solution, but it only works if you want to add things to the elements of the list.
Here is an example:
def plusOne(x): return x + 1 # should return [1, 2, 3] listmap(plusOne, [0, 1, 2])(Python has a built-in function called map which does exactly this! You can do help(map) for more information.)
Do help(filter) to find out about the built-in function filter, then implement it using a recursive solution. Call your version listfilter(fn, lst) You can ignore what happens if the first argument is None.