INFO I210: Lab 4

Lists

Overview

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.

Requirements

Problems

Define and test three functions on lists:

  1. The repl function takes three arguments:

    It returns a new list formed by replacing every occurrence of old in xs with new.

    Example:

    >>> 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']
  2. The search function looks for a value in a list. It takes two arguments:

    It returns the index of the first occurrence of y in xs, if it occurs; −1 otherwise.

    Examples:

    >>> words = ['four', 'very', 'black', 'sheep']
    >>> search('four', words)
    0
    >>> search('sheep', words)
    3
    >>> search('horse', words)
    -1
  3. The doubles function is given a list of numbers and forms a new list containing the doubles of every number in the given list.

    Example:

    >>> 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]

General Requirements

  1. You may not use any list methods except append. (In particular, you may not use the index or 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

    alist.methodname(args)

    unless methodname is append.

Hints

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

  2. A loop is needed for all three problems, and the accumulator pattern is a good strategy for two of them.

    1. If you're building a list, initialize the accumulator as the empty list, [].

    2. To update the list by appending an element z to the rear, use either of these forms:

      somelist = somelist + [z]
      somelist.append(z)
  3. The repl function for lists is very similar to the repl function for strings from Lab 2.

Submission

Turn this assignment in through Oncourse Assignments. by attaching two files and writing in "Submitted text."

  1. Attach a copy of your Python source file (the file name must end in ".py").

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

  3. 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."

Scoring

(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