In: Computer Science
PYTHON
Computer Science
Objectives
Work with lists Work with functions Work with files
Assignment
Write each of the following functions. The function header must be
implemented exactly as specified. Write a main function that tests
each of your functions.
Specifics
In the main function ask for a filename and fill a list with the values from the file. Each file should have one numeric value per line. This has been done numerous times in class. You can create the data file using a text editor or the example given in class – do not create this file in this program. Then call each of the required functions and then display the results returned from the functions. Remember that the functions themselves will not display any output, they will return values that can then be written to the screen.
If there are built in functions in Python that will accomplish the tasks lists below YOU CANNOT USE THEM. The purpose of this lab is to get practice working with lists, not to get practice searching for methods. DO NOT sort the list, that changes the order of the items. The order of the values in the list should be the same as the order of the values in the file.
Required Functions
def findMaxValue (theList) – Return the largest integer value found in the list. def findMinValue (theList) – Return the smallest integer value found in the list.
int calcRange (theList) – Return the range of values found in the list. The range is the difference between the largest and smallest values. This function MUST USE findMaxValue and findMinValue to determine the value to return. This function CANNOT have its own loop.
def calcAverage (theList) – Return the average of all the values found in the list.
def findNumberAbove (theList, testValue) - Return the number of values greater than OR equal to the second argument (testValue).
def findFirstOccurance (theList, valueToFind) – Return the index of the first occurrence of valueToFind. If valueToFind does not exist in the list return -1.
def findLastOccurance (theList, valueToFind) – Return the index of the last occurrence of valueToFind. If valueToFind does not exist in the list return -1.
def calcCount (theList, valueToFind) – Return the number of occurrences of valueToFind within the list.
def isInList (theList, valueToFind) – Return True if valueToFind occurs at least once in the list, otherwise return False.
Code is as follows:
The text file is as follows:
The output of the above code is as follows: