D’Hondt Method Calculator

hondt

D’Hondt Method is a mathematical method to calculate the number of seats for each party/list of people running for an election. It works by recursivelly applying a mathematical formula to the set of election results (the vote count). That formula is quite simple:

dhondt

Where V is the number of votes and s is the number of seats the party has so far.

Because it uses mathematical functions, control structures (loops) and lists, the D’Hondt method presents itself as a really good project for beginners in any programming language. In this example, I’ll be using Python (v3.5).

We need to start by initializing our variables. Looking at the formula above, we can see that we will need to know the number of seats available and the number of votes. Since each party will have a number of votes of its own, asking the users for the total ammount would be redundant. Instead, we’ll be asking for the number of parties running so that later on we can ask how many votes each party got (therefore, we we’ll need a list/array of parties). To get the data from the users we’ll be using the input function and casting its return value to an integer.

Remember, in Python, anything after a # is considered a comment and will not be processed.

As you can see, in the first two commands we’re using the input function with a string as an argument which is what will appear to the user in the prompt. In the other commands we’re simply stating that parties and temp will be two empty arrays. The temp array will be used later for calculations.

Now that we know how many parties there are, we need to know how they are called and how many votes each one of those parties got. In other words, we need to populate the parties array.

In the code above, you can see one of the most characteristic things in Python. Unlike most languages, it doesn’t use opening and closing statements (such as {} or endif) and you don’t need to declare the end of an expression. In Python good identation is essential as there is one expression per line and each line must be perfectly nested inside its scope.

Also in the code above, you’ll see two of the most essential control structures in any programming language: loops and if/else statements.

The for command runs the code nested inside its scope as long as the condition that is passed is true. In Python, the for loop is declared by defining a variable (that will have a different value on every loop) and a list (in this case, the numbers between 1 and the number of parties) through which it will run. Here, we could also have used any other type of loop, such as a while loop.

The first command inside the loop is nothing but a perfeccionism, but an important one for learning purposes. It will return the ordinal indicator for the current number (1 -> st, 2 -> nd, 3 -> rd, 4… -> th) It does so by using a special form of if/else statement called “ternary conditional operator” which is tipically used to save space while programming. I we were to get the ordinal indicator without using this kind of operator it would be something like:

Instead, we have:

Which works by defining a value, setting the condition on which it is true and then defining what happens if the condition isn’t met. The example above nests three of those expressions in one single line.

To finish the loop, we ask the users for the number of votes the current party got, and save them on the arrays. Since Python is also an Object-Oriented language, we do that by calling the append method on the parties array and we pass it the content. In this case, every index of the parties array will be, in itself, an array with the same info for every party.

The temp array will have exactly the same information as the parties array plus info on how many seats the party already has.

And this is where the fun part begins. Now we have to determine how many seats each party gets.

In the code above, a few things will be interesting for a beginner.

The first will be that we are declaring variables out of nowhere, which is perfectly valid in Python unlike, for instance, C or Java where you have to declare every variable and what type it has.

Second, loops inside loops, this is something quite common but that must be used carefully. Pay special attention not to use, in the loop declaration, a variable with the same name as the one you use in other loops in that scope. Loop-ception is particularly useful when you have multidimensional arrays like above, where we have several parties (1st dimension) and each party has several bits of information (2nd dimension).

Ans lastly, you can see how easy it is to access data inside a Python array. In the code above you can see it in different two ways. First when we loop through elements in the temp array.

Here, for every loop, j holds the information for that index. And you can access that information by calling the different indexes, in this case we want to get the votes for the current party.

And the second way used to get information from an array is by directly accessing an index:

Where we are getting the name of the party with most votes. Since maxParty holds the index of that party.

Now, for the algorithm used:

The D’Hondt method is used by comparing, for each party, the value that results from the application of the formula given at the begining of this article. For each seat, the party that has a bigger score, will get the seat. That being said, we must have a first loop that goes through all the seats available. Inside that loop, we must have another one that goes through all the parties, comparing the number of votes they got to the maximum so far. In the event of a tie, the winning party will be the one with less votes in total.

In the end of the first loop we change the values of the temp array for the winning party so that the number of seats is increased and the votes index holds the result from the application of the formula.

To finish, we print the results of the loop to the prompt and a summary in the end of the execution of the module:


You can download the code here: hondt.zip