CSC 110: Introduction to Computer Programming – HW 4 Possible Points: 25 points
ID: 3677595 • Letter: C
Question
CSC 110: Introduction to Computer Programming – HW 4
Possible Points: 25 points
Due Date: Refer to our Canvas class site
This homework has the following goals:
Give you more practice reading data from files
Introduce you to using functions to perform specific tasks in your programs
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
Explanation / Answer
# Create tables to parse the files into
hwy = []
city = []
gasGuzzlersList = []
def readData(fileName):
mpgData = open(fileName, "r")
mpgList = [float(i) for i in mpgData.read().replace(" ", " ").split()]
return mpgList
# Calculate the average mpg
def averageMPG(dataList):
avgMPG = sum(dataList) / len(dataList)
return avgMPG
# Enumerate and count vehicles meeting gas guzzler criteria
def countGasGuzzlers(masterList1, masterList2, threshold1, threshold2, targetList):
for i in range(len(masterList1)):
if masterList1[i] < threshold1:
targetList.append(masterList1[i])
elif masterList2[i] < threshold2:
targetList.append(masterList2[i])
return targetList
# Format and output the results of the study and this program
def printOutput(total, cAvg, hAvg, guzzlers):
print(total, "cars were recently tested by the US Dept of Energy.")
print("The average gas mileage found in the study was", cAvg, "for city and", hAvg, "for highway. ")
print("Of the vehicles tested,", guzzlers, "were considered gas guzzlers,")
print("that is, less than 22 mpg in the city or 27 mpg on the highway. ")
# Execute the program
def main():
city = readData("carModelData_city")
hwy = readData("carModelData_hwy")
cityAvg = averageMPG(city)
hwyAvg = averageMPG(hwy)
gasGuzzlers = countGasGuzzlers(city, hwy, 22.0, 27.0, gasGuzzlersList)
printOutput(len(city), round(cityAvg, 1), round(hwyAvg, 1), len(gasGuzzlers))
main()