-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayday.py
40 lines (27 loc) · 1.03 KB
/
payday.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
# This solution separates the ordering of debtors as well
# as the formatting of how their debt should be displayed
# in the report into two separate functions.
from dataclasses import dataclass
from typing import Iterable
@dataclass
class Debtor:
"""Stores the information on a person owing us money"""
name: str
debt: float
def list_debtors(debtors: Iterable[Debtor]) -> None:
for debtor in debtors:
print(f"{debtor.name}: {_format_debt(debtor.debt)}")
def payday(debtors: Iterable[Debtor]) -> None:
list_debtors(_order_by_descending_debt(debtors))
def _order_by_descending_debt(debtors: Iterable[Debtor]) -> Iterable[Debtor]:
return reversed(sorted(debtors, key=lambda debtor: debtor.debt))
def _format_debt(debt: float) -> str:
return str(debt) if debt <= 100.0 else f"!!!{debt}!!!"
if __name__ == "__main__":
payday([
Debtor("Person1", 100.0),
Debtor("Person2", 200.0),
Debtor("Person3", 10.0),
Debtor("Person4", 50.0),
Debtor("Person5", 1250.0)
])