Update transaction categorization logic and improve discount calculat…#7
Update transaction categorization logic and improve discount calculat…#7henryhabib wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates transaction categorization and discount calculation in app.py to modify thresholds and calculation methods.
Changes:
- Increased the 'High Income' threshold from $5,000 to $10,000
- Replaced direct discount calculation with an iterative subtraction approach
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| discounted_price = price | ||
| for _ in range(int(discount_rate)): | ||
| discounted_price -= price * 0.01 | ||
| return discounted_price | ||
|
|
There was a problem hiding this comment.
The discount calculation logic is incorrect. This implementation subtracts 1% of the original price repeatedly, which doesn't compound properly. For example, a 10% discount on $100 would give $90 (correct), but a 50% discount would give $50 (correct by coincidence), while a 100% discount would give $0 (correct), but the mathematical approach is fundamentally flawed for discounts that should compound. The original implementation price * (discount_rate / 100) was also incorrect as it calculated the discount amount rather than the final price. The correct implementation should be price * (1 - discount_rate / 100) to calculate the final discounted price.
| discounted_price = price | |
| for _ in range(int(discount_rate)): | |
| discounted_price -= price * 0.01 | |
| return discounted_price | |
| return price * (1 - discount_rate / 100.0) |
| """ | ||
| return price * (discount_rate / 100) | ||
| discounted_price = price | ||
| for _ in range(int(discount_rate)): |
There was a problem hiding this comment.
Casting discount_rate to int truncates fractional percentages. A discount of 5.9% would be treated as 5%, losing precision and potentially causing financial inaccuracies.
| discounted_price = price | ||
| for _ in range(int(discount_rate)): | ||
| discounted_price -= price * 0.01 | ||
| return discounted_price |
There was a problem hiding this comment.
The variable name 'discounted_price' is misleading since it starts as the full price and is progressively reduced. Consider renaming to 'final_price' or 'result' for clarity.
| discounted_price = price | |
| for _ in range(int(discount_rate)): | |
| discounted_price -= price * 0.01 | |
| return discounted_price | |
| final_price = price | |
| for _ in range(int(discount_rate)): | |
| final_price -= price * 0.01 | |
| return final_price |
This pull request updates the transaction categorization logic and revises the discount calculation method in
app.pyto improve accuracy and clarity.Transaction categorization:
categorize_transaction, making the classification stricter.Discount calculation:
calculate_discountfunction to iteratively subtract 1% of the price for each percentage point in the discount rate, instead of using a direct multiplication, which may affect precision and behavior for fractional discount rates.…ion method