Task
Suppose that p
pepole have to split a bill; each of them is identified by a triple (Name, n, k)
made up of:
Name
: the name;n
: the amount she/he has to pay;k
: the amount she/he actually paid.
The challenge here is to find out how much who owes whom.
Assumptions
Input and output can be in any convenient format.
p
\$\in \mathbb{N}, \,\;\;\$n
\$\in \mathbb{N}^{+},\;\$k
\$\in \mathbb{N}.\$p
\$\gt 1.\$Names are unique strings of arbitrary length, composed of lower case alphabet characters.
Solution
The solution is represented by the minimum set of transactions among the p
people; in particular they are triples (from, to, amount)
from
:name
of the person that gives money;to
:name
of the person that receives money;amount
: amount of money of the transaction.
NOTE: The sum of all the debts (n
) can differ from the sum of all the already payed amounts (k
). In this case, you must add in the output ('owner', Name, amount)
or (Name, 'owner', amount)
in the format you have chosen. Any name will never be owner
.The string 'owner' is flexible.
If several mimimum sets exist, select the one with the minimum sum of all the transaction amounts (absolute values); in case of a tie, choose one of them.
Test Cases:
inputs(Name,n,k):
[('a',30,40),('b',40,50),('c',30,15)]
[('a',30,30),('b',20,20)]
[('a',30,100),('b',30,2),('c',40,0)]
[('a',344,333),('b',344,200),('c',2,2)]
[('a',450,400),('b',300,300),('c',35,55)]
outputs(from, to, amount):
[('c','a',10),('c','b',5),('owner','b',5)] or [('c','b',10),('c','a',5),('owner','a',5)]
[]
[('owner','a',2),('b','a',28),('c','a',40)] PS: [('owner','a',2),('b','a',68),('c','b',40)] has the same number of transactions, but it is not a valid answer, because the total amount of its transaction is greater than that of the proposed solution.
[('a','owner',11),('b','owner',144)]
[('a','owner',30),('a','c',20)]
This is code-golf: shortest code wins.