Advertisement
AlexG2230954

Untitled

May 31st, 2022
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. def get_mod(a, b):
  2. return a / b - a // b
  3.  
  4.  
  5. places = 450
  6. votes = 0
  7. parties = []
  8. party_votes = dict()
  9.  
  10. with open("input.txt", "r") as file:
  11. for line in file:
  12. pvotes, party_name = line[::-1].split(" ", 1)
  13. pvotes = int(pvotes[::-1])
  14. party_name = party_name[::-1]
  15.  
  16. parties.append(party_name)
  17. party_votes[party_name] = pvotes
  18. votes += pvotes
  19.  
  20. # первое высчитываем избирательное частное
  21. d = votes / places
  22.  
  23. # определяем количество мест, занятое каждой партией
  24. filled = 0 # сколько всего занято мест
  25. party_places = dict() # под каждую партию
  26.  
  27. for party, votes in party_votes.items():
  28. pplaces = votes // d
  29. filled += pplaces
  30. party_places[party] = pplaces
  31.  
  32. # проверяем оставшиеся места
  33. if places - filled > 0:
  34. sorted_parties = list(sorted(
  35. parties,
  36. key=lambda party: (get_mod(party_votes[party], d), party_votes[party])
  37. ))
  38.  
  39. while filled < places:
  40. party = sorted_parties.pop()
  41. filled += 1
  42. party_places[party] += 1
  43.  
  44.  
  45. for party in parties:
  46. print(party, int(party_places[party]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement