Question

In: Computer Science

Python - No libraries - No count() function allowed You need to travel 100 miles via...

Python - No libraries - No count() function allowed

You need to travel 100 miles via rental car. There are several cars on the lot to choose from, each with their own MPG (miles per gallon) rating. Some cars have a manual transmission, while others do not (they're automatic). The price for gas in the area is $3 per gallon. Cars that have a manual transmission get a 10% discount at the pump.

To streamline your selection, the car rental place can supply you with a dictionary that represents the cars on their lot. The keys of this dictionary are names of cars, and their values are another dictionary. The inner dictionary has a key for the MPG of this car, and a key for whether or not the car is manual.

Write a function called def cheapest(cars) that returns the name of the car that costs the least amount of money to travel 100 miles.

Here is an example (there could be more than just two cars):

cars_on_lot = {'Civic':{'mpg':40,'manual':True},'Volt':{'mpg':50,'manual':False}}

print(cheapest(cars_on_lot)) # Volt

The "Civic" gets 40 miles to the gallon and is a manual transmission. 100 miles in this car requires 2.5 gallons of gas. The manual transmission deduction is $0.75. Therefore, it costs $6.75 to travel 100 miles in this car.

The "Volt" gets 50 miles to the gallon but is not a manual transmission. 100 miles in this car requires 2 gallons of gas. There is no manual transmission deduction. Therefore, it costs $6 to travel 100 miles in this car.

Of these two options, the Volt is the cheapest car you can use to travel 100 miles.

Solutions

Expert Solution

Source code:

def cheapest(cars_on_lot):
result="" #for storing result
g=0 #for stroing gallons
cost=0 #for calculating cost
minimum=65535
for car in cars_on_lot:
a=cars_on_lot[car]
speed=a['mpg']
m=a['manual']
g=(100.0/speed)
cost=g*3;
# if car is manual applying 10% discount
if(m):
cost=cost-(cost*0.10)
# finding minimum cost
if(cost<minimum):
minimum=cost
#cor with minimum cost
result=car
return result
  
cars_on_lot = {'Civic':{'mpg':40,'manual':True},'Volt':{'mpg':50,'manual':False}}
#calling function
print(cheapest(cars_on_lot))

Code screenshot:

Output:


Related Solutions

Writ a Python Program that illustrate the DES algorithm 1-Not allowed to use any crypto libraries...
Writ a Python Program that illustrate the DES algorithm 1-Not allowed to use any crypto libraries 2- The whole algorithm must be implemented from scratch 2- The code must be properly commented Please don’t use librae’s or any code on the internet please do the code for me i have posted this many times
Writ a Python Program that illustrate the DES algorithm 1-Not allowed to use any crypto libraries...
Writ a Python Program that illustrate the DES algorithm 1-Not allowed to use any crypto libraries 2- The whole algorithm must be implemented from scratch 2- The code must be properly commented Please don’t use librae’s or any code on the internet
Write a python function (Without using external libraries) to calculate the 1). determinate of a 2...
Write a python function (Without using external libraries) to calculate the 1). determinate of a 2 x 2 matrix 2). Calculate the inverse of a 2 x 2 matrix.
Trucks in a delivery fleet travel a mean of 100 miles per day with a standard...
Trucks in a delivery fleet travel a mean of 100 miles per day with a standard deviation of 29 miles per day. The mileage per day is distributed normally. Find the probability that a truck drives at least 42 miles in a day. Round your answer to four decimal places.
Trucks in a delivery fleet travel a mean of 100 miles per day with a standard...
Trucks in a delivery fleet travel a mean of 100 miles per day with a standard deviation of 33 miles per day. The mileage per day is distributed normally. Find the probability that a truck drives at least 169 miles in a day. Round your answer to four decimal places.
Python ONLY ONE STATEMENT ALLOWED PER FUNCTION (ONE RETURN STATEMENT ALLOWED) def plsfollowrule(num): return num like...
Python ONLY ONE STATEMENT ALLOWED PER FUNCTION (ONE RETURN STATEMENT ALLOWED) def plsfollowrule(num): return num like this. 1) create a function popular. The dictionary called database contains each people's hobby and their excitement level. This function searches the dictionary and returns a list of that most popular hobby to least popular hobby. If multiple hobbies have the same number of popularity, follow alphabetical order. #What is popular means? The dictionary contains each people's hobby. The programmer should search the dictionary...
Starting in Albany, you travel a distance 319 miles in a direction 22.3 degrees north of...
Starting in Albany, you travel a distance 319 miles in a direction 22.3 degrees north of west. Then, from this new position, you travel another distance 360 miles in a direction 11.0 degrees north of east. In your final position, what is your displacement from Albany? 679 miles 33.3 degrees North of West 844 miles 23.5 degrees North of West 679 miles 33.3 degrees North of East 198 miles 72.9 degrees North of West
How can you determine if you need to use a combination or permutation to count the...
How can you determine if you need to use a combination or permutation to count the number of outcomes? Which will usually have more outcomes? Why? Provide an example in your explanation. (please provide detailed answer with no less than 100 characters)
python Write a program that prints your name 100 times to the screen. Write a function...
python Write a program that prints your name 100 times to the screen. Write a function that takes a string s and an integer n as parameters and prints the string s a total of n times (once per line). Write a for loop that prints all the integers from 3141 to 5926, skipping every other one. Write a for loop that prints every 5th integer from 5926 down to 3141. Write a program (using a for loop) to print...
USE PYTHON-LIST Q1 - You need to keep track of book titles. Write code using function(s)...
USE PYTHON-LIST Q1 - You need to keep track of book titles. Write code using function(s) that will allow the user to do the following. 1. Add book titles to the list. 2. Delete book titles anywhere in the list by value. 3. Delete a book by position. Note: if you wish to display the booklist as a vertical number booklist , that is ok (that is also a hint) 4. Insert book titles anywhere in the list. 5. Clear...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT