CS8, Spring 2017

Lab05:
Using and printing lists
Finding extreme values in lists


Goals for this lab

By the time you have completed this lab, you and your lab partner should be able to

Step by Step Instructions

Step 0: Get together with your assigned lab partner.

Carefully choose the first pilot. If one of you has usually been the pilot, then choose the other partner to go first this time. Otherwise, whichever partner was pilot first last time needs to be navigator first this time. This lab's pilot should log in and create a lab05 directory under your cs8 folder. You will work in this account for the rest of the lab. If your partner is more than 5 minutes late, ask the TA to pair you with someone else for this week.

Step 1: Bring up IDLE as usual, and open a new window for function definitions

After IDLE starts, select "File=>New Window" and add a comment at the top of this new file like the following (with the proper substitutions of course):

# lab05.py
# Name: your name(s), today's date
# some list functions and test cases

Then, save it under the name lab05.py inside your lab05 directory.

Step 2: Function to print list elements

Write a function named printList to print the elements of a list with labels showing each element's order in the list. The function header should like this:

def printList(aList):

And here is an example run:

>>> myList = [92.5, 127.1, 9, 104.2, 78.4]
>>> printList(myList)
0 92.5
1 127.1
2 9
3 104.2
4 78.4

Test your function from the Python shell window on various types of lists with varying lengths.

Step 3: Functions to find maximum and minimum list values

Switch roles between pilot and navigator.

Now write two more functions - to find the position (or index) in the list of the maximum value (named findMax), and then the minimum value (findMin). You may assume that any list passed to these functions will not be empty - that is, assume there will always be at least one value in the list. Use these function headers:

def findMax(aList):

def findMin(aList):

Remember these functions must return the index of the maximum and minimum values, not the values themselves. Assuming the list myList is the same one used above in the example run for printList, then results should match these calls:

>>> findMax(myList)
1
>>> findMin(myList)
2

You can always test your work by comparing results to the built-in Python functions, max and min:

>>> max(myList) == myList[findMax(myList)]
True
>>> min(myList) == myList[findMin(myList)]
True

Step 4: Write test cases

Switch roles between pilot and navigator again.

Write a few good test cases - below the function definitions in the lab05.py window - just like you did in the last lab. For each test case: (a) create a list; (b) call printList; (c) use the Python print function to print both the maximum value and its position, and the minimum value and its position. Here is a sample test case:

myList = [-22.5, 1.4, -7.2, 10.9, -30.7, 11.3, 4.0]
printList(myList)
iMax = findMax(myList)
iMin = findMin(myList)
print("maximum is", myList[iMax], "at index", iMax)
print("minimum is", myList[iMin], "at index", iMin)

And here are the results of running the module with this test case at the bottom:

0 -22.5
1 1.4
2 -7.2
3 10.9
4 -30.7
5 11.3
6 4.0
maximum is 11.3 at index 5
minimum is -30.7 at index 4

Use different lists of varying sizes with the maximum and minimum values in varied positions. Be sure to test boundary cases where maximum and minimum values are the first and last elements, as well as routine cases where they are somewhere in the middle. Also notice you can save lots of typing by writing a function to perform one complete test for an arbitrary list. This function is not required though.

Be ready to demo your functions and test cases to the TA.

Step 5: Show off your work and get credit for the lab

Get your TA's attention to inspect your work, and to record your lab completion.

Don't leave early though ... see challenge problems below.

Step 5a. ONLY IF YOU RAN OUT OF TIME TO HAVE THE TA INSPECT YOUR WORK

If you must complete this assignment at CSIL, then submit it with the turnin program. You MUST have both your name and your partner's name in the file in order to receive credit. Remember that the original pilot needs to do this step, since that is whose account you have been using in Phelps 3525.

Bring up a terminal window on CSIL, and cd into the original pilot's cs8 directory, and cd again into the lab05 directory (because there is just one file to turn in this time). Then type the following:

turnin Lab05@cs8c lab05.py

Respond "yes" when the program asks if you want to turn in (be sure to read the list of files you are turning in), and then wait for the message indicating success.


Evaluation and Grading

Each student must accomplish the following to earn full credit for this lab:

Optional Extra Challenge

If you finish the lab early or would like an extra challenge

a. Try creating larger lists to test by filling them with random values. Remember to import random, and recall that random.random() returns a value in the range 0-1. You can multiply each value by a constant, and add it to another constant to produce list values in a different range. For example, the following calculations give random values between -100 and +100:

>>> 200 * random.random() - 100
-70.25452618972605
>>> 200 * random.random() - 100
-36.160738600182121
>>> 200 * random.random() - 100
77.647260575687909

b. Write and test functions like secondGreatest or thirdLeast to find specified extremes not as extreme as the maximum and minimum values. For example, secondGreatest in myList from step 4 is 10.9 at position 3, and thirdLeast is -7.2 at position 2.


Prepared by Diana Franklin and Michael Costanzo.