Created
July 5, 2017 12:41
-
-
Save rshk/edaf631e05d8c5981f36a616386c4ac4 to your computer and use it in GitHub Desktop.
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
import sys | |
from itertools import combinations | |
from collections import defaultdict | |
from random import shuffle | |
def pick_one(): | |
while True: | |
foo = input('Which one? ').strip().lower() | |
if foo == 'a': | |
return (1, 0) | |
if foo == 'b': | |
return (0, 1) | |
print('Invalid choice') | |
def expected(A, B): | |
return 1 / (1 + 10 ** ((B - A) / 400)) | |
def elo(old, exp, score, k=32): | |
return old + k * (score - exp) | |
def main(): | |
if len(sys.argv) > 1: | |
rows = sys.argv[1:] | |
else: | |
rows = filter(None, list(x.strip() for x in sys.stdin)) | |
rows = list(rows) | |
scores = defaultdict(lambda: 1200) | |
pairs = [(a, b) for (a, b) in combinations(rows, 2)] | |
shuffle(pairs) | |
for idx, (a, b) in enumerate(pairs): | |
print('[{}/{}]'.format(idx + 1, len(pairs))) | |
print('A - {}\nB - {}'.format(a, b)) | |
oldscore_a = scores[a] | |
oldscore_b = scores[b] | |
score_a, score_b = pick_one() | |
scores[a] = elo( | |
oldscore_a, | |
expected(oldscore_a, oldscore_b), | |
score_a) | |
scores[b] = elo( | |
oldscore_b, | |
expected(oldscore_b, oldscore_a), | |
score_b) | |
print() | |
print(' Final score '.center(80, '-')) | |
score_items = sorted( | |
scores.items(), key=lambda x: (x[1], x[0]), reverse=True) | |
for name, score in score_items: | |
print('{:6.0f}: {}'.format(score, name)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment