Skip to content

Instantly share code, notes, and snippets.

@robla
Created February 16, 2019 00:54
Show Gist options
  • Save robla/f1061e093d30306b6b31972c9c71d382 to your computer and use it in GitHub Desktop.
Save robla/f1061e093d30306b6b31972c9c71d382 to your computer and use it in GitHub Desktop.
Reverse-engineering a ballot set from a specific table of pairwise voting results listed in a 2019 reddit post
#!/usr/bin/python3
# Script for generating an answer to this:
# <https://www.reddit.com/r/Voting/comments/aqsxft/condorcet_winner_from_counts_of_ranks_possible/>
import json
import itertools
from collections import Counter
innerperm = [x for x in itertools.permutations(['P1', 'P2', 'P3'], 3)]
ballotsets = set()
for result in itertools.product(innerperm, repeat=10):
topcount = Counter([x[0] for x in result])
if(topcount['P1'] == 3 and topcount['P2'] == 2 and topcount['P3'] == 5):
middlecount = Counter([x[1] for x in result])
if(middlecount['P1'] == 2 and middlecount['P2'] == 6 and middlecount['P3'] == 2):
bottomcount = Counter([x[2] for x in result])
if(bottomcount['P1'] == 5 and bottomcount['P2'] == 2 and bottomcount['P3'] == 3):
ballotcount = Counter(['>'.join(ballot) for ballot in result])
ballotset = '\n'.join(
["{}:{}".format(ballotcount[b], b) for b in sorted(ballotcount.keys())])
ballotsets.add(ballotset)
for i, s in enumerate(ballotsets):
print("Ballot set #{}:".format(i + 1))
print(s)
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment