ranking_ballot = ('GREEN','LIBERAL','CPC','NDP',) ranking_ballot2 = ('CPC','NDP','LIBERAL','GREEN',) ranking_ballot3 = ('NDP','CPC','LIBERAL','GREEN',) ranking_ballot4 = ('NDP','CPC','LIBERAL','GREEN',) rankings = {ranking_ballot4 : 2,ranking_ballot2 : 1, ranking_ballot : 1} print rankings[ranking_ballot4] NAME_TO_INDEX = {'NDP': 0,'GREEN': 1,'LIBERAL': 2,'CPC': 3} INDEX_TO_NAME = {0: 'NDP',1: 'GREEN',2: 'LIBERAL',3: 'CPC'} def voting_borda(dict_list_str): print dict_list_str rank = [0,0,0,0] sum_of_votes = 2 winner_name = INDEX_TO_NAME[0] while(rank[NAME_TO_INDEX[winner_name]] < sum_of_votes/2): for x in xrange(0,len(rank)): rank[x] = 0 for x in dict_list_str : current_list = x indice = 0 for y in current_list : rank[NAME_TO_INDEX[y]] += (3 - indice)*dict_list_str[current_list] indice+=1 max = 0 ply = 0 while rank[ply] == 0 : ply+=1 min = rank[ply] indice = 0 winner_name = '' loser_name = '' sum_of_votes = 0 print rank for key in rank: if key != 0 : print (INDEX_TO_NAME[indice], 'has a score of', key) if(key > max): max = key winner_name = INDEX_TO_NAME[indice] if(key < min): min = key loser_name=INDEX_TO_NAME[indice] sum_of_votes += key indice+=1 print loser_name current_key = '' current_value = 0 tempdict = {} if loser_name != '' : for x in dict_list_str: current_key = list(x) current_value = dict_list_str[x] current_key.remove(loser_name) tempdict.update({tuple(current_key):current_value}) dict_list_str = tempdict print rank print(dict_list_str) print(rank) return(winner_name,'None') print voting_borda(rankings)
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