Story
Your online store likes to give out coupons for special occasions. Some customers try to cheat the system by entering invalid codes or using expired coupons.
Task
Your mission: Write a function called checkCoupon which verifies that a coupon code is valid and not expired.
A coupon is no more valid on the day AFTER the expiration date. All dates will be passed as strings in this format: "MONTH DATE, YEAR".
Examples:
checkCoupon("123", "123", "July 9, 2015", "July 9, 2015") == True
checkCoupon("123", "123", "July 9, 2015", "July 2, 2015") == False
Solution:
from datetime import datetime
def validation_date(func):
def wrapper(*args):
data = list(args)
expiration_date = datetime.strptime(data.pop(), "%B %d, %Y")
current_date = datetime.strptime(data.pop(), "%B %d, %Y")
is_valid = current_date <= expiration_date
return func(entered_code = data[0], correct_code = data[1], is_valid = is_valid)
return wrapper
@validation_date
def check_coupon(*, entered_code: str, correct_code: str, is_valid: bool) -> bool:
return entered_code is correct_code and is_valid
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
0908. Sorted? yes? no? how? (0) | 2022.09.08 |
---|---|
0907. Count by X (0) | 2022.09.07 |
0905. Training JS #7: if..else and ternary operator (0) | 2022.09.05 |
0904. No zeros for heros (0) | 2022.09.04 |
0903. L1: Bartender, drinks! (0) | 2022.09.03 |