JoelSjogren icon

Untitled

JoelSjogren | PRO | 11/01/20 10:58:33 AM UTC | 0 ⭐ | 550 👁️ | Never ⏰ | []
text |

710 B

|

None

|

0 👍

/

0 👎

import numpy as np
 races = (
    (0,4,2,5,1),
    (2,4,6,7,8),
    (6,4,3,1,2),
    (7,1,3,2),
    (4,0,6,5,8),
    (9,0,8,5)
)
 assert all(len(r) == len(set(r)) for r in races)
 racers = set(sum(races, ()))
 def won_against(i, j):
    out = np.array([0] * len(racers))
    out[i] = 1
    out[j] = -1
    return out
 A = np.array([won_against(r[i], r[i+1])
              for r in races
              for i in range(len(r) - 1)])
b = np.array([1] * A.shape[0])
 # solve A*scores = b with least squares
scores = np.linalg.lstsq(A, b, rcond=None)[0]
ranking = list(reversed(np.argsort(scores)))
print(f"ranking: {ranking}")
 """
 box% python main.py 
ranking: [9, 0, 6, 4, 7, 3, 2, 1, 5, 8]
 """
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎