How to give points for each indices of list
Posted
by
Eric Jung
on Stack Overflow
See other posts from Stack Overflow
or by Eric Jung
Published on 2012-11-24T15:07:03Z
Indexed on
2012/11/27
5:04 UTC
Read the original article
Hit count: 253
python-3.x
def voting_borda(rank_ballots):
'''(list of list of str) -> tuple of (str, list of int)
The parameter is a list of 4-element lists that represent rank ballots for a single riding.
The Borda Count is determined by assigning points according to ranking. A party gets 3 points for each first-choice ranking, 2 points for each second-choice ranking and 1 point for each third-choice ranking. (No points are awarded for being ranked fourth.) For example, the rank ballot shown above would contribute 3 points to the Liberal count, 2 points to the Green count and 1 point to the CPC count. The party that receives the most points wins the seat.
Return a tuple where the first element is the name of the winning party according to Borda Count and the second element is a four-element list that contains the total number of points for each party. The order of the list elements corresponds to the order of the parties in PARTY_INDICES.'''
#>>> voting_borda([['GREEN','NDP', 'LIBERAL', 'CPC'], ['GREEN','CPC','LIBERAL','NDP'],
['LIBERAL','NDP', 'CPC', 'GREEN']])
#('GREEN',[4, 6, 5, 3])
list_of_party_order = []
for sublist in rank_ballots:
for party in sublist[0]:
if party == 'GREEN':
GREEN_COUNT += 3
elif party == 'NDP':
NDP_COUNT += 3
elif party == 'LIBERAL':
LIBERAL_COUNT += 3
elif party == 'CPC':
CPC_COUNT += 3
for party in sublist[1]:
if party == 'GREEN':
GREEN_COUNT += 2
elif party == 'NDP':
NDP_COUNT += 2
elif party == 'LIBERAL':
LIBERAL_COUNT += 2
elif party == 'CPC':
CPC_COUNT += 2
for party in sublist[2]:
if party == 'GREEN':
GREEN_COUNT += 1
elif party == 'NDP':
NDP_COUNT += 1
elif party == 'LIBERAL':
LIBERAL_COUNT += 1
elif party == 'CPC':
CPC_COUNT += 1
I don't know how I would give points for each indices of the list MORE SIMPLY. Can someone please help me? Without being too complicated. Thank you!
© Stack Overflow or respective owner