-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporter.py
37 lines (31 loc) · 1.35 KB
/
reporter.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
class Reporter(object):
"Builds report message for given debts dict"
def report(self, people_debts):
"""Builds report message for given debts dict.
Args:
people_debts: Dict where key is person and value if array of debts.
Returns:
Human understandable string describing people debts.
"""
keys_sorted = sorted(people_debts.keys())
report = []
for person in keys_sorted:
debts = people_debts[person]
person_messages = []
total_debt = 0
if debts:
for recipient, amount in debts:
person_messages.append(self.owe_message(person, amount, recipient))
total_debt += amount
else:
person_messages.append(self.no_debts_message(person))
if total_debt > 0:
person_messages.append(self.total_debt_message(person, total_debt))
report.append("\n".join(person_messages))
return "\n\n".join(report)
def owe_message(self, person, amount, recipient):
return "%s owes %s to %s" % (person, amount, recipient)
def no_debts_message(self, person):
return "%s has no debts." % person
def total_debt_message(self, person, total_debt):
return "%s owes %s in total." % (person, total_debt)