Question

In: Computer Science

PLEASE USE PTHON SPYDER Given a data file, find the max, min, and average values of...

PLEASE USE PTHON SPYDER

Given a data file, find the max, min, and average values of columns. Also, create an addition column that is based on the other columns. There will be no error checking required for this lab. You are provided with a data file named (surprise!) data.txt. Data is arranged in columns where each item of data is twelve columns wide (so it is easy to extract data items using slicing): Name Height(m) Weight(kg)

Joe 1.82 72.57

Mary 1.60 63.50

Dion 1.90 90.71

Kayla 1.72 66.31

Jose 1.78 70.23

Sofia 1.63 65.12

Erik 1.98 92.21

Sara 1.57 65.77

Your task is read the file data.txt and calculate the max, min, and average of the height and weight columns and to add an additional column that has the BMI calculated based on each height and weight. Your output will look like this (and needs to be formatted like this).

Name Height(m) Weight(kg) BMI

Joe 1.82 72.57 21.91

Mary 1.60 63.50 24.80

Dion 1.90 90.71 25.13

Kayla 1.72 66.31 22.41

Jose 1.78 70.23 22.17

Sofia 1.63 65.12 24.51

Erik 1.98 92.21 23.52

Sara 1.57 65.77 26.68

Average 1.75 73.30 23.89

Max 1.98 92.21 26.68

Min 1.57 63.50 21.91 The formula for BMI is simple in the metric system: BMI = weight/height**2 How do we find the max and min? The basic concept is similar to counting that you have done multiple times: initialize a value before a loop and the update the value every time through the loop. Based on that concept here is the algorithm to find the minimum of a set of values:

1. Initialize minimum to be much larger than any data value in your data, e.g. 10**6.

2. When you consider each data item, if the data item is smaller than the current minimum, you have a new minimum (so update it).

3. After considering all data items your minimum will contain the smallest. The algorithm to find the maximum is a tiny variation. To find the average, add up all the values and count how many values there are. Divide those two to get the average. Hint: build this program incrementally

1. Begin by simply opening the file and printing every line – now you know that you are correctly working with the correct file.

2. Next ignore the header line—use either readline() before the loop or continue in the loop.

3. Next find the average of a column: collect a total and count, remembering to initialize before the loop.

4. Next find the minimum of a column using the algorithm above.

5. Continue in such small increments to do the remainder of the program.

Part B: Create a data file The second task is to write that output to a file. The steps are

1. Open a file for writing, e.g. outfile = open(“output.txt”,”w”)

2. Whenever you print, include the argument file = outfile For example, if you previously had print(x) you will now have print(x, file = outfile)

3. Close the file, e.g. outfile.close() If you forget this step, nothing will be written to the file. Also, don’t forget the parentheses. Make those modifications to your lab06a.py file and test that the file created is correct.

Solutions

Expert Solution

Screenshot of the Code:

Input File:

Sample Output:

Output File:

Code to Copy:

#This code is for python 3.

#Open the file inr ead mode.

file=open("data.txt",'r')

#Read the file line by line.

file.readline()

#Create list

lstBMI=[]

BMIlst=[]

#Define the variables.

totalHeight=0

totalWeight=0

count=0

maxHeight=0

maxWeight=0

minHeight=100000

minWeight=100000

maxBMI=0

minBMI=100000

#Read the file and strip extra spaces.

for l in file:

        l = l.strip().split(" ")

        lstBMI.append(l)

#close the data.txt file.

file.close()

#Iterate through the list.

for i in range(len(lstBMI)):

   

        #Extract the height and weight.

        h=float(lstBMI[i][1])

        w=float(lstBMI[i][2])

        #Evaluate the BMI

        bmi=w/(h*h)

        #Append BMI to the list.

        BMIlst.append(bmi)

#Iterate through the list.

for i in range(len(BMIlst)):

        #Find the maximum BMI.

        if maxBMI < BMIlst[i]:

                maxBMI = BMIlst[i]

        #Find the minimum BMI.

        if minBMI > BMIlst[i]:

                minBMI=BMIlst[i]

#Iterate through the list.

for i in range(len(lstBMI)):

        #Update the count.

        count+=1

        #Evalaue the total height.

        totalHeight+=float(lstBMI[i][1])

        #Find the maximum height.

        if maxHeight< float(lstBMI[i][1]):

                maxHeight = float(lstBMI[i][1])

        #Find the minimum height.

        if minHeight > float(lstBMI[i][1]):

                minHeight = float(lstBMI[i][1])

        #Find the total weight.

        totalWeight += float(lstBMI[i][2])

       

        #Find the maximum weight.

        if maxWeight< float(lstBMI[i][2]):

                maxWeight=float(lstBMI[i][2])

        #Find the minimum weight.

        if minWeight> float(lstBMI[i][2]):

                minWeight=float(lstBMI[i][2])

#Find the average height, weight and BMI.

averageHeight = totalHeight/count

averageWeight = totalWeight/count

avgerageBmi = sum(BMIlst)/len(BMIlst)

#Display the result to the console.

print("{:<12s}{:<12s}{:<12s}{:<12s}".format("Name","Height(m)","Weight(kg)","BMI"))

for i in range(len(lstBMI)):

        print("{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}".format(lstBMI[i][0],float(lstBMI[i][1]),float(lstBMI[i][2]),BMIlst[i]))

print()

print("{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}".format("Average",averageHeight,averageWeight,avgerageBmi))

print("{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}".format("Max",maxHeight,maxWeight,maxBMI))

print("{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}".format("Min",minHeight,minWeight,minBMI))

#Open the output file in write mode.

outfile = open("output.txt",'w')

#Write the content to the file.

print("{:<12s}{:<12s}{:<12s}{:<12s}".format("Name","Height(m)","Weight(kg)","BMI"),file=outfile)

for i in range(len(lstBMI)):

        print("{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}".format(lstBMI[i][0],float(lstBMI[i][1]),float(lstBMI[i][2]),BMIlst[i]),file=outfile)

print(" ",file=outfile)

print("{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}".format("Average",averageHeight,averageWeight,avgerageBmi),file=outfile)

print("{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}".format("Max",maxHeight,maxWeight,maxBMI),file=outfile)

print("{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}".format("Min",minHeight,minWeight,minBMI),file=outfile)

#Close the output file.

outfile.close()


Related Solutions

Use Lagrange Multipliers to find any max or min values of f(x,y) = y^2 - 7x^2...
Use Lagrange Multipliers to find any max or min values of f(x,y) = y^2 - 7x^2 with the constraint x^2 + 2y^2 = 4
Find the absolute max/min values of f(x) = x/x2+1 on the interval [-2,2].
Find the absolute max/min values of f(x) = x/x2+1 on the interval [-2,2].
Using the ListNode file. Write methods called min and max that return the smallest and largest...
Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value. Write a method called insertNode that inserts a new node...
Using the ListNode file. Write methods called min and max that return the smallest and largest...
Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value. Write a method called insertNode that inserts a new node...
Find intervals of increase/decrease, max/min values, intervals of concavitiy. f(x)=x^3 -3x^2 +2
Find intervals of increase/decrease, max/min values, intervals of concavitiy. f(x)=x^3 -3x^2 +2
Please provide the following info for the given function Increasing:? Decreasing:? Local Min(s):? Local Max(s):? Concave...
Please provide the following info for the given function Increasing:? Decreasing:? Local Min(s):? Local Max(s):? Concave up:? Concave down:? Point(s) of Inflection:? f(x)= 2sin(x)+sin(2x), over [0,2pi ]
Describe the center and spread of the following values: Min: 3.31 Max: 5.21 Std Dev. 0.4339...
Describe the center and spread of the following values: Min: 3.31 Max: 5.21 Std Dev. 0.4339 Mean: 4.08 Median: 4.02 1st Quartile: 3.78 3rd Quartile: 4.40
1. Open the Salesum document, and add functions to calculate the average, Max, Min and Count...
1. Open the Salesum document, and add functions to calculate the average, Max, Min and Count on rows 18-21 in columns B-E. 2. In Cell B25 type an answer to the following question: Is the average computed in B18-E18 a good predictor of how much a new expense might cost, should we incur a new expense next year? Why or why not?
In the game given below, use the mini-max algorithm to find the prudent strategies for both...
In the game given below, use the mini-max algorithm to find the prudent strategies for both players and to find all the saddle points. Draw the flow diagram. Find any dominated strategies and completely reduce the game. 2 0 0 1 0 -1 0 -1 -2
The data file wages contains monthly values of the average hourly wages (in dol-lars) for workers...
The data file wages contains monthly values of the average hourly wages (in dol-lars) for workers in the U.S. apparel and textile products industry for July 1981 through June 1987. (a) Display and interpret the time series plot for these data. (b) Use least squares to fit a linear time trend to this time series. Interpret the regression output. Save the standardized residuals from the fit for further analysis. (c) Construct and interpret the time series plot of the standardized...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT