Delivery Fee Calculator¶
Write a Python function that calculates the final delivery fee for an online order. The fee is based on the following factors:
Amount: The total order amount of the order in euros with decimals.
Distance: the customer’s distance from the store in Km with decimals.
VIP: Whether the customer has a VIP membership or not.
Weekday: The number of the day of the week, being 1 Monday, 2 Tuesday, … and 7 Sunday.
The rules to calculate the fee are the following:
There is a base delivery fee that depends on the distance:
If distance is less than 1 km, the base fee is 3.00€.
If distance is between 1 km and 2.5 km (inclusive), the base fee is 5.00€.
If distance is larger than 2.5 km but less than 5.0 km then the base fee is 7.50€.
If distance is 5 km or more then the base fee is 10.00.
If distances greater than 10 km are not serviced and the function will return -1.0.
A discount can be applied to the base fee depending on the total amount of the order:
For amounts higher than 100.00€, the delivery is completely free (0.00€) regardless anything else.
For amounts between 50.00€ and 100.00€ (inclusive), a 50 percent discount must be applied to the base delivery fee.
For amounts higher or equal than 30.00€ but lower that 50.00€ a 10 percent discount is applied.
VIP customers get an additional 1.00€ discount.
- The day of the week may also affect the delivery fee:
On weekends (Saturday and Sunday) an 100 percent (double) increment is applied to the delivery fee except to VIP customers.
On mondays and Tuesdays and Wednesday, the fee is just 1.5€ independently of anything else.
Finally, unless the order qualifies for free delivery (100€+ order amount), the final delivery fee cannot drop below 1.00€.
You are required to implement the following functions in the delivery_fee module (file delivery_fee.py):
- delivery_fee_calc(amount, dist, is_vip, day)¶
such that
For example:
>>> amount, dist, is_vip, day = 60.0, 5.0, True, 5 >>> fee = delivery_fee_calc(amount, dist, is_vip, day) >>> fee == 4.0 True >>> amount, dist, is_vip, day = 60.0, 5.0, False, 6 >>> fee = delivery_fee_calc(amount, dist, is_vip, day) >>> fee == 10.0 True >>> amount, dist, is_vip, day = 60.0, 5.0, True, 6 >>> fee = delivery_fee_calc(amount, dist, is_vip, day) >>> fee == 4.0 True >>> amount, dist, is_vip, day = 60.0, 5.0, True, 2 >>> fee = delivery_fee_calc(amount, dist, is_vip, day) >>> fee == 1.5 True
Additional doctests for validation are available in the delivery_fee_calc.test file.