Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Dhondt calculator/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import src.utils as utils

# Get data
results: list[tuple[str, int]] = []

## Number of seats to allocate
num_seats: int = utils.get_positive_integer("Enter the number of seats to allocate:")

## Number of parties
num_parties: int = utils.get_positive_integer("Enter the number of parties:")

## Names and votes of each party
for i in range(num_parties):
party_name: str = input(f"Enter the name of party {i + 1}: ")
party_votes: int = utils.get_positive_integer(f"Enter the number of votes for {party_name}: ")
results.append((party_name, party_votes))

# Calculate the seats
seats_allocated: dict[str, int] = utils.calculate_dhondt_seats(results, num_seats)

# Print the result of seats
print("\nSeat allocation results:")
for party, seats in seats_allocated.items():
print(f"{party}: {seats} seats")
14 changes: 14 additions & 0 deletions Dhondt calculator/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# D'Hondt Calculator

A Python implementation of the D'Hondt method for seat allocation in proportional representation electoral systems.

## Description

The D'Hondt method is a highest averages method for allocating seats in parliamentary systems. This calculator allows you to input election results and automatically calculates how seats should be distributed among parties according to the D'Hondt formula.

## How It Works

The D'Hondt method works by:
1. Dividing each party's vote total by 1, 2, 3, etc.
2. Allocating seats one by one to the party with the highest quotient
3. Repeating until all seats are allocated
33 changes: 33 additions & 0 deletions Dhondt calculator/src/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def get_positive_integer(prompt: str) -> int:
"""Prompt the user to enter a positive integer."""

valid_input: bool = False
while not valid_input:
print(prompt)
try:
value: int = int(input())
if value <= 0:
print("Please enter a positive integer.")
else:
return value
except ValueError:
print("Invalid input. Please enter a valid integer.")

def calculate_dhondt_seats(results: list[tuple[str, int]], num_seats: int) -> dict[str, int]:
"""Calculate the number of seats allocated to each party using the D'Hondt method."""

seats_allocated: dict[str, int] = {party: 0 for party, _ in results}
votes: dict[str, int] = {party: votes for party, votes in results}

for _ in range(num_seats):
# Calculate the highest quotient for each party
quotients: dict[str, float] = {
party: votes[party] / (seats_allocated[party] + 1) for party in votes
}
# Find the party with the highest quotient
winning_party: str = max(quotients, key=lambda party: quotients[party])
# Allocate a seat to that party
seats_allocated[winning_party] += 1

return seats_allocated

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ More information on contributing and the general code of conduct for discussion
| CSV to Excel | [CSV to Excel](https://github.com/DhanushNehru/Python-Scripts/tree/main/CSV%20to%20Excel) | A Python script to convert a CSV to an Excel file. |
| CSV_TO_NDJSON | [CSV to Excel](https://github.com/DhanushNehru/Python-Scripts/tree/main/CSV_TO_NDJSON) | A Python script to convert a CSV to an NDJSON files file. |
| Currency Script | [Currency Script](https://github.com/DhanushNehru/Python-Scripts/tree/main/Currency%20Script) | A Python script to convert the currency of one country to that of another. |
| D'Hondt calculator | [D'Hondt calculator](https://github.com/DhanushNehru/Python-Scripts/tree/main/DHondt%calculator) | A Python script to calculate the number of seats that a party get giving the electoral results |
| Digital Clock | [Digital Clock](https://github.com/DhanushNehru/Python-Scripts/tree/main/Digital%20Clock) | A Python script to preview a digital clock in the terminal. |
| Disk Usage Visualizer | [Disk Usage Visualizer](https://github.com/DhanushNehru/Python-Scripts/tree/main/Disk%20Usage%20Visualizer) | A Python script to display the top N directories taking up space in your disk.
| Display Popup Window | [Display Popup Window](https://github.com/DhanushNehru/Python-Scripts/tree/main/Display%20Popup%20Window) | A Python script to preview a GUI interface to the user. |
Expand Down