This assignment is designed to build your competence with basic list operations. As with the previous lab on strings, we are going to "tie one arm behind our backs" by not making use of most of the methods of Python lists, so as to focus on basic operations.
You are also responsible for program style, including all the style rules in Coding Standards through Chapter 4.
Define and test three functions on lists:
repl function takes three arguments:
oldis any value;
newis any value;
xsis a list.
It returns a new list formed by replacing every occurrence of
>>> repl('zebra', 'donkey', ['mule', 'horse', 'zebra', 'sheep', 'zebra']) ['mule', 'horse', 'donkey', 'sheep', 'donkey']
It must not mutate the list
xs; i.e., after return from the function, the actual argument given for
xs must be what it was before:
>>> friends = ['jules', 'james', 'janet', 'jerry'] >>> repl('james', 'henry', friends) ['jules', 'henry', 'janet', 'jerry'] >>> friends ['jules', 'james', 'janet', 'jerry']
search function looks for a value in a list. It takes two arguments:
yis the value being searched for.
xsis the list being searched in.
It returns the index of the first occurrence of
xs, if it occurs; −1 otherwise.
>>> words = ['four', 'very', 'black', 'sheep'] >>> search('four', words) 0 >>> search('sheep', words) 3 >>> search('horse', words) -1
doubles function is given a list of numbers and forms a new list containing the doubles of every number in the given list.
>>> doubles([1, 3, 7, 10]) [2, 6, 14, 20]
It must not mutate the given list:
>>> salaries = [5000, 7500, 15000] >>> doubles(salaries) [10000, 15000, 30000] >>> salaries [5000, 7500, 15000]
You may not use any list methods except
append. (In particular, you may not use the
count for the
search function.) List methods include those described in Table 4.2 on page 125 of the textbook, but are not limited to those in the table. In general, a list followed by a dot (
.), method name, and method arguments make a list method call; so don't do anything in this form
unless methodname is
You may use the list
len function, and the list operations
*, indexing, slicing, and
== for comparing lists or elements. You will need to use some of these but not all.
A loop is needed for all three problems, and the accumulator pattern is a good strategy for two of them.
If you're building a list, initialize the accumulator as the empty list,
To update the list by appending an element
z to the rear, use either of these forms:
somelist = somelist + [z] somelist.append(z)
repl function for lists is very similar to the
repl function for strings from Lab 2.
Turn this assignment in through Oncourse Assignments. by attaching two files and writing in "Submitted text."
Attach a copy of your Python source file (the file name must end in ".py").
Run the program in a fresh IDLE Python Shell and evaluate the following test cases:
>>> # Problem 1 >>> flowers = ['petunia', 'daffodil', 'lily', 'lily', 'violet', 'rose', 'rose'] >>> repl('lily', 'hyacinth', flowers) >>> flowers >>> repl('trumpet', 'violin', flowers) >>> # Problem 2 >>> search('petunia', flowers) >>> search('rose', flowers) >>> search('xylophone', flowers) >>> # Problem 3 >>> numbers = [12, 24, 15, -9, 7] >>> doubles(numbers) >>> numbers
Save the Python shell in a file name ending in ".txt". Attach this file as your test results.
In the "Submitted text" box, describe any problems you encountered in developing the program, and explain how you solved them. If you had no problems, just say "No problems."
(CA = Competency Assessment; pca = partial credit is available)
LIST CA SUBTOTAL: 9 points, 6 to "pass". This is not recorded as a separate score, and it is not "all or nothing." If you earn significantly less than 6, you may get a chance to resubmit; but do try to get it right the first time!
TOTAL: 12 points