# arfi.chazyl[at]gmail.com # Raw Votes # The user with no Bonus at all give 25 or 75 points to a participation , of course he will automatically give positive vote to himself on his own participation submission # # Bonus Altered Votes # # Score calculation import math def raw_results_calculate(candidates): candidates_results = {} for candidate in candidates: nbVotes = 0 totalScore = 0 for vote in candidates[candidate]: nbVotes += 1 totalScore += vote candidates_results[candidate] = {"votes": nbVotes, "score": totalScore} return candidates_results def bonus_results_calculate(raw_results,profile_bonus): candidates_results = {} score = 0 for candidate in raw_results: candidate_bonus = 0 for bonus in profile_bonus[candidate]: candidate_bonus += bonus score = (raw_results[candidate]["votes"]-1)*50 # we multiply the number of votes by 50 to make contributions with more votes have more chances to win score += raw_results[candidate]["score"] # we add the raw score of the candidate score += (score * candidate_bonus ) / 100 # we add the bonus of the user to make regular and trusted users have more chances to win candidates_results[candidate] = score return candidates_results def displayable_results_calculate(displayable_results): candidates_results = {} score = 0 for candidate in displayable_results: score = displayable_results[candidate] score = math.log(score,1000)# we apply a log base 1000 to the score score = score * 1000 # we multiply by 1000 to make the score prettier score = round(score) candidates_results[candidate] = score return candidates_results candidates = { 'Ahmed': [75, 25, 75, 25, 75, 75, 75],#This user received votes only from user without bonus 'Ali': [75,13, 16, 22, 75, 25, 75, 75, 36, 90, 100], 'Amel': [75,25, 78, 82, 22, 13, 10, 81], 'Ahlem': [75,13, 6, 78, 90, 80, 79, 82, 12, 14, 84, 78], 'Sawsen': [75,22, 25, 75, 88, 90, 19, 5], 'Jamil': [75,13, 12, 5, 25, 25, 75, 25, 80, 75, 75, 25, 25], 'guest': [75] # no one voted for guest except him } profile_bonus = { 'Ahmed': [12,10],#the profile bonus can be at max 12.5 + 12.5 = 25 and give 25% advantage at max when voting and when participating 'Ali': [5,7], 'Amel': [10,2], 'Ahlem': [5,6], 'Sawsen': [0,0], 'Jamil': [0,0], 'guest': [0,0] } raw_results = raw_results_calculate(candidates) bonus_results = bonus_results_calculate(raw_results,profile_bonus) displayable_results = displayable_results_calculate(bonus_results) print("Raw scores") print(raw_results) print("") print("") print("Bonus altered scores") print(bonus_results) print("") print("") print("Prettier displayable results Logarithm based and multiplied") print(displayable_results) print("") print("") print("Winners are") print(sorted(bonus_results))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question