r/dailyprogrammer • u/nint22 1 2 • Nov 20 '13
[11/20/13] Challenge #136 [Intermediate] Ranked Voting System
(Intermediate): Ranked Voting System
A Ranked Voting System is a system that chooses a result based on a ranked-preference rather than a simple majority. A standard ranked ballot generally has multiple choices, only one of which one can be picked. A ranked ballot allows you to choose the order in which you prefer candidates. An example could be that you prefer choice B first, then choice C, and finally choice A.
There are some neat implications on how this differs from conventional voting systems, and is used in many different countries and states (check out the same [article's list of current uses](http://en.wikipedia.org/wiki/Ranked_voting_systems#Use_by_polities). CGP Grey has a great explanation on the overall system; well worth a watch! The overall difference between the two system is that a more agreed-upon candidate could win during a heavily split election.
Your goal is to take a list of candidates and voter's ballots, implement this voting system (using the Instant-runoff rules), and print the results of the fictional election.
Formal Inputs & Outputs
Input Description
On standard console input, you will be given two space-delimited integers, N and M. N is the number of votes, while M is the number of candidates. After this line, you will be given the candidates line, which is a space-delimited set of M-number of candidate names. These names are one-word lower-case letters only. This is followed by N-lines of ballots, where each ballot is a list of M-integers, from 0 to M-1, representing the order of preference.
Note that the order of preference for ballots goes from left-to-right. The integers are the index into the candidate list. For the example below, you can map 0: Knuth, 1: Turing, 2: Church. This means that if the ballot row is "1 0 2", that means the voter prefers Turing over Knuth over Church.
Output Description
Given the candidates and ballots, compute the first-round of successful candidates (e.g. rank them based on all ballot's first choice). If the percentage of votes for any one candidate is more than 50%, print the candidate name as the winner. Else, take all the votes of the least-successful candidate, and use their ballot's 2nd choice, summing again the total votes. If needed (e.g. there is no candidate that has more than 50% of the votes), repeat this process for the 3rd, 4th, etc. choice, and print the winner of the election.
For each round of computation, print the percentage of votes for each candidate, and rank them based on that percentage, using the output format.
Sample Inputs & Outputs
Sample Inputs
5 3
Knuth Turing Church
1 0 2
0 1 2
2 1 0
2 1 0
1 2 0Sample Outputs
Round 1: 40.0% Turing, 40.0% Church, 20.0% Knuth
Round 2: 60.0% Turing, 40.0% Church
Turing is the winner
6
u/rectal_smasher_2000 1 1 Nov 20 '13
what if there's a draw?
5
u/skeeto -9 8 Nov 20 '13
Lisp. In the case of an elimination tie, all are eliminated. Since the sample input is broken, leaving it unspecified: I consider 0 to be the highest preference. No parsing was done.
A vote is a list of preferences and an election is a list of votes. First define some helper functions.
This function removes a set of candidates from the election:
When no one gets above 50%, eliminate the bottom candidates and recurse on the new election.
Usage (modified the sample input to fix it):
3
u/chunes 1 2 Nov 20 '13 edited Nov 20 '13
EDIT: I'm wrong. Don't listen to me.
In case anyone is confused by the order of preference like I was, 2 (or M-1) is thestrongestpreference and 0 is theweakest. This can be seen because otherwise there would be a winner in the first round given the sample input.12
u/Edward_H Nov 20 '13 edited Nov 21 '13
Here's my solution in COBOL-74 (or something near it), the last COBOL standard to require unstructured programming.
EDIT: Fixed bug when getting votes.
5
u/chunes 1 2 Nov 21 '13
Proud of my first intermediate solution! Java:
3
u/dante9999 Nov 20 '13
Nice that daily programmer is back and appears regularly again, here's my soution in Python.
Output:
3
u/nadanone Nov 20 '13 edited Nov 21 '13
Python. I have only been programming for a couple months, so any feedback is much appreciated.
For my solution, I don't provide the program N or M (and honestly don't see why that's necessary: if the program is using those vals rather than generating them itself where needed, isn't that just cause for potential problems?). Also if there are tied lowest scoring candidates, both are removed.
3
u/ooesili Nov 21 '13
Haskell solution, lots of comments. It doesn't handle invalid input or ties very well, but I got tired.
3
u/[deleted] Nov 21 '13 edited Nov 21 '13
shitty python 3...
3
u/[deleted] Nov 21 '13
C# -- along with a shoehorn and some duct tape.
3
u/h3ckf1r3 Nov 22 '13
Wrote this in ruby, I actually feel that I wrote some decently clean code, usually for these competitions I write absolutely terrifying code, but this time I rewrote some of it to make it a little more decent.
As always, criticism and advice are welcome :)
3
u/lukz 2 0 Nov 22 '13
BASIC
(BASIC interpreter 1Z-016 for an 8-bit computer.) P. is an abbreviation for PRINT
Example session
3
u/bheinks 0 0 Nov 24 '13
Some ugly Vala (a reminder of how spoiled I am on scripting languages)
3
u/Apex42 Nov 25 '13 edited Nov 25 '13
Here is my solution in C (first intermediate!). A bit rough, so tips on how to polish up the code would be appreciated, as I only started C (and programming in general) a few months ago.
Sample input:
Sample output:
3
u/VerifiedMyEmail Dec 13 '13 edited Dec 15 '13
javascript with html that way the input constrictions will be obeyed
try it on codepen
feedback please
2
u/letalhell Nov 20 '13
The sample input is wrong. The last person voting choose " 1 2 1" Which is Turing > Church > Turing . It should be 1 2 0 or 0 2 1.
2
u/toodim Nov 20 '13
I found this to be quite a relevant problem because I leave in Minneapolis where we recently had a 35 candidate ranked choice mayoral election that took several days to count. In our system you only rank the top 3 candidates though, instead of assigning ranks to all of them.
Python (did not order candidates by rank each round)
I also changed up the input a bit to use candidates from our election:
2
u/OffPiste18 Nov 20 '13
Scala!
2
u/minikomi Nov 21 '13 edited Nov 21 '13
Good one! In racket:
2
u/ooesili Nov 21 '13 edited Nov 22 '13
Here are two tools for testing your programs. The first is just a simple tie case:
The other is a little perl script for generating random inputs. You can optionally supply a file with a list of names, for really big tests, like one from here:
2
u/r_s Nov 22 '13
C++ (C11)
Not really happy with my solution, will just post a link to not clutter this up with a poorer solution.
https://gist.github.com/r-s-/7593224
2
u/vape Nov 22 '13
My solution in Python 3. I'm cheating a little. At the end of the round, if more than 1 candidate is tied at the bottom (i.e. Knuth: 2, Turing: 2, Church: 3) I remove only one of the losers from the election instead of removing both of them.
2
u/Hoten 0 1 Nov 22 '13
Scala solution
2
u/eslag90 Nov 24 '13 edited Nov 24 '13
First submission! Python 2.7.
2
u/ixid 0 0 Nov 24 '13 edited Nov 24 '13
An attempt in D. It should be able to deal with people who expressed varied numbers of preferences.
2
u/[deleted] Nov 25 '13
Here's my (late) Python solution. I read the input from a file rather than the console, but aside from that the problem specs should be satisfied. The solution works properly in the edge case of a complete tie.
2
u/stuartnelson3 Nov 25 '13 edited Nov 25 '13
starting to learn go
and the output:
2
u/Sandor_at_the_Zoo Nov 26 '13 edited Nov 26 '13
Mathematica. I'm still not sure what idiomatic mathematica code looks like, so this may not be the best way to do it.
2
u/jagt Nov 28 '13 edited Nov 28 '13
Groovy 2.2 solution. I've been reading about Groovy recently and it looks like my new favorite scripting language. But after this I find it's really full of gotchas :( Just look how awkward the closure tail recursion is written...
2
u/agambrahma Dec 19 '13
C++ solution
Output: $ ./a.out < sample Will read in 5 votes for 3 candidates. Read in the following candidates :- Knuth Turing Church Read in the following votes :- 1 0 2 0 1 2 2 1 0 2 1 0 1 2 0
Round 1: 20.00% Knuth, 40.00% Turing, 40.00% Church
Round 2: 60.00% Turing, 40.00% Church
Turing is the winner.
2
u/[deleted] Dec 20 '13
Assuming the input will come from a multi line string this is my Java solution:
First method extracts info from input:
And feeds the input in the second method where it is processed:
2
u/[deleted] Mar 03 '14 edited Mar 03 '14
Hi /r/dailyprogrammer, this is my first post, would be EXTREMELY appreciative of a critique of what I've come up with. Just fyi, I'm a senior in high school, majoring in comp sci next year, and I haven't done a ton of coding in the past year or two since I had taken every programming class at my high school by my sophomore year. In short, I'm probably a bit rusty and am no way near as experienced as all of you are I'm sure. I hope someone sees this, this question was posted awhile ago though :/ Anyhow, here's a solution in Java, the only language I'm actually familiar with. There's definitely more compact versions than mine. Also, I didn't bother covering for draws. By the way, I'm pretty sure each voter's tertiary choice is not necessary under any circumstances, hence the corresponding instance variable and array (candCountThird) are not necessary at all.
1
u/letalhell Nov 20 '13
Can anyone give some insight on how to accomplish that?
If I use fixed variables from the begining is pretty easy, but when I'm using the input to create variables to hold the votes it get all messy.
So far I got something like this.
What I'm struggling with is... I want to break the votes input into timed variables(inside a loop with canditates range) (i'm think of using pop() to take the item of top of the vote/list/input), but I can't manage to add the value to it's proper candidate in the dict because they are in random order, and I can't make a loop with == Number because the initial number of canditates is decided via-input and is not a fixed number.
Any hint?
1
u/Atumm Mar 03 '14 edited May 06 '14
My first posted solution. The language is Python. Constructive feedback is really appreciated. It doesn't handle all off corner cases. Because of the 80/20 rule.