fork download
  1. #!/usr/bin/env python
  2. import fileinput
  3. from collections import Counter
  4. from itertools import takewhile
  5.  
  6. def read_parties(lines):
  7. next(lines) # skip PARTIES header
  8. return takewhile(lambda s: 'VOTES:' not in s, lines)
  9.  
  10.  
  11. def get_elected_parties(parties, threshold=0.07):
  12. votes = Counter(parties)
  13. min_nvotes = threshold * sum(votes.values())
  14. return {party for party, nvotes in votes.items() if nvotes >= min_nvotes}
  15.  
  16. lines = map(str.strip, fileinput.input())
  17. parties = list(read_parties(lines))
  18. elected_parties = get_elected_parties(lines)
  19. for party in parties:
  20. if party in elected_parties:
  21. print(party)
Success #stdin #stdout 0.04s 9420KB
stdin
PARTIES:
Party one
VOTES: something
Party one
stdout
Party one