Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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]
- """
Advertisement