In Python 3.7: 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...


In Python 3.7:


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 fileMileage.txt. (Formula is 100 divided by gallons used, rounded up to the nearest .hundredth, as in "11.11")


Table 5.11 shows the data for gallons used. Write a 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 b the model).


---------------------


I already did it. I'm just asking if you can check my work.  I included pictures of the table and the code, as well has providing the code below.  Thank you.


-----------------------


''' This program create a dictionary of five items, with a key for each model,
and a two-tuple for each value. '''


inp=open('Mileage.txt','r') #reading the file
inp=inp.readlines() #reading every line from file
inp=[i.rstrip('\n') for i in inp] #removing the next line character from every line
inp=[i.split() for i in inp] #spliting every line into make and gallons
inp=[[i[0],float(i[1])] for i in inp] #converting the gallon to float
cars={} #empty dictionary
for i in inp:
if cars.get(i[0],0)==0:
cars[i[0]]=(1,i[1]) #creating new tuple
else:
count,mileage=cars[i[0]]
count+=1
mileage+=i[1]
cars[i[0]]=(count,mileage) #updating tuple


#empty lists to store make and mileage
makes=[]
mileages=[]
for i in cars:
makes.append(i)
j=cars[i]
mileages.append(round(j[0]*100/j[1],2))


#sorting mileage and make according to mileage in ascending order
zipped_pairs=zip(mileages,makes)
makes=[x for _, x in sorted(zipped_pairs)]
mileages.sort()


#reversing the list to make it descending order
makes=makes[::-1]
mileages=mileages[::-1]


#output
print(' Model MPG')
for i,j in zip(makes,mileages):
print('%8s %7.2f' %(i,j))


Gallons of gasoline used in 100 miles of driving.<br>TABLE 5.11<br>Model<br>Gal<br>Model<br>Gal<br>Model<br>Gal<br>Accord<br>2.1<br>Accord<br>4.3<br>Prius<br>4.1<br>2.3<br>Camry<br>Sebring<br>Camry<br>3.8<br>Prius<br>4.1<br>4.2<br>4.2<br>Camry<br>Mustang<br>3.9<br>Camry<br>5.2<br>Accord<br>Mustang<br>5.3<br>4.4<br>Model<br>MPG<br>Prius<br>45.45<br>25.00<br>Camry<br>Sebring 23.81<br>23.44<br>Accord<br>Mustang 19.05<br>Outcome of Programming Project 6.<br>FIGURE 5.51<br>

Extracted text: Gallons of gasoline used in 100 miles of driving. TABLE 5.11 Model Gal Model Gal Model Gal Accord 2.1 Accord 4.3 Prius 4.1 2.3 Camry Sebring Camry 3.8 Prius 4.1 4.2 4.2 Camry Mustang 3.9 Camry 5.2 Accord Mustang 5.3 4.4 Model MPG Prius 45.45 25.00 Camry Sebring 23.81 23.44 Accord Mustang 19.05 Outcome of Programming Project 6. FIGURE 5.51
1 inp=open('Mileage.txt','r') #reading the file<br>2 inp=inp.readlines() #reading every line from file<br>3 inp=[i.rstrip('\n') for i in inp] #removing the next line character from every Line<br>4 inp=[i.split() for i in inp] #spliting every Line into make and gallons<br>5 inp=[[i[0],float(i[1])] fori in inp] #converting the gallon to float<br>6 cars={} #empty dictionary<br>7 for i in inp:<br>if cars.get(i[0],e)==0:<br>8.<br>cars[i[0]]=(1,i[1]) #creating new tuple<br>else:<br>10<br>count, mileage=cars[i[0]]<br>11<br>12<br>count+=1<br>mileage+-i[1]<br>cars[i[0]]=(count, mileage) #updating tuple<br>13<br>14<br>15 #empty lists to store make and mileage<br>16 makes=[]<br>17 mileages-[]<br>18 for i in cars:<br>makes.append (i)<br>19<br>j=cars[i]<br>mileages.append(round(j[0]*100/j[1],2))<br>20<br>21<br>22 #sorting mileage and make according to mileage in ascending order<br>23 zipped_pairs=zip(mileages,makes)<br>24 makes=[x for , x in sorted(zipped_pairs)]<br>25 mileages.sortT)<br>26 #reversing the list to make it descending order<br>27 makes-makes[::-1]<br>28 mileages=mileages[::-1]<br>29 #output<br>30 print('<br>31 for i,j in zip(makes, mileages):<br>Model<br>MPG')<br>print('%85<br>%7.2f' %(i,j))<br>32<br>

Extracted text: 1 inp=open('Mileage.txt','r') #reading the file 2 inp=inp.readlines() #reading every line from file 3 inp=[i.rstrip('\n') for i in inp] #removing the next line character from every Line 4 inp=[i.split() for i in inp] #spliting every Line into make and gallons 5 inp=[[i[0],float(i[1])] fori in inp] #converting the gallon to float 6 cars={} #empty dictionary 7 for i in inp: if cars.get(i[0],e)==0: 8. cars[i[0]]=(1,i[1]) #creating new tuple else: 10 count, mileage=cars[i[0]] 11 12 count+=1 mileage+-i[1] cars[i[0]]=(count, mileage) #updating tuple 13 14 15 #empty lists to store make and mileage 16 makes=[] 17 mileages-[] 18 for i in cars: makes.append (i) 19 j=cars[i] mileages.append(round(j[0]*100/j[1],2)) 20 21 22 #sorting mileage and make according to mileage in ascending order 23 zipped_pairs=zip(mileages,makes) 24 makes=[x for , x in sorted(zipped_pairs)] 25 mileages.sortT) 26 #reversing the list to make it descending order 27 makes-makes[::-1] 28 mileages=mileages[::-1] 29 #output 30 print(' 31 for i,j in zip(makes, mileages): Model MPG') print('%85 %7.2f' %(i,j)) 32
Jun 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here