Immediate Food Delivery Cost Calculation
This challenge involves calculating the minimum cost to deliver an order from a restaurant to a customer. In a simplified food delivery scenario, customers place orders, and restaurants prepare them. The cost of delivery is influenced by the time taken for preparation and the distance to the customer's location. Your task is to determine the most cost-effective delivery option.
Problem Description
You are given a list of food orders, each with a preparation time and a delivery distance. The cost of delivery for a single order is calculated as follows:
- Base Delivery Cost: A fixed cost of $8 for every order.
- Urgency Surcharge: If the preparation time is less than 20 minutes, an additional surcharge of $2 is added to the delivery cost.
Your goal is to write a function that takes a list of orders and calculates the total delivery cost for all orders.
Key Requirements:
- Iterate through each order in the provided list.
- For each order, determine if the urgency surcharge applies.
- Sum up the base delivery cost and any applicable surcharges for all orders.
Expected Behavior:
The function should return a single numerical value representing the total delivery cost for all orders.
Edge Cases:
- An empty list of orders should result in a total cost of $0.
- Orders with preparation times exactly equal to 20 minutes should not incur the urgency surcharge.
Examples
Example 1:
Input: [
{"preparation_time": 15, "delivery_distance": 5},
{"preparation_time": 25, "delivery_distance": 10}
]
Output: 18
Explanation:
- Order 1: Preparation time is 15 minutes (< 20), so base cost ($8) + urgency surcharge ($2) = $10.
- Order 2: Preparation time is 25 minutes (>= 20), so base cost ($8) = $8.
- Total cost = $10 + $8 = $18.
Example 2:
Input: [
{"preparation_time": 20, "delivery_distance": 8},
{"preparation_time": 10, "delivery_distance": 3},
{"preparation_time": 30, "delivery_distance": 15}
]
Output: 26
Explanation:
- Order 1: Preparation time is 20 minutes (>= 20), so base cost ($8) = $8.
- Order 2: Preparation time is 10 minutes (< 20), so base cost ($8) + urgency surcharge ($2) = $10.
- Order 3: Preparation time is 30 minutes (>= 20), so base cost ($8) = $8.
- Total cost = $8 + $10 + $8 = $26.
Example 3:
Input: []
Output: 0
Explanation: An empty list of orders results in zero total cost.
Constraints
- The number of orders will be between 0 and 1000, inclusive.
- Preparation time for each order will be an integer between 1 and 60 minutes, inclusive.
- Delivery distance for each order will be an integer between 1 and 50 kilometers, inclusive.
- The output should be an integer.
- The solution should be efficient enough to handle up to 1000 orders without significant performance degradation.
Notes
- The
delivery_distanceattribute is provided but not used in the cost calculation for this specific problem. Focus only onpreparation_timefor determining the urgency surcharge. - Consider how you will handle the data structure representing the orders. A list of dictionaries or a similar structure is anticipated.