Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Python by Checkraise ( 14 years ago )
people = ['AKl', 'AY', 'AKa', 'Mi', 'Li']

items = [ {'name' : 'good_beer',  'who_needs_it' : ['AKl', 'AY'],   'cost' : 841,  'who_payed' : 'AKl'}, 
   {'name' : 'other_beer', 'who_needs_it' : ['AKl', 'AY', 'AKa'],  'cost' : 1534,  'who_payed' : 'AKl'},
   {'name' : 'ugol',   'who_needs_it' : people,     'cost' : 218,  'who_payed' : 'AKl'},
   {'name' : 'wine',   'who_needs_it' : ['AKl', 'AY', 'AKa'],  'cost' : 208,  'who_payed' : 'AKl'},

   {'name' : 'sugar_3',  'who_needs_it' : ['Mi'],  'cost' : 57,   'who_payed' : 'Mi'}, 
   {'name' : 'sugar_1',  'who_needs_it' : people,  'cost' : 19,   'who_payed' : 'Mi'},
   {'name' : 'tea_3',   'who_needs_it' : ['Mi'],  'cost' : 240.75,  'who_payed' : 'Mi'},
   {'name' : 'tea_1',   'who_needs_it' : people,  'cost' : 80.25,  'who_payed' : 'Mi'},
   {'name' : 'baguet',  'who_needs_it' : ['AY'],  'cost' : 70,   'who_payed' : 'Mi'},
   {'name' : 'chak_chak',  'who_needs_it' : ['AKl'],  'cost' : 59,   'who_payed' : 'Mi'},
   {'name' : 'always',  'who_needs_it' : ['Li'],  'cost' : 188,   'who_payed' : 'Mi'},
   {'name' : 'toothbrush', 'who_needs_it' : ['Mi', 'Li'],  'cost' : 440,  'who_payed' : 'Mi'},
   {'name' : 'other_stuff', 'who_needs_it' : people,  'cost' : 2746,   'who_payed' : 'Mi'},
  ]

3900 - 57-19-240.75-80.25-70-59-188-440
  
def calc(items):
 balance = {}
 for item in items:
  part_cost = float(item['cost']) / len(item['who_needs_it'])
  payer = item['who_payed']
  for needy in item['who_needs_it']:
   if payer in balance.keys():
    if needy in balance[payer].keys():
     balance[payer][needy] += part_cost
    else:
     balance[payer][needy] = part_cost
   else:
    balance[payer] = {needy : part_cost}
 
 for payer in balance.keys():
  for needy in balance[payer].keys():
   if needy in balance.keys() and payer in balance[needy].keys():
    balance[payer][needy] -= balance[needy][payer]
    del balance[needy][payer]
    
 for payer in balance.keys():
  for needy in balance[payer].keys():
   print '%s debt to %s is %f' % (needy, payer, balance[payer][needy])
    
 
 
calc(items)

 

Revise this Paste

Your Name: Code Language: