Dynamic Discount Calculator
This challenge focuses on implementing various flow control structures in Python to dynamically calculate discounts based on customer purchase history and item type. You will use if, elif, else statements, and for loops to build a robust discount logic. This is a fundamental skill for creating applications that respond differently based on conditions, such as e-commerce platforms.
Problem Description
You are tasked with creating a Python function calculate_discount that takes a customer's purchase history and the current item's price as input. The function should determine and return the applicable discount percentage based on a set of rules.
Requirements:
- Customer Loyalty Discount: If a customer has made 10 or more purchases historically, they receive a 5% discount.
- High-Value Item Discount: If the current item's price is $100 or more, they receive an additional 3% discount.
- Special Category Discount: If the item belongs to the "Electronics" category, they receive a 7% discount.
- Combined Discounts: Discounts are additive. For example, a loyal customer buying an expensive electronic item can receive multiple discounts.
- Maximum Discount: The total discount cannot exceed 15%.
- No Discount: If none of the above conditions are met, no discount is applied (0%).
Expected Behavior:
The function should return a floating-point number representing the discount percentage (e.g., 0.05 for 5%, 0.10 for 10%).
Edge Cases:
- Customers with zero purchases.
- Items with a price of exactly $0.
- Items with prices just below or at the thresholds ($99.99, $100).
- When multiple discounts apply, ensure the total does not exceed 15%.
Examples
Example 1:
Input:
purchase_history = {"customer_id": "user123", "purchases": 12}
item_price = 50.00
item_category = "Clothing"
Output: 0.05
Explanation: The customer has made 12 purchases, qualifying for the 5% loyalty discount. No other discounts apply, and it's below the 15% cap.
Example 2:
Input:
purchase_history = {"customer_id": "user456", "purchases": 3}
item_price = 120.00
item_category = "Home Goods"
Output: 0.03
Explanation: The customer is not loyal (3 purchases). The item price is $120, triggering the 3% high-value item discount. No other discounts apply.
Example 3:
Input:
purchase_history = {"customer_id": "user789", "purchases": 20}
item_price = 150.00
item_category = "Electronics"
Output: 0.15
Explanation: The customer is loyal (20 purchases) -> 5%. The item price is $150 -> 3%. The item is "Electronics" -> 7%. Total potential discount: 5% + 3% + 7% = 15%. This is exactly the maximum allowed discount.
Example 4:
Input:
purchase_history = {"customer_id": "user007", "purchases": 15}
item_price = 200.00
item_category = "Electronics"
Output: 0.15
Explanation: The customer is loyal (15 purchases) -> 5%. The item price is $200 -> 3%. The item is "Electronics" -> 7%. Total potential discount: 5% + 3% + 7% = 15%. Even though the sum of the individual discounts is 15%, the logic applies them sequentially and then caps the total to 15%.
Example 5:
Input:
purchase_history = {"customer_id": "new_user", "purchases": 0}
item_price = 75.00
item_category = "Books"
Output: 0.00
Explanation: The customer has no purchase history and the item price/category do not trigger any discounts.
Constraints
purchase_historywill be a dictionary containing at least a"purchases"key with an integer value.item_pricewill be a non-negative float or integer.item_categorywill be a string.- The number of historical purchases will be between 0 and 1000.
- The
item_pricewill be between 0.00 and 10000.00. - The function should execute within reasonable time limits for typical use cases. No specific time complexity is mandated, but an efficient solution is preferred.
Notes
- Remember that percentages should be converted to decimal form for calculations (e.g., 5% is 0.05).
- Pay close attention to the order of operations and how discounts stack.
- The maximum discount cap of 15% should be applied after all applicable individual discounts have been summed up.