Last active
September 15, 2016 19:00
-
-
Save razzius/943908ffb9a787965fee007d169c1666 to your computer and use it in GitHub Desktop.
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
def truncate_amounts_based_on_maximum(amounts, maximum): | |
"""Take from amounts until maximum is reached, possibly truncating the final amount. | |
>>> truncate_amounts_based_on_maximum([10, 10], 15) | |
[10, 5] | |
>>> truncate_amounts_based_on_maximum([100], 0) | |
[] | |
>>> truncate_amounts_based_on_maximum([1,1,1], 1) | |
[1] | |
""" | |
truncated = [] | |
for amount in amounts: | |
maximum_reached = sum(truncated) >= maximum | |
if maximum_reached: | |
return truncated | |
remaining_available = maximum - sum(truncated) | |
truncated_amount = min(remaining_available, amount) | |
truncated.append(truncated_amount) | |
return truncated |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment