diff --git a/fill.py b/fill.py index b2b56284fad4cb2d999a494558b5d2266409b593..308d1d85391dbd42e1f855f6201a1c98c5efdbb9 100755 --- a/fill.py +++ b/fill.py @@ -1,24 +1,39 @@ #!/bin/env python3 -listes = ["", "che", "com", "fii", "hur", "iik", "ani", "lab", "ove"] -nbListes = len(listes)-1 +""" +This script transforms votes in number form into votes in mathematical form. +For example, given five candidates A, B, C, D, E, and two ballots 12345 and 11222, our output should be "A > B > C > D > E" for the first ballot, and "A = B > C = D = E" for the second ballot. +Input votes should be stored in number form in the file `results_bruts.txt`, one vote per line. +Votes in mathematical form will be written to the file `results_out.txt`, one vote per line. +""" -with open('results.txt','r') as data: - o = open('results_out.txt','w') +candidates = ["che", "com", "fii", "hur", "iik", "ani", "lab", "ove"] +nbCandidates = len(candidates) +# For example, with 8 candidates, the max rank a candidate can be given is 9 (no rank given => below every other rank). +maxRankPossible = nbCandidates + 1 + +with open("results_bruts.txt", "r") as data: + o = open("results_out.txt", "w") + # * Read raw data. One line = one vote. Each line is a string with exactly nbCandidates = 8 numbers, each number is in [1, maxRankPossible]. for line in data.readlines(): - maxColumn = int(max(line)) - for column in range(1,10): + # Store the highest rank for this vote, so that we know when we need to stop putting ">" at the end. + maxRank = int(max(line)) + # * For each rank possible, we are going to check if one or several candidates has been given that rank. + for rank in range(1, maxRankPossible + 1): first = True found = False - for ind in range(0,nbListes): - if int(line[ind]) != column: + # Check all candidates. + for ind in range(0, nbCandidates): + # If the rank given to that candidate isn't equal to the rank that we are currently checking, do nothing. + if int(line[ind]) != rank: continue + # If another candidate has already been given that rank if not first: o.write(" = ") - o.write(listes[ind+1]) + o.write(candidates[ind]) first = False found = True - if found and column < maxColumn: + if found and rank < maxRank: o.write(" > ") o.write("\n") o.close()