# How Voting Systems Work 6, Exercise 2: burying under the Borda count # Assumed, invented profile: 9 voters, three candidates. Find one honest voter who can get a # better winner by reordering their own ballot, and show the lie and the new points. from itertools import permutations def borda_points(ballots): k = len(ballots[0]) points = {c: 0 for c in sorted(ballots[0])} for b in ballots: for place, c in enumerate(b): points[c] += k - 1 - place return points def winner(points): return max(points, key=points.get) groups = [ (4, ("Ana", "Bo", "Cy")), (3, ("Bo", "Ana", "Cy")), (2, ("Cy", "Bo", "Ana")), ] ballots = [r for n, r in groups for _ in range(n)] honest = borda_points(ballots) print("honest points:", honest, "->", winner(honest)) found = False for i, truth in enumerate(ballots): for lie in permutations(truth): trial = ballots[:i] + [lie] + ballots[i + 1:] pts = borda_points(trial) if truth.index(winner(pts)) < truth.index(winner(honest)): print(f"voter {i} (true order {truth}) lies {lie}") print("points after the lie:", pts, "->", winner(pts)) found = True break if found: break if not found: print("no single voter can gain") # Output when run: # honest points: {'Ana': 11, 'Bo': 12, 'Cy': 4} -> Bo # voter 0 (true order ('Ana', 'Bo', 'Cy')) lies ('Ana', 'Cy', 'Bo') # points after the lie: {'Ana': 11, 'Bo': 11, 'Cy': 5} -> Ana