Important: The deadline for this event has passed. Solutions are available in VBScript, Windows PowerShell, and Perl. |
In Instant (Runoff) Winner, competitors must write a script that determines the winner of an “instant runoff” election.
Not a native speaker of English? These event instructions are also available in the following languages: Chinese (Simplified); Chinese (Traditional); French; German; Japanese; Portuguese Brazilian; Russian; and Spanish. For more information, and to access these localized instructions, see the Scripting Games International page.
| About This Event | |
| Event Scenario |
Division | Advanced |
Deadline | Friday, February 22, 2008 (8:00 AM PST) |
Points Awarded | 20 |
To tell you the truth, explaining Event 3 is probably harder than successfully completing the event. But here goes.
One of the latest trends in voting and elections is the “instant runoff” election. This event requires you to determine the winner in just such an election.
Of course, when it comes to instant runoff elections there are all sorts of variations; our version of the instant runoff election works like this. Four candidates are running for office:
| • | Ken Myer |
| • | Jonathan Haas |
| • | Pilar Ackerman |
| • | Syed Abbas |
In a typical election, voters vote for just one of these candidates. In our instant runoff election, however, voters vote for all four candidates, in order of preference. For example, suppose a user believes Jonathan Haas is best-suited for the job, followed – in order – by Pilar Ackerman, Syed Abbas, and Ken Myer. In that case, the voter’s ballot would look like this:
1. | Jonathan Haas |
2. | Pilar Ackerman |
3. | Syed Abbas |
4. | Ken Myer |
That part’s easy; now it gets a little tricky. When it comes time to count the ballots, votes are tallied only for the voter’s top choice; that means that, on this ballot, Jonathan Haas gets one vote. After the votes have been tallied, you then check to see if any of the candidates received more than 50% of the vote. Let’s say that Jonathan Haas received 53% of the vote. In that case, Jonathan Haas is the winner, and your script should echo back that fact along with Jonathan’s percentage of the vote:
The winner is Jonathan Haas with 53% of the vote.
Congratulations, Jonathan!
Ah, but suppose nobody gets more than 50% of the vote. In that case, the candidate who got the fewest votes is eliminated. For example, suppose that, after the first round, candidates received the following vote percentages:
Pilar Ackerman | 40% |
Syed Abbas | 30% |
Ken Myer | 20% |
Jonathan Haas | 10% |
Because he got the lowest total percentage, Jonathan Haas is eliminated from the race. That also means that the votes need to be recounted, albeit with one exception: no votes will be recorded for Jonathan Haas. Instead, Jonathan Haas will be removed from all the ballots. Remember the voter who voted like this:
1. | Jonathan Haas |
2. | Pilar Ackerman |
3. | Syed Abbas |
4. | Ken Myer |
That means that this ballot will now look like this:
1. | Pilar Ackerman |
2. | Syed Abbas |
3. | Ken Myer |
See how that works? As soon as we remove Jonathan Haas from the ballot, Pilar Ackerman (who started out as the voter’s No. 2 choice) becomes the voter’s top choice. The votes are then recounted. If no one receives more than 50% of the vote then the candidate receiving the lowest total percentage will be dropped from the ballot and then votes will be tabulated one last time.
Got all that? Good. What your script needs to do is read in the text file Votes.txt, found in the Scripting Games Competitors’ Pack (by the way, this file should be placed in the folder C:\Scripts). The script should then take an initial pass through all the ballots, tabulating the votes for each of the four candidates. Each line in the text file represents a single ballot, and looks like this, with all four candidates listed in order of preference, and the candidate names separated by a comma:
Jonathan Haas,Pilar Ackerman,Syed Abbas,Ken Myer
In this case, the voter’s first choice was Jonathan Haas; remember, we count only the first choice when tallying votes.
If no candidate receives more than 50 percent of the vote, the lowest-scoring candidate is removed from the ballot. If Jonathan Haas was the lowest-scoring candidate this ballot would now look like this, with Pilar Ackerman now becoming the voter’s top choice:
Pilar Ackerman,Syed Abbas,Ken Myer
Once Jonathan Haas is removed from the ballot, the vote totals are reset to 0, and then the revised ballots are recounted. If no candidate receives more than 50 percent of the vote, the lowest-scoring candidate is removed from the ballot and the votes are counted one last time. As soon as a candidate receives more than 50 percent of the vote the election is over, and the results need to be reported like this:
The winner is Syed Abbas with 62% of the vote.
Yes, we know: it is a little complicated. The trick to solving Event 3 is this: count the votes (remember, count only the votes for the user’s first choice) and see if anyone gets more than 50% of those votes. If they don’t, completely remove the candidate who got the lowest percentage of the vote, set the vote totals for the remaining candidates to 0, and then count again. How do you completely remove the candidate who got the lowest percentage of the vote? Sorry, but we can’t help you there; you’re on your own with that one.
Note. Whatever you do, however, don’t overwrite the file Votes.txt. If you do, your entry will be disqualified. But don’t worry; there’s no need to overwrite Votes.txt. |
Incidentally, your script does not have to worry about handling a tie; we’ve set up the data to make that there will not be a tie.