Skip to content

Instantly share code, notes, and snippets.

@Arti3DPlayer
Created June 30, 2017 15:30
Show Gist options
  • Save Arti3DPlayer/e7f629b286da3fa782f9261b23f12ad4 to your computer and use it in GitHub Desktop.
Save Arti3DPlayer/e7f629b286da3fa782f9261b23f12ad4 to your computer and use it in GitHub Desktop.
sum_ = sum(result.values())
for i in result:
try:
result[i] = int((kwargs[i] * 100 / sum_).quantize(Decimal('.0000'), rounding=ROUND_UP))
except InvalidOperation:
result[i] = 0
total_percents = sum(result.values())
_sorting = sorted(result.keys(), key=lambda key: result[key], reverse=True)
result_count = len(result)
delta = 100 - total_percents
step = 1 if delta > 0 else -1
for n in range(0, delta, step):
index = abs(n)
if index > result_count - 1:
index = (index % result_count) - 1
result[_sorting[index]] += step
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment