fork download
  1. from itertools import product, starmap
  2. from operator import ne
  3.  
  4. def fraction_percentage(n, d):
  5. return '{}/{} ({:f}%)'.format(n, d, n * 100 / d)
  6.  
  7. for n in range(2, 8):
  8. ties_all = 0
  9. ties_noself = 0
  10. for votes in product(range(n), repeat=n):
  11. vote_counts = [0] * n
  12. for x in votes:
  13. vote_counts[x] += 1
  14. if vote_counts.count(max(vote_counts)) > 1:
  15. ties_all += 1
  16. ties_noself += all(starmap(ne, enumerate(votes)))
  17. print('With {}: {} ties with no self-votes, {} ties allowing self-votes'
  18. .format(n, fraction_percentage(ties_noself, (n-1)**n),
  19. fraction_percentage(ties_all, n**n)))
Success #stdin #stdout 1.55s 9328KB
stdin
Standard input is empty
stdout
With 2: 1/1 (100.000000%) ties with no self-votes, 2/4 (50.000000%) ties allowing self-votes
With 3: 2/8 (25.000000%) ties with no self-votes, 6/27 (22.222222%) ties allowing self-votes
With 4: 21/81 (25.925926%) ties with no self-votes, 60/256 (23.437500%) ties allowing self-votes
With 5: 344/1024 (33.593750%) ties with no self-votes, 1020/3125 (32.640000%) ties allowing self-votes
With 6: 6535/15625 (41.824000%) ties with no self-votes, 19020/46656 (40.766461%) ties allowing self-votes
With 7: 129534/279936 (46.272719%) ties with no self-votes, 372540/823543 (45.236254%) ties allowing self-votes