Question

In: Computer Science

Assignment Write each of the following functions. The function header must be implemented exactly as specified....

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).

Solutions

Expert Solution

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:


  
  
  


Related Solutions

You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function (merge-sorter L1) that takes list-of-integers L1 and returns all elements of L1 in sorted order. You must use a merge-sort technique that, in the recursive case, a) splits L1 into two approximately-equal-length lists, b) sorts those lists, and then c) merges the...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (forget-n L1 N) that returns the elements of L1 except for the first N. If N is negative, return all elements. If N exceeds the length of L1 return the empty list....
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem needs to use DrRacket software. Racket Language. Write a function (indices L1 X) that takes a list of elements L1 and an element X. The function returns a list of the indices in L1 that contain X. See the following examples for clarification....
You must write each of the following scheme functions. You must use only basic scheme functions,...
You must write each of the following scheme functions. You must use only basic scheme functions, do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (first-n L1 N) that returns the first N elements of L1. If N is negative, return the empty list. If N exceeds the length of L1 return all elements of L1. (first-n...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function (join-together L1 L2) that takes a sorted list (ascending) of integers L1 and a sorted list of elements L2 and returns a sorted list containing all elements of both L1 and L2. See...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function (indices L1 X) that takes a list of elements L1 and an element X. The function returns a list of the indices in L1 that contain X. See the following examples for clarificaton. (indices '(a b c a e f a) 'a')...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function named (list-replace ALIST SYM VAL) that accepts a list of elements and returns that list where all SYM's (a single symbol) have been replaced by the VAL (some scheme value). The replacement must occur even within nested lists. For example: (list-replace '(a...
Implement each of the following functions and write a basic main() function that tests each. For...
Implement each of the following functions and write a basic main() function that tests each. For convenience, you should be able to find this starter class under Mimir's assignment 4 starter code. Do not change the name, parameters, or returns of any of these functions or of the name of the class itself. There is also no need in this assignment for any global variables. You are strongly encouraged to use your solution for some of these functions in others...
For this assignment, you must follow directions exactly. Create a P5 project in Eclipse then write...
For this assignment, you must follow directions exactly. Create a P5 project in Eclipse then write a class P5 with a main method, and put all of the following code into the main method: Instantiate a single Scanner object to read console input. Declare doubles for the gross salary, interest income, and capital gains. Declare an integer for the number of exemptions. Declare doubles for the total income, adjusted income, federal tax, and state tax. Print the prompt shown below...
Write the following functions. Each function needs function comments that describes function and its parameters double...
Write the following functions. Each function needs function comments that describes function and its parameters double sphereVolume( double radius) double sphereSuface( double radius) double cylinderVolume( double radius, double height) double coneSurface( double radius, double height) double coneVolume( double radius, double height) That computes the volume and surface of the sphere with radius, a cylinder with a circular base with radius radius , and height height , and a cone with a circular base with radius radius , and height height...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT