Skip to content

Instantly share code, notes, and snippets.

@AhmedAbouelkher
Created July 7, 2022 12:35
Show Gist options
  • Save AhmedAbouelkher/858a8bc4fe6ce6c282e43bb418a4ce22 to your computer and use it in GitHub Desktop.
Save AhmedAbouelkher/858a8bc4fe6ce6c282e43bb418a4ce22 to your computer and use it in GitHub Desktop.
Toptal Quiz 3 Solution
def solution(S: float, B: list[float]) -> list[float]:
total = 0
for i, v in enumerate(B):
total += v
B = sorted(B, reverse=True)
R = list(len(B) * [0])
for i, num in enumerate(B):
if i > 0:
S = abs(S - R[i - 1])
result = S * num / total
R[i] = round(result, 2)
total -= num
return R
if __name__ == '__main__':
print(solution(300.01, [300, 200, 100]))
print(solution(1.00, [0.05, 1.00]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment