diff --git a/Dhondt calculator/main.py b/Dhondt calculator/main.py new file mode 100644 index 00000000..b80fdd22 --- /dev/null +++ b/Dhondt calculator/main.py @@ -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") diff --git a/Dhondt calculator/readme.md b/Dhondt calculator/readme.md new file mode 100644 index 00000000..acae5ff1 --- /dev/null +++ b/Dhondt calculator/readme.md @@ -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 diff --git a/Dhondt calculator/src/utils.py b/Dhondt calculator/src/utils.py new file mode 100644 index 00000000..77f4aecc --- /dev/null +++ b/Dhondt calculator/src/utils.py @@ -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 + \ No newline at end of file diff --git a/README.md b/README.md index 9065422b..e3547a21 100644 --- a/README.md +++ b/README.md @@ -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. |