statistics.py 文件源码

python
阅读 61 收藏 0 点赞 0 评论 0

项目:eoj3 作者: ultmaster 项目源码 文件源码
def get_contest_rank_list(contest: Contest, privilege=False):
    def _calculate():

        def find_key(tup):
            if contest.penalty_counts:
                return tup[1]['score'], -tup[1]['penalty']
            else:
                return tup[1]['score']

        items = sorted(get_all_contest_participants_detail(contest, privilege=privilege).items(),
                       key=find_key, reverse=True)
        ans = []  # ans = [(user_id, rank), ...]
        last_item = None
        for idx, item in enumerate(items, start=1):
            if last_item and find_key(item) == find_key(last_item):
                ans.append((item[0], ans[-1][1]))
            else:
                ans.append((item[0], idx))
            last_item = item
        return ans

    if not privilege:
        # Try to use cache
        cache_name = PARTICIPANT_RANK_LIST.format(contest=contest.pk)
        t = cache.get(cache_name)
        if t is None:
            t = _calculate()
            cache.set(cache_name, t, FORTNIGHT * uniform(0.6, 1))
            d = {PARTICIPANT_RANK.format(contest=contest.pk, user=user): rank for user, rank in t}
            cache.set_many(d, FORTNIGHT * uniform(0.6, 1))
        return t
    else:
        return _calculate()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号