Created
June 30, 2017 15:30
-
-
Save Arti3DPlayer/e7f629b286da3fa782f9261b23f12ad4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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