Transportation on vacation
Description:
After a hard quarter in the office you decide to get some rest on a vacation. So you will book a flight for you and your girlfriend and try to leave all the mess behind you.
You will need a rental car in order for you to get around in your vacation. The manager of the car rental makes you some good offers.
Every day you rent the car costs $40. If you rent the car for 7 or more days, you get $50 off your total. Alternatively, if you rent the car for 3 or more days, you get $20 off your total.
Write a code that gives out the total amount for different days(d).
Solution:
1. Multiply 40 by the number of 'd'.
2. If the number of 'd' is 7 or more, subtract 50 and return it.
3. If the number of 'd' is 3 or more, subtract 20 and return it.
def rental_car_cost(d):
cost = d * 40
discount = {
True:50, False:{
True:20, False:0
}[d >= 3]
}[d >= 7]
return cost - discount
This time, I deliberately used a dictionary to solve it.
I was able to solve the two branches into a dictionary without using if.
Actually, this problem can be solved more simply.
For a simpler solution, refer to the code below.
def rental_car_cost(d):
return d * 40 - (d > 2) * 20 - (d > 6) * 30