Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment: This problem uses fuel economy data in miles per gallon (mpg) taken

ID: 3769745 • Letter: A

Question

Assignment:
This problem uses fuel economy data in miles per gallon (mpg) taken from the following US Department of Energy website: http://www.fueleconomy.gov/feg/download.shtml. The data files you have been provided with have been adapted from the CSV file on the website, and contain vehicle testing data for all models tested between 1984 and 2014 (last updated Sept 30, 2014). The first data file, carModelData_city, contains all the test results for city mpg and the second, carModelData_hwy, contains all the test results for highway mpg. Each file contains the same number of values, as the values in the same position in each list refer to the same vehicle. You will likely want to use the float() function to cast the string values to floats.
a. Write a function readData(filename) that will read in all the data from a text file that consists of float data formatted such that each value is on a new line.
b. Write a function averageMPG(dataList) that calculates the average mpg for all vehicles tested given a list of the mpg values.

c. Write a function countGasGuzzlers(list1, list2) that calculates the number of gas guzzlers among the vehicle models tested – for this program, define a “gas guzzler” as a car that gets EITHER less than 22 mpg city OR less than 27 mpg highway.

d. Write a function output(<parameters>) to print the following output (you will determine what parameters this function needs to have passed in to it): i. The total number of vehicles tested ii. The average for the city mpg for all the vehicles tested iii. The average for the highway mpg for all the vehicles tested iv. The number of gas guzzlers among the vehicle models tested

e. Write a program fuelEconomy.py that contains a main() function that calls all the functions you made in parts a-d.
Details:

As you have done on the earlier home-works, add comments to your Python functions describing the required inputs (if any) and the expected outputs. In addition, add comments as appropriate within the body of your program (e.g. to explain what a particular block of code is meant to accomplish).

Turn In:
Submit all of your python programs for this assignment as .py files. Include the output you obtained from each of your programs in a comment at the end of each program

#You can download the files through these links:
http://www.mediafire.com/download/6h5ntcdcc77kpx1/carModelData_city%282%29

http://www.mediafire.com/download/awns657l1vc4942/carModelData_hwy%282%29

#NEED TO WORK ON PYTHON 3.3+

http://www.mediafire.com/download/awns657l1vc4942/carModelData_hwy%282%29

Explanation / Answer

implemented functions a,b , d and e
def readData(filename):
   with open(filename) as f:
#       map function reads float data from file from each line
       floats = list(map(float, f))
   return floats

def averageMPG(dataList):
   total = 0
   count = 0
#   caluclating average of the list in the items
   for item in dataList:
       total = total+item
       count = count+1
  
   avg = total / count;
   return avg

def output(totalVehicles,avgCityMPG, avgHighMpg, noOfGuzzlers):
   print("The total number of vehicles tested : %d" %totalVehicles)
   print("The average for the city mpg for all the vehicles tested : %f" %avgCityMPG)
   print("The average for the highway mpg for all the vehicles tested : %f" %avgHighMpg)
   print("The number of gas guzzlers among the vehicle models tested : %d" %noOfGuzzlers)
  

def main():
   dataList = readData("D:/ravi/Cheg/carModelData_city(2).txt")
   print("Data in the file: ")
   print(dataList)
   avgMPG = averageMPG(dataList)
   print(" Average MPG: %s " %avgMPG)
  
#   sample test call
   output(100,avgMPG, 10.25, 40 )
  
  
main()

---------------output-----------------

[23.3333, 11.0, 29.0, 12.2222, 21.0, 27.0, 28.0, 29.0, 30.0, 29.0, 30.0, 23.0, 27.0, 23.0, 14.4444, 25.0, 22.0, 23.3333, 21.0, 21.0, 17.7778, 17.0, 13.3333, 26.0, 1.4, 19.7, 22.7, 21.1441,....]
Average MPG: 22.17491529481448
The total number of vehicles tested : 100
The average for the city mpg for all the vehicles tested : 22.174915
The average for the highway mpg for all the vehicles tested : 10.250000
The number of gas guzzlers among the vehicle models tested : 40

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote