0

Possible Duplicate:
Changing the output a bit

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)
return winner, results

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?

Community
  • 1
  • 1
user1864828
  • 349
  • 1
  • 3
  • 10

1 Answers1

0
PARTY_INDICES = ['NDP_INDEX', 'GREEN_INDEX', 'LIBERAL_INDEX', 'CPC_INDEX']
party_dic = {}

for i, item in enumerate(PARTY_INDICES):
    party_dic[item.split('_')[0]] = i

print party_dic

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 = [v for k, v in sorted(results.items(), key = lambda x: party_dic[x[0]])]
    return winner, results


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


{'NDP': 0, 'CPC': 3, 'GREEN': 1, 'LIBERAL': 2}
('GREEN', [4, 6, 5, 3])
Jun HU
  • 3,176
  • 6
  • 18
  • 21
  • but party_indice is a constant. PARTY_INDICES = [NDP_INDEX, GREEN_INDEX, LIBERAL_INDEX, CPC_INDEX]. i cant change it :((((((( – user1864828 Nov 30 '12 at 05:43
  • ahh? What do you mean? We can't make it dict? – Jun HU Nov 30 '12 at 05:44
  • i mean we can.. but is there a way to solve this without making it? and still have the output in order of ndp,green,liberal,cpc? – user1864828 Nov 30 '12 at 05:46
  • i must know the index of the parties by some way. are you sure the indices = [strs] or indices = [obj] – Jun HU Nov 30 '12 at 05:51
  • # A dict where each key is a party name and each value is that party's index. NAME_TO_INDEX = { 'NDP': NDP_INDEX, 'GREEN': GREEN_INDEX, 'LIBERAL': LIBERAL_INDEX, 'CPC': CPC_INDEX } – user1864828 Nov 30 '12 at 05:55
  • that is to say, you have a index list in advance? – Jun HU Nov 30 '12 at 05:56
  • yupppp yes i will edit it sec. – user1864828 Nov 30 '12 at 06:00
  • Oh, i'm sorry, i have no skype account... you can make another question in stackoverflow. ps: i'm very sorry that my english is so poor that i can't comunicate with you smoothly~~-_- – Jun HU Nov 30 '12 at 06:08