111 lab6

Computer Science 111 – Fundamentals of Programming I

Programming Project 6

Due 11:59 PM Friday 30 October

In this project you will define some functions to manipulate lists and explore the use of higher- order functions and optional/default parameters.


Open an IDLE shell and run the following expressions at the prompt:

import random
numbers = range(1, 51)
map(str, numbers)
list(map(str, numbers))
list(map(lambda n: n ** 2, numbers))
list(filter(lambda n: n >= 20 and n <= 30, numbers))
list(map(lambda n: n ** 2, filter(lambda n: n % 2 == 0, numbers)))
numbers = list(numbers)
numbers.sort(reverse = True)

To Turn in

Launch idle3 and complete the following exercises. Each file or module should have the usual docstring comment, and each new function that you define should also have its own docstring comment.

  1. Copy the file myfunctions.py from the website directory to your project6 directory. Open the file with idle3 and examine its contents. You will see a main function that tests three functions when the file is launched as a Python app (by pressing F5 or by running python3from a terminal window). You can uncomment the code in main when you are ready to test your new functions (Exercises 2-4).
  2. Define a new function inputList. This function expects a string and an optional type conversion function as arguments and builds and returns a list of inputs as described in lecture. Uncomment the relevant code in main to test your new function.
  3. Define a new function isSorted. This function expects a list of comparable (i.e., capable of being compared with each other) values (such as strings or integers) as an argument, and returns True if the values are in ascending order or False otherwise. (Hint: loop from the beginning till the second-to last item, returning False immediately when an out-of- order pair of items is encountered. If you make it to the end without such a pair, returnTrue.) Uncomment the relevant code in main to test your new function.
  4. Define a new function myRange. This function should behave like Python’s standardrange function, with the required and optional arguments, but should return a list. Do not use the range function in your implementation! Your new function should have one required parameter and two optional parameters (name them first, second, and third). Use a default value of None for the two optional parameters. If these parameters both equalNone, then the function has been called with just the upper bound. If only the third parameter equals None, then the function has been called with a lower bound as well. If neither optional parameter equals None, then the function has been called with a lower bound, an upper bound, and a step value. Thus, the first part of the function’s code establishes what the values of the parameters are or should be (hint: at this point, set the values of new temporary variables, named lower, upper, and step, to be used later in the function). The rest of the code uses these values in a while loop to build a list by counting up. Uncomment the relevant code in main to test your new function.
  5. Create a new file with idle3 named analyzegrades.py. This program consists of a singlemain function and imports your inputList function from the myfunctions module. The main function uses inputList to obtain a list of integer grades, filters them, sorts them in descending order, prints them, and prints the average grade. Your filter should drop all grades under 70 by using Python’s filter function.

Turn in your two Python files in your project6 folder.