# How Voting Systems Work 5, Exercise 2: the same score ballots under different approval cut-offs # Eight voters score three candidates from 0 to 10 (assumed, invented numbers). # For every cut-off from 1 to 10, approve any candidate scored at or above it and print the winner(s). # Then print the score-voting winner by total, and the cut-offs at which approval disagrees with it. scores = [ {"Ana": 9, "Bo": 5, "Cy": 1}, {"Ana": 8, "Bo": 6, "Cy": 2}, {"Ana": 9, "Bo": 4, "Cy": 0}, {"Ana": 7, "Bo": 5, "Cy": 3}, {"Ana": 1, "Bo": 4, "Cy": 10}, {"Ana": 0, "Bo": 3, "Cy": 9}, {"Ana": 2, "Bo": 5, "Cy": 8}, {"Ana": 1, "Bo": 6, "Cy": 9}, ] names = ["Ana", "Bo", "Cy"] totals = {c: sum(s[c] for s in scores) for c in names} score_winner = max(totals, key=totals.get) print("score totals:", totals, "->", score_winner) disagree = [] for cutoff in range(1, 11): approvals = {c: sum(1 for s in scores if s[c] >= cutoff) for c in names} best = max(approvals.values()) winners = [c for c in names if approvals[c] == best] print(f"cut-off {cutoff:>2}: {approvals} -> {' and '.join(winners)}") if winners != [score_winner]: disagree.append(cutoff) print("cut-offs where approval does not give the score winner alone:", disagree) # Output when run: # score totals: {'Ana': 37, 'Bo': 38, 'Cy': 42} -> Cy # cut-off 1: {'Ana': 7, 'Bo': 8, 'Cy': 7} -> Bo # cut-off 2: {'Ana': 5, 'Bo': 8, 'Cy': 6} -> Bo # cut-off 3: {'Ana': 4, 'Bo': 8, 'Cy': 5} -> Bo # cut-off 4: {'Ana': 4, 'Bo': 7, 'Cy': 4} -> Bo # cut-off 5: {'Ana': 4, 'Bo': 5, 'Cy': 4} -> Bo # cut-off 6: {'Ana': 4, 'Bo': 2, 'Cy': 4} -> Ana and Cy # cut-off 7: {'Ana': 4, 'Bo': 0, 'Cy': 4} -> Ana and Cy # cut-off 8: {'Ana': 3, 'Bo': 0, 'Cy': 4} -> Cy # cut-off 9: {'Ana': 2, 'Bo': 0, 'Cy': 3} -> Cy # cut-off 10: {'Ana': 0, 'Bo': 0, 'Cy': 1} -> Cy # cut-offs where approval does not give the score winner alone: [1, 2, 3, 4, 5, 6, 7]