Guest User

Untitled

a guest
Apr 20th, 2019
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. import requests
  2.  
  3. contests = [1146]
  4.  
  5. # [1, rank_start] get tshirts with no random
  6. # [rank_start + 1, rand_end] get random tshirts
  7. rank_start = 1
  8. rank_end = 250
  9.  
  10. n_random_tshirts = 25
  11.  
  12. def is_eligible(contest, party_name, rank, points, problems, last_submission_time):
  13.     return (1 <= rank_start and rank <= rank_end)
  14.  
  15. def get_party_name(party):
  16.     if 'teamName' in party:
  17.         return party['teamName']
  18.     else:
  19.         return party['members'][0]['handle']
  20.  
  21. class Party:
  22.     def __init__(self, contest, name, rank, points, last_submission_time):
  23.         self.contest = contest
  24.         self.name = name
  25.         self.rank = rank
  26.         self.points = points
  27.         self.last_submission_time = last_submission_time
  28.  
  29.     def __lt__(self, other):
  30.         if self.contest != other.contest:
  31.             return self.contest < other.contest
  32.         if self.rank != other.rank:
  33.             return self.rank < other.rank
  34.         if self.last_submission_time != other.last_submission_time:
  35.             return self.last_submission_time < other.last_submission_time
  36.         if self.name != other.name:
  37.             return self.name < other.name
  38.         return False
  39.  
  40. eligible = []
  41.  
  42. for contest in contests:
  43.     rows_to_fetch = 20000
  44.     r = requests.get('https://codeforces.com/api/contest.standings?contestId=%d&from=1&count=%d&showUnofficial=false' % (contest, rows_to_fetch))
  45.     if r.status_code != 200 or r.json()['status'] != 'OK':
  46.         print('Error: failed to fetch results of contest %d.' % contest)
  47.         exit()
  48.     results = r.json()['result']['rows']
  49.     if len(results) == rows_to_fetch:
  50.         print('Error: results are too long in contest %d, increase limit.' % contest)
  51.         exit()
  52.     for row in results:
  53.         party_name = get_party_name(row['party'])
  54.         rank = row['rank']
  55.         points = row['points']
  56.         problems = 0
  57.         last_submission_time = 0
  58.         for problem in row['problemResults']:
  59.             if problem['points'] > 0:
  60.                 problems += 1
  61.                 last_submission_time = max(last_submission_time, problem['bestSubmissionTimeSeconds'])
  62.         if is_eligible(contest, party_name, rank, points, problems, last_submission_time):
  63.             eligible.append(Party(contest, party_name, rank, points, last_submission_time))
  64.  
  65. eligible = sorted(eligible)
  66.  
  67. for i in range(1, len(eligible)):
  68.     if not eligible[i - 1] < eligible[i]:
  69.         print('Error: unable to sort eligible at positions {} {}.'.format(i, i, i + 1))
  70.         exit()
  71.  
  72. print('Eligible for tshirts are:')
  73. for idx, party in enumerate(eligible):
  74.     print('{} {} {} {} {}'.format(idx + 1, party.contest, party.rank, party.name, party.last_submission_time))
  75.  
  76.  
  77. seed = eligible[0].points
  78.  
  79. print('Total %d eligible, seed is %d.' % (len(eligible), seed))
  80.  
  81.  
  82. import subprocess
  83. testlib_rand = subprocess.run(['./randgen', str(seed), str(len(eligible)), str(rank_start - 1),    str(n_random_tshirts)], stdout=subprocess.PIPE)
  84. winners = list(map(int, testlib_rand.stdout.split()))
  85.  
  86. print()
  87. print('Winners are:')
  88. for place in winners:
  89.     print('{} {} {} {}'.format(place, eligible[place - 1].contest, eligible[place - 1].rank, eligible[place - 1].name))
  90.  
  91. print()
  92. print('| List place | Contest | Rank | Name |')
  93. print('|--|--|--|--|')
  94. for place in winners:
  95.     print('| {} | {} | {} | [user:{}] |'.format(place, eligible[place - 1].contest, eligible[place - 1].rank, eligible[place - 1].name))
Advertisement
Add Comment
Please, Sign In to add comment