this repo has no description
at main 1.7 kB view raw
1#!/usr/bin/env nix-shell 2#!nix-shell -i python3 -p python3 3 4import collections 5from typing import Dict 6 7ballots: list[list[int]] = [] 8total_ballots = 0 9candidates: list[str] = [] 10votes: list[list[int]] = [] 11votes_can: list[list[str | int]] = [] 12can_dict: Dict[str, int] = {} 13 14for i in range(0, 24): 15 votes.append([]) 16 votes_can.append([]) 17 18# Expand candidates to list 19with open("candidates.txt", "r") as c: 20 for l in c.readlines(): 21 can = l.rstrip("\n") 22 candidates.append(can) 23 can_dict[can] = 0 24 25with open("ballots.txt", "r") as b: 26 # Each line is its own ballot 27 for bal in b.readlines(): 28 # Get a list of all votes for that ballot, zero-indexed for easier math 29 choices = [int(c)-1 for c in bal.split(" ")] 30 ballots.append(choices) 31 # Output each choice into a 1st place, 2nd place, etc array 32 for i, c in enumerate(choices): 33 votes[i].append(c) 34 35with open("ballots.txt", "r") as b: 36 total_ballots = len(b.readlines()) 37 38for v_idx, v in enumerate(votes): 39 v_tmp = v 40 for can_idx, can in enumerate(candidates): 41 v_tmp = [can if x == can_idx else x for x in v_tmp] 42 votes_can[v_idx] = v_tmp 43 44for idx, v in enumerate(votes_can): 45 cnt = dict(collections.Counter(v)) 46 print(f"Place {idx+1} votes: {cnt}\n\n") 47 # Add vote weights 48 for can, total in cnt.items(): 49 can_dict[can]+=(total*(idx+1)) 50 51for can, weight in can_dict.items(): 52 wght_avg = weight/total_ballots 53 can_dict[can] = wght_avg 54 if len(can) < 8: 55 padding = "\t" 56 else: 57 padding = "" 58 print(f"{can}\t{padding} Average Placement: \t{str(wght_avg)[:5]}") 59 60 61