I am trying to write a function that takes a parameter that is a list of 4-element lists that represent approval ballots for a single riding; the order of the inner list elements corresponds to the order of the parties in a list of parties called PARTY_INDICES
.
The party with the most number of 'yes' votes wins.
It should return a 2-tuple where the first element is the name of the winning party and the second element is a four-element list that contains the number of yes votes for each party. The order of the list elements corresponds to the order of the parties in PARTY_INDICES
.
My code is:
def voting_approval(approval):
parties = ['NDP','GREEN','LIBERAL','CPC']
values = [0,0,0,0]
for decision in approval:
for no, item in enumerate(decision):
if item == 'Yes':
values[no] += 1
total = [(values) for x in zip(approval)]
return (parties[values.index(max(values))], total)
If I try:
voting_approval([['Yes', 'No', 'Yes', 'No'],['No', 'No', 'Yes', 'No']])
it spits out:
('LIBERAL', [[1, 0, 2, 0], [1, 0, 2, 0]])
but I want the result to be:
('LIBERAL', [1, 0, 2, 0])