-
-
Save dspasic/6b7ab7ace962dddc6e91f5de4c1b2cf3 to your computer and use it in GitHub Desktop.
Separate these numbers into 4 groups. The sum of every group must be equal.
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
n = [11, 14, 3, 17, 7, 1, 16, 16, 4, 18, 2, 13, 5, 4, 5, 12] | |
g = 4 | |
a = {i:True for i in range(len(n))} | |
target_sum = sum(n)/g | |
def compute_pairs(n): | |
frame = 0 | |
size = len(n)-int(len(n)/g) | |
p = {} | |
while frame < size: | |
it = frame + 1 | |
p[frame] = {} | |
while it < len(n): | |
if it != frame: | |
cs = n[frame] + n[it] | |
if cs < target_sum: | |
p[frame][it] = n[frame] + n[it] | |
it += 1 | |
frame = frame + 1 | |
return p | |
def determine(s): | |
r = [] | |
y = 0 | |
while y < len(s)-1: | |
x = y + 1 | |
while x < len(s): | |
current = s[y].items() | |
partner = s[x].items() | |
for yk,yv in current: | |
for xk,xv in partner: | |
if x == yk: | |
continue | |
if a[x] is True and a[xk] is True and a[y] is True and a[yk] is True: | |
total = xv + yv | |
if total == target_sum: | |
a[x] = a[y] = a[xk] = a[yk] = False | |
r.append({y:n[y], yk:n[yk], x:n[x], xk:n[xk]}) | |
x += 1 | |
y += 1 | |
return r | |
pairs = compute_pairs(n) | |
result = determine(pairs) | |
print(result) | |
# time complexity O(log(n^n-1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment