Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 3rd, 2012  |  syntax: Python  |  size: 1.30 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. k = 3
  3.  
  4. scores = [
  5.     [ 0,  1, -1,  3],
  6.     [ 1,  0,  2, -4],
  7.     [-1,  2,  0,  2],
  8.     [ 3, -4,  2,  0],
  9. ]
  10.  
  11. # each individual starts as his own team
  12. # since we don't have names, just use indices
  13. teams = [[i,] for i in list(xrange(len(scores)))]
  14.  
  15. def cool_as_fuck(k, scores, teams):
  16.  
  17.     while True:
  18.         max_score = None
  19.         best_team = None
  20.         n_scores = len(scores)
  21.  
  22.         for i in xrange(n_scores):
  23.             for j in xrange(n_scores):
  24.                 if i != j and (not max_score or scores[i][j] > max_score):
  25.                     max_score = scores[i][j]
  26.                     best_team = (i, j)
  27.  
  28.         i, j = best_team
  29.         best_team = teams[i] + teams[j]
  30.  
  31.         if len(best_team) >= k:
  32.             return best_team
  33.  
  34.         del teams[j]
  35.         del teams[i]
  36.         teams.append(best_team)
  37.  
  38.         new_scores = []
  39.         for a in xrange(n_scores):
  40.             if a != i and a != j:
  41.                 new_score = (scores[a][i] + scores[a][j]) / 2.0
  42.                 del scores[a][j]
  43.                 del scores[a][i]
  44.                 scores[a].append(new_score)
  45.                 new_scores.append(new_score)
  46.         del scores[j]
  47.         del scores[i]
  48.         new_scores.append(0)
  49.         scores.append(new_scores)
  50.  
  51. print(cool_as_fuck(k, scores, teams))