In some exercises below, we will use an electoral seat assignment as context to gain familiarity with programming tasks. In proportional elections, seats are assigned using the d'Hondt method or, in an American context, the Jefferson method. (Both methods work in a similar way and provide the same results.) In these methods, each candidate is assigned a quotient based on their partiesâ success compared to other parties and the success of them compared to other candidates in their party. The most-voted candidate from each party will have a quotient of the total number of votes from the party. The candidate with the second-most votes will have a quotient of the total votes of the party divided by two, the third with three and so on. All candidates from all parties are sorted based on their quantities, and candidates with the highest quotients are given the seat. The number of seats, or , depends on the elections.
Table 2.3 shows results from an example election with four parties, AâD. For example, candidate received 1000 votes, the most votes in the election. In total, Party B received only 1300 votes (see Table 2.4). Candidate 's quotient is 1300, and similarly, candidate 's quotient is 650. However, while candidate has fewer votes (only 750), her party has a total of 1500 votes. Therefore, candidate 's quotient is higher than candidate 's. If five candidates were elected, they would be, in order of quantities, , , , and . The proportional election system ensured Party A two seats and Parties C and D one seat each, even while their candidates performed at best similarly to Party B's candidates.
Articulating this with the terms we have learned above: Each candidate has two variables, the number of votes and the quotient. The number of votes is based on the election results (directly), while the quotient is computed based on these election results. To compute the quotient, we need to compute the total number of votes for each party. We therefore need gatherers for each party. Following this, we need to compute the quantities as indicated above to each candidate and select those candidates with the highest quantities.