Advertisement
AlexG2230954

ДЗ. Задание 4

May 22nd, 2022
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. from collections import defaultdict
  2. import math
  3.  
  4.  
  5. def get_party():
  6.     *party_name, votes = input().split()
  7.     return " ".join(party_name), int(votes)
  8.  
  9.  
  10. def get_total_vote(parties):
  11.     return sum(party[1] for party in parties)
  12.  
  13.  
  14. def first_round(parties, first_private):
  15.     return {party: int(votes // first_private) for party, votes in parties}
  16.  
  17.  
  18. def second_round(parties, first_private):
  19.     mods = [(math.modf(votes / first_private)[0], votes, party) for party, votes in parties]
  20.     mods.sort()
  21.  
  22.     return [party for *_, party in reversed(mods)]
  23.  
  24.  
  25. parties = []
  26. places = defaultdict(int)
  27.  
  28. while True:
  29.     try: party = get_party()
  30.     except EOFError: break
  31.     parties.append(party)
  32.  
  33. total_vote = get_total_vote(parties)
  34. first_private = total_vote / 450
  35. places.update(first_round(parties, first_private))
  36.  
  37. add_places = second_round(parties, first_private)
  38. need_places = 450 - sum(places.values())
  39.  
  40.  
  41. for party in add_places:
  42.     if need_places == 0: break
  43.  
  44.     places[party] += 1
  45.     need_places -= 1
  46.  
  47. for party, _ in parties:
  48.     print(party, places[party])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement