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 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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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 file Mileage.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.
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
Transcribed Image 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
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
Transcribed Image 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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Types of Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education