Skip to content

Instantly share code, notes, and snippets.

@zeako
Created March 21, 2015 15:25
Show Gist options
  • Save zeako/f659d0d4fc3e2dabf801 to your computer and use it in GitHub Desktop.
Save zeako/f659d0d4fc3e2dabf801 to your computer and use it in GitHub Desktop.
from math import floor, ceil
def distribute(z, *args):
"""
returns distribution of z over args with natural numbers
and each arg >= 1
:param z: z >= len(args)
:param args: len(args) >= 2
:return: distribution of z overs args ratio
"""
original_z = z # for ratio comparsion
n_sum = sum(*args)
sorted_args = sorted(*args)
solution = []
for i, arg in enumerate(sorted_args):
solution.append(floor(original_z * (arg / n_sum)))
z -= solution[i]
print(solution)
z_2 = z # for second ratio comparison
for i, arg in enumerate(solution):
if arg == 0:
solution[i] += 1
z -= 1
solution[i] += floor(z_2 * (sorted_args[i]) / n_sum)
z -= floor(z_2 * (sorted_args[i]) / n_sum)
solution[-1] += z
return solution
if __name__ == '__main__':
print(distribute(10000, [1, 2, 3, 4, 6, 10])) # prints [86, 172, 258, 344, 517, 8623]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment