-
Notifications
You must be signed in to change notification settings - Fork 7
/
1_do_one_thing_after.py
51 lines (40 loc) · 1.14 KB
/
1_do_one_thing_after.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from dataclasses import dataclass
from datetime import datetime
def luhn_checksum(card_number: str) -> bool:
def digits_of(number: str) -> list[int]:
return [int(d) for d in number]
digits = digits_of(card_number)
odd_digits = digits[-1::-2]
even_digits = digits[-2::-2]
checksum = 0
checksum += sum(odd_digits)
for digit in even_digits:
checksum += sum(digits_of(str(digit * 2)))
return checksum % 10 == 0
@dataclass
class Customer:
name: str
phone: str
cc_number: str
cc_exp_month: int
cc_exp_year: int
cc_valid: bool = False
def validate_card(customer: Customer) -> bool:
customer.cc_valid = (
luhn_checksum(customer.cc_number)
and datetime(customer.cc_exp_year, customer.cc_exp_month, 1) > datetime.now()
)
return customer.cc_valid
def main() -> None:
alice = Customer(
name="Alice",
phone="2341",
cc_number="1249190007575069",
cc_exp_month=1,
cc_exp_year=2024,
)
is_valid = validate_card(alice)
print(f"Is Alice's card valid? {is_valid}")
print(alice)
if __name__ == "__main__":
main()