Skip to content

Instantly share code, notes, and snippets.

@aniline
Last active January 29, 2017 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aniline/aca3da5fda719c1c0f2b25c2fcf0d28e to your computer and use it in GitHub Desktop.
Save aniline/aca3da5fda719c1c0f2b25c2fcf0d28e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import random, sys
def distribute(parts, value):
total = sum(parts)
min_value = len(parts)
if ((value < min_value) or (value > total)):
return None
cur = [1]*len(parts)
while sum(cur) < value:
bits = int(random.uniform(1,(1<<len(cur))))
for n in range(len(cur)):
inc = bits & 1
bits = bits >> 1
if (cur[n]+inc <= parts[n]) and ((sum(cur)+inc) <= value):
cur[n] = cur[n] + inc
return cur
def do_rev_rubricks(fn):
f = open(fn)
lns = f.readlines()
f.close()
ratios = map(int, lns[0].split(',')[:-1])
print lns[0], lns[1][:-1]
for l in lns[2:]:
v = int(l[:-1].split(',')[-1])
rrub = distribute(ratios, v)
print ','.join(map(str, rrub+[v]))
try:
if (len(sys.argv) < 2):
raise Exception("No File Given")
do_rev_rubricks(sys.argv[1])
except Exception, e:
print "Error, ", e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment