Question

A fuel-economy study was carried out for five models of cars. Each car was driven 100...

A fuel-economy study was carried out for five models of cars. Each car was driven 100 miles, and then the model of the car and the number of gallons used were placed in a line of the file Mileage.txt. Table 5.11 shows the data for the entries of the file. Write a python program to display the models and their average miles per gallon in decreasing order with respect to mileage. See Fig. 5.51. The program should create a dictionary of five items, with a key for each model, and a two-tuple for each value. Each two-tuple should be of the form (number of test vehicles for the model, total number of gallons used by the model).

Table 5.11Gallons of gasoline used in 100 miles of driving.

Model Gal

Prius 2.1

Camry 4.1

Sebring 4.2

Mustang 5.3

Accord 4.1

Camry 3.8

Camry 3.9

Mustang 5.2

Accord 4.3

Prius 2.3

Camry 4.2

Accord 4.4

Figure 5.51 Outcome

Model       MPG
Prius       45.45 
Camry       25.00 
Sebring     23.81 
Accord      23.44 
Mustang     19.05
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#Load the file Mileage.txt

fileName = 'Mileage.txt'

file=open(fileName, 'r')

lines=file.readlines()

carData=[tuple(line.strip().split()) for line in lines]

#remove the headers of the Mileage.txt i.e. 'Model' and 'Gal'

carData = carData[1:]

#print("Raw Data \n")

#print(carData)

milesDriven = 100;

carDict = {}

for i in range(len(carData)):

    if carData[i][0] in carDict:

        carCount, gallonsTotal = carDict[carData[i][0]]

        carCount += 1

        gallonsTotal += float(carData[i][1])

        carDict[carData[i][0]] = (carCount, gallonsTotal)

    else:

        carDict[carData[i][0]] = (int(1), float(carData[i][1]))

averageMileage = []

for i in carDict.keys():

    #carDict[i][0] has car count and carDict[i][1] has total gallons used.

    #averageMileage is rounded off to two decimal precision.

    averageMileage.append((i, round((carDict[i][0] * milesDriven)/ carDict[i][1], 2)))

#sort based on average milage with key to average mileage

averageMileage = sorted(averageMileage, key = lambda x: x[1], reverse=True)

#print the averageMileage

averageMileage.insert(0, ('Model','MPG'))

for i in averageMileage:

    print(i[0],i[1],sep='\t',end='\n')

Output terminal:========== RESTART: E:\test\test.py ======== Model MPG Prius 45.45 Camry 25.0 Sebring 23.81 Accord Mustang TO

It would be of great help to me if you provide thumbs up for the solution.

========== RESTART: E:\test\test.py ======== Model MPG Prius 45.45 Camry 25.0 Sebring 23.81 Accord Mustang TO

Add a comment
Know the answer?
Add Answer to:
A fuel-economy study was carried out for five models of cars. Each car was driven 100...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT