Last active
December 1, 2018 20:04
-
-
Save felipessalvatore/ecb46c981076a940af95e0e18eea31f9 to your computer and use it in GitHub Desktop.
Script to distribut costs over a dict of people
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
p = {} | |
p["Felipe"] = 140 | |
p["Pedri"] = 150 | |
p["Ale"] = 495 | |
p["Amanda"] = 0 | |
p["Andre"] = 0 | |
p["Caio"] = 0 | |
def find_above_value(people_dict, upper_bound): | |
select = None | |
for p in people_dict: | |
if people_dict[p] > upper_bound: | |
select = p | |
return select | |
def distr_cost(people_dict): | |
total_cost = np.sum([people_dict[p] for p in people_dict]) | |
cost_per_person = total_cost / len(people_dict) | |
transactions = [] | |
second_person = find_above_value(people_dict, cost_per_person) | |
for person in people_dict: | |
while people_dict[person] < cost_per_person: | |
if second_person is None: | |
break | |
trans = cost_per_person - people_dict[person] | |
if people_dict[second_person] - trans < cost_per_person: | |
trans = people_dict[second_person] - cost_per_person | |
transactions.append("{} -> {} {:.2f}".format(person, second_person, trans)) | |
people_dict[person] += trans | |
people_dict[second_person] += -trans | |
second_person = find_above_value(people_dict, cost_per_person) | |
return transactions | |
print(p) | |
print() | |
print(distr_cost(p)) | |
print() | |
print(p) | |
assert np.std([p[d] for d in p]) < 0.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment