0

Question is:

voting_borda:

(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.

This is my code:

def voting_borda(*args):
results = {}
for sublist in args:
    for i in range(0, 3):
        if sublist[i] in results:
            results[sublist[i]] += 3-i
        else:
            results[sublist[i]] = 3-i

winner = max(results, key=results.get)
results_sort = sorted(results,key=lambda x:x[1],reverse=True)
return winner, results_sort

However, if i try

voting_borda(['GREEN','NDP', 'LIBERAL', 'CPC'],['GREEN','CPC','LIBERAL','NDP'],['LIBERAL','NDP', 'CPC', 'GREEN'])

I get,

('GREEN', {'NDP': 4, 'CPC': 3, 'GREEN': 6, 'LIBERAL': 5})

But, I want the first parameter to be the winner(that part is fine), and the second parameter to be just the values and also to be in the order of PARTY_INDICES which is PARTY_INDICES = [NDP_INDEX, GREEN_INDEX, LIBERAL_INDEX, CPC_INDEX].

any solutions or ways that i could make this better?

user1864828
  • 349
  • 1
  • 3
  • 10
  • Either this is homework or [this guy](http://stackoverflow.com/questions/13546205/how-to-shorten-this-code-without-using-bunch-of-if-statements/) beat you to it. – engineerC Nov 30 '12 at 04:04
  • Lots of you in the same class. http://stackoverflow.com/questions/13505169/how-to-append-the-number-of-item-frequencies-in-a-list-in-python-3-2/13505225#13505225 – sberry Nov 30 '12 at 04:05
  • ive done that one already! but this question is differnet from that one – user1864828 Nov 30 '12 at 04:06
  • Haha, here too: http://stackoverflow.com/questions/13637068/been-stuck-on-this-for-3-hours-voting-approval#comment18707069_13637068 – sberry Nov 30 '12 at 04:06
  • yeah.. our profs arent that helpful this year. – user1864828 Nov 30 '12 at 04:07
  • I think he's gonna know something's up when you all show up with the same answer but none of you understand it. PS [this one too](http://stackoverflow.com/questions/13542458/how-to-give-points-for-each-indices-of-list) – engineerC Nov 30 '12 at 04:08
  • Could you help me or give me a hint into the right direction? – user1864828 Nov 30 '12 at 04:08

1 Answers1

1

The index thing (NDP_INDEX etc.) is really unnecessary and un-pythonic. Just use the string, and if you need to sort it according to something, use lists of tuples.

 vv = [ ('republican',3), ('democrat',9), ('libertarian',73), ('green',-2) ]
 vsort = sorted(vv,key=lambda x:x[1],reverse=True)
 print(list(party for party, value in vsort))

You can do something similar to get your answer. Hate to do the entire assignment for you...

engineerC
  • 2,808
  • 17
  • 31
  • could you explain what key=lambda is, we havent learned that yet! – user1864828 Nov 30 '12 at 04:23
  • A lambda function is an unnamed inline function. `lambda x: x[1]` is equivalent to `def f(x): return x[1]`. It's telling sorted to sort the functions according to the second element of each tuple. (psst read the page I linked) – engineerC Nov 30 '12 at 04:24
  • is there a simpler way? i am a real beginner, we havent learned reverse=True that either :*(.. this is frustrating – user1864828 Nov 30 '12 at 04:27
  • Well, it talked about that in the page I linked you (nudge nudge wink wink). reverse is a [keyword argument](http://docs.python.org/2/tutorial/controlflow.html#keyword-arguments) that controls whether the sort is ascending or descending. And lambda is [here](http://docs.python.org/2/tutorial/controlflow.html#lambda-forms). It's gonna be tough at first, especially if this is your first language, but hang in there. Hey, at least you won't have any bad habits from C. :) – engineerC Nov 30 '12 at 04:33
  • Yes, that is true. You could accomplish the same thing using the `reverse` or `reversed` function after the fact, but it's more efficient to go ahead and let the `sorted` function do its sort that way in the first place. – engineerC Nov 30 '12 at 04:42
  • now i full understand what u are saying. however, i have edited the code, but now the output is , ('GREEN', ['GREEN', 'CPC', 'LIBERAL', 'NDP']). how do i make it so that it gives the values? :( – user1864828 Nov 30 '12 at 04:47
  • If you're using the `list(party for party, value in vsort)`, use value for party instead of party for party. Read it like a sentence- - "Make a list of the parties, for each party and value in my sorted array". – engineerC Nov 30 '12 at 05:12
  • wait i have lost u again. i have edited my code. if i do that i get ('GREEN', ['GREEN', 'CPC', 'LIBERAL', 'NDP']). but i want the values – user1864828 Nov 30 '12 at 05:18