Created
December 22, 2020 15:54
-
-
Save bernikr/10d803749e4523773ad210a68fc73f70 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
import bisect | |
import random | |
from itertools import accumulate | |
def towers(inp, n): | |
accumulated_height = [0] + [i for i in accumulate(inp)] | |
average_tower_height = accumulated_height[-1]//n | |
tows = [] | |
last_tower_end_index = 0 | |
for _ in range(n-1): | |
ideal_tower_end = accumulated_height[last_tower_end_index] + average_tower_height | |
index = bisect.bisect(accumulated_height, ideal_tower_end) | |
if abs(accumulated_height[index-1]-ideal_tower_end) < abs(accumulated_height[index]-ideal_tower_end): | |
index = index-1 | |
tows.append(inp[last_tower_end_index+1:index]) | |
last_tower_end_index = index | |
tows.append(inp[last_tower_end_index+1:]) | |
print(average_tower_height) | |
return tows | |
if __name__ == '__main__': | |
inp = [random.randint(20, 100) for _ in range(14)] | |
print(inp) | |
tow = towers(inp, 3) | |
print([sum(t) for t in tow]) | |
for t in tow: | |
print(t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment