-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpayday.py
36 lines (28 loc) · 1.11 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
# This code allows you to store information on people owing you money
# and then list all those people in a ordered and formatted way in the terminal.
# Try to improve the `payday` function by splitting the logic into smaller 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 payday(debtors: Iterable[Debtor]) -> None:
# First, we sort the debtors according to their debt
# such that those with the highest debt are printed first
ordered = reversed(sorted(debtors, key=lambda debtor: debtor.debt))
# Then we print the debtors, highlighting debts above 100 by exclamation marks
for debtor in ordered:
if debtor.debt > 100.0:
print(f"{debtor.name}: !!!{debtor.debt}!!!")
else:
print(f"{debtor.name}: {debtor.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)
])