Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_mod(a, b):
- return a / b - a // b
- places = 450
- votes = 0
- parties = []
- party_votes = dict()
- with open("input.txt", "r") as file:
- for line in file:
- pvotes, party_name = line[::-1].split(" ", 1)
- pvotes = int(pvotes[::-1])
- party_name = party_name[::-1]
- parties.append(party_name)
- party_votes[party_name] = pvotes
- votes += pvotes
- # первое высчитываем избирательное частное
- d = votes / places
- # определяем количество мест, занятое каждой партией
- filled = 0 # сколько всего занято мест
- party_places = dict() # под каждую партию
- for party, votes in party_votes.items():
- pplaces = votes // d
- filled += pplaces
- party_places[party] = pplaces
- # проверяем оставшиеся места
- if places - filled > 0:
- sorted_parties = list(sorted(
- parties,
- key=lambda party: (get_mod(party_votes[party], d), party_votes[party])
- ))
- while filled < places:
- party = sorted_parties.pop()
- filled += 1
- party_places[party] += 1
- for party in parties:
- print(party, int(party_places[party]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement