Skip to content
Extraits de code Groupes Projets
Valider 76663bba rédigé par Anthony Barbier's avatar Anthony Barbier
Parcourir les fichiers

Clean up code, and add comments

parent 1731a9c3
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
#!/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()
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter