Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

starter boilerplate #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 1 addition & 76 deletions program.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,78 +48,7 @@ def create_votes():
#################### FUNCTIONS ###################
##################################################

############### CREATE ARRAY #####################

def results_hash(votes):
""" Count votes per candidate and per mention

Returns a dict of candidate names containing vote arrays.
"""
candidates_results = {
candidate: [0]*len(MENTIONS)
for candidate in CANDIDATES.keys()
}
for vote in votes:
for candidate, mention in vote.items():
candidates_results[candidate][mention] += 1
return candidates_results

############### CALCULATE MEDIAN ##################

def majoritary_mentions_hash(candidates_results):
r = {}
for candidate, candidate_result in candidates_results.items():
cumulated_votes = 0
for mention, vote_count in enumerate(candidate_result):
cumulated_votes += vote_count
if MEDIAN < cumulated_votes:
r[candidate] = {
"mention": mention,
"score": cumulated_votes
}
break
return r

############### SORT CANDIDATES #####################

def sort_candidates_by(mentions):
## bubble sort here we go!
unsorted = [(key, (mention["mention"], mention["score"])) for key, mention in mentions.items()]
swapped = True
while swapped:
swapped = False
for j in range(0, len(unsorted) - 1):
## but we need REVERSE bubble sort ;-)
# (note that here we compare tuples, which is pretty neat)
if unsorted[j + 1][1] > unsorted[j][1]:
unsorted[j+1], unsorted[j] = unsorted[j], unsorted[j+1]
swapped = True

return [
{
"name": candidate[0],
"mention": candidate[1][0],
"score": candidate[1][1],
}
for candidate in unsorted
]

############### FORMAT RESULTS #####################

def print_results(results):
for i, result in enumerate(results):
name = CANDIDATES[result["name"]]
mention = MENTIONS[result["mention"]]
score = result["score"] * 100. / VOTES
if i == 0:
print("Gagnant: {} avec {:.2f}% de mentions {} ou inférieures".format(
name, score, mention
))
continue
else:
print("- {} avec {:.2f}% de mentions {} ou inférieures".format(
name, score, mention
))
# Your code goes here


##################################################
Expand All @@ -128,10 +57,6 @@ def print_results(results):

def main():
votes = create_votes()
results = results_hash(votes)
majoritary_mentions = majoritary_mentions_hash(results)
sorted_candidates = sort_candidates_by(majoritary_mentions)
print_results(sorted_candidates)

if __name__ == '__main__':
main()