
Untitled
By: a guest on
Aug 3rd, 2012 | syntax:
Python | size: 1.30 KB | hits: 14 | expires: Never
k = 3
scores = [
[ 0, 1, -1, 3],
[ 1, 0, 2, -4],
[-1, 2, 0, 2],
[ 3, -4, 2, 0],
]
# each individual starts as his own team
# since we don't have names, just use indices
teams = [[i,] for i in list(xrange(len(scores)))]
def cool_as_fuck(k, scores, teams):
while True:
max_score = None
best_team = None
n_scores = len(scores)
for i in xrange(n_scores):
for j in xrange(n_scores):
if i != j and (not max_score or scores[i][j] > max_score):
max_score = scores[i][j]
best_team = (i, j)
i, j = best_team
best_team = teams[i] + teams[j]
if len(best_team) >= k:
return best_team
del teams[j]
del teams[i]
teams.append(best_team)
new_scores = []
for a in xrange(n_scores):
if a != i and a != j:
new_score = (scores[a][i] + scores[a][j]) / 2.0
del scores[a][j]
del scores[a][i]
scores[a].append(new_score)
new_scores.append(new_score)
del scores[j]
del scores[i]
new_scores.append(0)
scores.append(new_scores)
print(cool_as_fuck(k, scores, teams))