Lab Exercise 11

Lists and Methods and Loops


CS 1510
Introduction to Computing


Introduction

This week is all about lists, in particular Python's built-in functions and methods. Of course, we also get more practice with loops and choices.

Create a directory on your USB device for this lab, say, lab11, and launch IDLE.

This week, you will not submit your shell window at the end of the session. Do not panic if you close it accidentally along the way.

You will submit a responses.txt file this week. Download this template file and use it to record any answers or predictions asked for in the exercises.



Task 1: Experiment with Lists

Study this code:

code snapshot
Step 1.
Predict the output of this code.

Step 2.
Type the code into a file named task01.py. Run the code.

Step 3.
Explain any discrepancies between your predictions and the actual output.


Task 2: Implement Standard List Functions

A good way to learn a language and how it works is to implement some of its built-in functionality.

Create a new Python file named task02.py.

Step 1.
Write a function named length(a_list) that behaves exactly like the built-in function len().
       >>> length( ['alice', 20, True] )
       3

Step 2.
Write a function named total(a_list) that behaves exactly like the built-in function sum().
       >>> total( [1, 100, 3, 4, 5, 6] )
       119

Step 3.
Write a function named contains(a_list, item) that behaves exactly like the built-in operator item in a_list.
       >>> contains( ['alice', 20, True], 20 )
       True
       >>> contains( ['alice', 20, True], 'eugene' )
       False

Step 4.
Write a function named find(a_list, item) that behaves exactly like the built-in list method find().
       >>> find( ['alice', 20, True], True )
       2
       >>> find( ['alice', 20, True], 42 )
       -1


Task 3: Implement New List Functions

Python has lots of cool built-in functions, but people who program with lists often need more than the built-in functionality.

Create a new Python file named list_utils.py.

Step 1.
Write a function named all_to_int(a_list) that expects a list of strings and returns a new list where every string in the list has been turned into an integer. For example:
       >>> all_to_int( '1,2,3,4,5'.split(',') )
       [1, 2, 3, 4, 5]

Step 2.
Write a function named find_bigger(a_list, a_num) that expects a list of numbers and a number. It returns a new list consisting of all the numbers in the list that are greater than the number. For example:
       >>> find_bigger( [12, 45, 36, 4, 106, 43, 45, 7, 9, 15], 12 )
       [45, 36, 106, 43, 45, 15]
       >>> find_bigger( [12, 45, 36, 4, 106, 43, 45, 7, 9, 15], 102 )
       [106]

Step 3.
Write a function named add(list_a, list_b) that expects two lists of numbers and returns a new list containing the sums of those lists' members. For example:
       >>> add( [1,2,3], [4,5,6] )
       [5, 7, 9]
Bonus. If the lists are of different lengths, only process to the end of the shorter list:
       >>> add( [1,2,3], [4,5,6,7,8] )
       [5, 7, 9]


Task 4: Process an Arbitrary Number of Inputs

With list processing, we can let our user give us any number of inputs on the same line!

    >>>
    Enter as many numbers as you like, separated by spaces.
    Your numbers: 42 34 56 1 3
    ... add up to 136
    ... multiply to 239904
    ... contain 0 zeros
    >>>
    Enter as many numbers as you like, separated by spaces.
    Your numbers: 0 12 0 34 56 1 3 0 0 42 15 0
    ... add up to 163
    ... multiply to 0
    ... contain 5 zeros

Step 1.
Download this partially-completed program.

Step 2.
Add the statements necessary to produce the output above.

You may use Python's built-in functions and methods for lists.


Step 3.
Run your completed program with the inputs in the examples above and another example of your own choosing. Copy the results from the shell window and paste it into your responses file.


Finishing Up

Make sure that your program files are complete and saved. Save your responses.txt file.

Submit your files for grading on the electronic submission system, at lab11 -- Lists and Methods and Loops.

As always, make sure you see the verification screen that says The files listed above were uploaded.

If you need any help, let me know.



Eugene Wallingford ..... wallingf@cs.uni.edu ..... November 5, 2014