6
\$\begingroup\$

In a preference ballot there are many ways to decide who won. One of which is the Borda count method, where we give everyone point based on their places on the ballot.

For example, in these results:

  10  5  7  2
_____________
1  a  d  a  c
2  b  a  d  b
3  c  c  b  a
4  d  b  c  d

The results are

1: a : 87
2: d : 60
3: b : 55
4: c : 45

Because

4*10 is 40
5*3 is 15
7*4 is 28
2*2 is 4
40 + 15 + 28 + 4 is 87

Or is words: We give first place 4 points , second 3, third 2, etc.

So a is in first place 17 times, so they get 68 points then they get 15 points for being in second 5 times and 4 points for being in third 2 times for a total of 87 points.

Because this process is a pain, I need you write a program that finds the results for me, but since I have limited space on my PC, your program will have to be as small as possible.

Your Challenge

Write a program or function the takes an input in this format

Number of Ballots : placings, Number of Ballots : placings, Etc...

and will output the results in this format:

1: a: 90
2: b: 80
Etc...

Rules:

This is Code Golf, so the shortest answer that does the task wins

Standard Loopholes Apply

Example:

Input:
3: A B C, 2: C B A, 1: C A B

Output:
1: A: 13
2: C: 12
3: B: 11

This is my first challenge, so please give me all the criticism you so desire, I won't take it offensively.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Could you explain the table format? It's not obvious where e.g. 4*10 comes from. \$\endgroup\$ Nov 5, 2015 at 22:40
  • 1
    \$\begingroup\$ Are the candidates always A, B, C...? How do we handle ties? \$\endgroup\$
    – xnor
    Nov 5, 2015 at 22:57
  • \$\begingroup\$ The candidate will always be single letters, but they will not always be A B and C. As for ties just assume there will not be any, just to keep thing simple. \$\endgroup\$ Nov 5, 2015 at 23:21

3 Answers 3

2
\$\begingroup\$

Python 2, 248 bytes

x={}
for a,b in[y.split(':')for y in raw_input().split(', ')]:
 for d,e in[(c,(len(b)/2-i)*int(a)) for i,c in enumerate(b[1::2])]:x[d]=x.setdefault(d,0)+e
for i,(f,g) in enumerate(sorted(x.items(),key=lambda t:-t[1]),1):print '%d: %s : %d'%(i,f,g)

Reads the from the standard input (for example, echo '3: A B C, 2: C B A, 1: C A B' | python borda.py).

\$\endgroup\$
1
\$\begingroup\$

CJam, 78 bytes

q", "%{S%}%_0=,1-:L;{(1<i*}%e_W%L/L{)}%:C;{C.*}%e_$e`$W%C.+{W%{": "}%e_W(<N+}%

Try it Online.

Holy crap! How did I do this?

When I saw that CJam had a run length encode/decode function, I wondered why someone would use it. Today I found out. Essentially, what this program does is create a string containing each letter once for each vote. Then it sorts the string and uses run length encode to find out how many votes each letter got, and then formats the data.

\$\endgroup\$
1
  • \$\begingroup\$ Well, I guess you won, Congratulations! \$\endgroup\$ Nov 25, 2015 at 1:56
1
\$\begingroup\$

Perl 5, 131 bytes

for(split/, /,$ARGV[0]){($n,@v)=split/:? /;$s{$v[@v-$_]}+=$n*$_ for 1..@v}$,=": ";map{say++$i,"$_ ",$s{$_}}sort{$s{$b}<=>$s{$a}}@v

Use as perl -E'the string above' 'the input string'

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.