In: Computer Science
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.
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 calcAverage (theList) – Return the average of all the values found in the list.
def findNumberAboveAverage (theList) - Return the number of values greater than OR equal to the average. This requires you to call the calcAverage function from this function – DO NOT repeat the code for calculating the average.
def findNumberBelowAverage (theList) - Return the number of values less than the average. This requires you to call the calcAverage function from this function – DO NOT repeat the code for calculating the average.
def standardDeviation (theList) – Return the standard deviation of the values. To find the standard deviation start with the average of the list. Then for each value in the list subtract the average from that value and square the result. Find the average of the squared differences and then take the square root of that average.
For example, if the list contains the values 4, 5, 6, and 3 the average of those values would be 4.5. Subtract 4.5 from each value and square that results. The differences would be -0.5, 0.5, 1.5 and -1.5 respectively. The square of each of those differences would be 0.25, 0.25, 2.25, and 2.25 respectively. The average of these values is 1.25 ((0.25 + 0.25 + 2.25 + 2.25) / 4).
Solution:
Code implementation:
def findMaxValue(theList): # finds max value from the list
max=theList[0]
for i in range(1,len(theList)):
if(theList[i]>max):
max=theList[i]
return max
def findMinValue(theList): # finds min value from the list
min=theList[0]
for i in range(1,len(theList)):
if(theList[i]<min):
min=theList[i]
return min
def findFirstOccurance(theList,valueToFind): # finds first
occurance of valuetoFind from the list.
for i in range(len(theList)):
if(theList[i]==valueToFind):
return i
return -1
def findLastOccurance(theList,valueToFind): # finds last
occurance of valuetoFind from the list.
index=-1
for i in range(len(theList)):
if(theList[i]==valueToFind):
index=i
return index
def calcAverage(theList): # finds average of all the values of
the list
sum=0
for i in range(len(theList)):
sum+=theList[i]
return sum/len(theList)
def findNumberAboveAverage(theList): # finds counts of numbers
above or equal to average.
avg=calcAverage(theList)
cnt=0
for i in theList:
if(i>=avg):
cnt+=1
return cnt
def findNumberBelowAverage(theList): # finds counts of numbers
below average.
avg=calcAverage(theList)
cnt=0
for i in theList:
if(i<avg):
cnt+=1
return cnt
import math # imports math module for using sqrt function.
def standardDeviation(theList): # finds standard deviation of the
list elements.
avg=calcAverage(theList)
for i in range(len(theList)):
theList[i]=(theList[i]-avg)**2
avg_squared=calcAverage(theList)
return math.sqrt(avg_squared)
def main():
filename=input("Enter the filename: ")
print(filename) # takes file name as input.
theList=list() # creates the list
with open(filename) as f: # opens the file
lines=f.readlines() # reads all the lines at a time.
for line in lines:
theList.append(int(line.strip())) # creates the list of
numbers.
print("Maximum: "+str(findMaxValue(theList)))
print("Minimum: "+str(findMinValue(theList)))
print("First Occurance:
"+str(findFirstOccurance(theList,12)))
print("Last Occurance: "+str(findLastOccurance(theList,12)))
print("Average: "+str(calcAverage(theList)))
print("Count of number above average:
"+str(findNumberAboveAverage(theList)))
print("Count of number below average:
"+str(findNumberBelowAverage(theList)))
print("Standard Deviation: "+str(standardDeviation(theList)))
if __name__== "__main__":
main()
Code screenshot:
Output screenshot: