Skip to content

Instantly share code, notes, and snippets.

@sublee
Last active December 15, 2015 00:59
Show Gist options
  • Save sublee/5177221 to your computer and use it in GitHub Desktop.
Save sublee/5177221 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from collections import namedtuple
from ranking import Ranking
Player = namedtuple('Player', ['score', 'weight'])
players = [Player(100, 1), Player(80, 1), Player(80, 2.5), Player(80, 2.5),
Player(70, 1), Player(60, 1.5), Player(60, 1.5)]
def weighted(ranking):
cumulated_weight = 0
effective_cumulated_weight = 0
prev_rank = None
for rank, player in ranking:
if prev_rank != rank:
effective_cumulated_weight = cumulated_weight
yield rank + effective_cumulated_weight, player
prev_rank = rank
cumulated_weight += player.weight - 1
for rank, player in weighted(Ranking(players, key=lambda p: p.score)):
print rank, player
# output:
# 0 Player(score=100, weight=1)
# 1 Player(score=80, weight=1)
# 1 Player(score=80, weight=2.5)
# 1 Player(score=80, weight=2.5)
# 7.0 Player(score=70, weight=1)
# 8.0 Player(score=60, weight=1.5)
# 8.0 Player(score=60, weight=1.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment