Skip to content

Instantly share code, notes, and snippets.

@rshk
Created July 5, 2017 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rshk/edaf631e05d8c5981f36a616386c4ac4 to your computer and use it in GitHub Desktop.
Save rshk/edaf631e05d8c5981f36a616386c4ac4 to your computer and use it in GitHub Desktop.
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