Skip to content

Instantly share code, notes, and snippets.

@HauptJ
Last active June 25, 2020 04:22
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 HauptJ/540a5890c09f3518541216e92c70c336 to your computer and use it in GitHub Desktop.
Save HauptJ/540a5890c09f3518541216e92c70c336 to your computer and use it in GitHub Desktop.
def find_grants_cap(grantsArray, newBudget):
cap = float(0)
grantsArray.sort(reverse = True)
# pad the array with a zero at the end to cover the case where 0 <= cap <= grantsArray[i]
grantsArray.append(0)
# calculate the total amount we need to cut back to meet the reduced budget
surplus = sum(grantsArray) - newBudget
# if there is nothing to cut, return the highest grant as the cap
if surplus <= 0:
return grantsArray[0]
for i in range(0, len(grantsArray)-1, 1):
surplus -= (i+1)*(grantsArray[i]-grantsArray[i+1])
if surplus <= 0:
break
cap = grantsArray[i+1] + (-1*surplus/float((i+1)))
return cap
"""
are you still there?
I'm not able to hear you, its showing connecting to your peer
try refreshing
ok
2 2 50 100 120
2 2 100 50 120
2 39 100 50
38 47
38+(36/3)+(36/3) = cap
input: grantsArray = [2, 100, 50, 120, 1000], newBudget = 190
sum = 1272/5 = 254.4
190/5 = 38
38+(36/4) = 47
# cap is avg of new budget
output: 47
# and given this cap the new grants array would be
# [2, 47, 47, 47, 47]. Notice that the sum of the
# new grants is indeed 190
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment