Created
March 4, 2025 01:13
-
-
Save osdevisnot/577a437a53bd4e451ec0bf85bfe7e3a3 to your computer and use it in GitHub Desktop.
Cassidy Newsletter - Issue #394 - Discount Problem Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
FORMAT = "%Y-%m-%d" | |
def calculate_price(closing_date, visit_date, original_price): | |
closing_date = datetime.strptime(closing_date, FORMAT) | |
visit_date = datetime.strptime(visit_date, FORMAT) | |
if visit_date > closing_date: # no discount if visit is after closing | |
return original_price | |
diff = (closing_date - visit_date) | |
weeks = diff.days // 7 | |
price = original_price * (0.90 ** weeks) # exponential decay - Price[n] = Original Price x (0.90)n | |
# without using the exponential decay, one can also loop through weeks | |
# price = original_price | |
# for _ in range(weeks): | |
# price *= 0.90 | |
return round(price, 2) | |
print(calculate_price('2025-04-01', '2025-03-03', 100)) # 4 weeks of discount > 65.61 | |
print(calculate_price('2025-04-01', '2025-03-15', 50)) # 2 weeks of discount > 40.5 | |
print(calculate_price('2025-04-01', '2025-04-15', 75)) # No discount (visit after closing) > 75 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment