From fc108c69f5810e9088cd70780a0ae0d838731204 Mon Sep 17 00:00:00 2001 From: javi0martinez Date: Wed, 19 Nov 2025 23:00:09 +0100 Subject: [PATCH 1/7] Add initial Dhondt calculator script and README Introduces the main.py file with a function for positive integer input validation and creates a placeholder readme.md for documentation. --- Dhondt calculator/main.py | 21 +++++++++++++++++++++ Dhondt calculator/readme.md | 0 2 files changed, 21 insertions(+) create mode 100644 Dhondt calculator/main.py create mode 100644 Dhondt calculator/readme.md diff --git a/Dhondt calculator/main.py b/Dhondt calculator/main.py new file mode 100644 index 00000000..599578b6 --- /dev/null +++ b/Dhondt calculator/main.py @@ -0,0 +1,21 @@ +# Get data +from typing import Optional + + +results = [] + +def get_positive_integer(prompt: str) -> Optional[int]: + value: Optional[int] = None + valid_input = False + + while not valid_input: + print(prompt) + try: + value = int(input()) + if value <= 0: + print("Please enter a positive integer.") + else: + valid_input = True + except ValueError: + print("Invalid input. Please enter a valid integer.") + return value diff --git a/Dhondt calculator/readme.md b/Dhondt calculator/readme.md new file mode 100644 index 00000000..e69de29b From 8432bc80718650f1a0b6a108faeec2403e538762 Mon Sep 17 00:00:00 2001 From: javi0martinez Date: Thu, 20 Nov 2025 23:00:28 +0100 Subject: [PATCH 2/7] Moved get_positive_integer function --- Dhondt calculator/main.py | 27 ++++++++++----------------- Dhondt calculator/src/utils.py | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 Dhondt calculator/src/utils.py diff --git a/Dhondt calculator/main.py b/Dhondt calculator/main.py index 599578b6..9b317062 100644 --- a/Dhondt calculator/main.py +++ b/Dhondt calculator/main.py @@ -1,21 +1,14 @@ +from src.utils import get_positive_integer + # Get data -from typing import Optional +results = [] + +## Number of seats to allocate +num_seats = get_positive_integer("Enter the number of seats to allocate:") + +## Number of parties +num_parties = get_positive_integer("Enter the number of parties:") -results = [] -def get_positive_integer(prompt: str) -> Optional[int]: - value: Optional[int] = None - valid_input = False - - while not valid_input: - print(prompt) - try: - value = int(input()) - if value <= 0: - print("Please enter a positive integer.") - else: - valid_input = True - except ValueError: - print("Invalid input. Please enter a valid integer.") - return value +# Print the result of seats diff --git a/Dhondt calculator/src/utils.py b/Dhondt calculator/src/utils.py new file mode 100644 index 00000000..55788d41 --- /dev/null +++ b/Dhondt calculator/src/utils.py @@ -0,0 +1,20 @@ + + +from typing import Optional + + +def get_positive_integer(prompt: str) -> Optional[int]: + value: Optional[int] = None + valid_input = False + + while not valid_input: + print(prompt) + try: + value = int(input()) + if value <= 0: + print("Please enter a positive integer.") + else: + valid_input = True + except ValueError: + print("Invalid input. Please enter a valid integer.") + return value \ No newline at end of file From ba3595272d083911966852ace44a277d6dcd107d Mon Sep 17 00:00:00 2001 From: javi0martinez Date: Thu, 20 Nov 2025 23:01:00 +0100 Subject: [PATCH 3/7] Get parties names and votes --- Dhondt calculator/main.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Dhondt calculator/main.py b/Dhondt calculator/main.py index 9b317062..aa4a9e80 100644 --- a/Dhondt calculator/main.py +++ b/Dhondt calculator/main.py @@ -9,6 +9,14 @@ ## Number of parties num_parties = get_positive_integer("Enter the number of parties:") +## Names and votes of each party +for i in range(num_parties): + party_name = input(f"Enter the name of party {i + 1}: ") + party_votes = get_positive_integer(f"Enter the number of votes for {party_name}: ") + results.append((party_name, party_votes)) + + +# Calculate the seats # Print the result of seats From 0803e3b2b356fb874390ea5d1f5e05d85bec7ac3 Mon Sep 17 00:00:00 2001 From: javi0martinez Date: Sun, 23 Nov 2025 23:03:15 +0100 Subject: [PATCH 4/7] Fix type hints --- Dhondt calculator/main.py | 11 +++++------ Dhondt calculator/src/utils.py | 19 +++++++------------ 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/Dhondt calculator/main.py b/Dhondt calculator/main.py index aa4a9e80..3cca535d 100644 --- a/Dhondt calculator/main.py +++ b/Dhondt calculator/main.py @@ -1,21 +1,20 @@ from src.utils import get_positive_integer # Get data -results = [] +results: list[tuple[str, int]] = [] ## Number of seats to allocate -num_seats = get_positive_integer("Enter the number of seats to allocate:") +num_seats: int = get_positive_integer("Enter the number of seats to allocate:") ## Number of parties -num_parties = get_positive_integer("Enter the number of parties:") +num_parties: int = get_positive_integer("Enter the number of parties:") ## Names and votes of each party for i in range(num_parties): - party_name = input(f"Enter the name of party {i + 1}: ") - party_votes = get_positive_integer(f"Enter the number of votes for {party_name}: ") + party_name: str = input(f"Enter the name of party {i + 1}: ") + party_votes: int = get_positive_integer(f"Enter the number of votes for {party_name}: ") results.append((party_name, party_votes)) - # Calculate the seats diff --git a/Dhondt calculator/src/utils.py b/Dhondt calculator/src/utils.py index 55788d41..e81ac84c 100644 --- a/Dhondt calculator/src/utils.py +++ b/Dhondt calculator/src/utils.py @@ -1,20 +1,15 @@ - - -from typing import Optional - - -def get_positive_integer(prompt: str) -> Optional[int]: - value: Optional[int] = None - valid_input = False - +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(input()) + value: int = int(input()) if value <= 0: print("Please enter a positive integer.") else: - valid_input = True + return value except ValueError: print("Invalid input. Please enter a valid integer.") - return value \ No newline at end of file + \ No newline at end of file From 90a15d9f66d9e0c1b1976cb1093bd79ad985c8bd Mon Sep 17 00:00:00 2001 From: javi0martinez Date: Mon, 24 Nov 2025 22:40:47 +0100 Subject: [PATCH 5/7] Add D'Hondt seat allocation calculation Introduced a new function `calculate_dhondt_seats` in utils.py to compute seat allocation using the D'Hondt method. Updated main.py to use this function and print the seat allocation results. Refactored imports for clarity. --- Dhondt calculator/main.py | 13 ++++++++----- Dhondt calculator/src/utils.py | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Dhondt calculator/main.py b/Dhondt calculator/main.py index 3cca535d..b80fdd22 100644 --- a/Dhondt calculator/main.py +++ b/Dhondt calculator/main.py @@ -1,21 +1,24 @@ -from src.utils import get_positive_integer +import src.utils as utils # Get data results: list[tuple[str, int]] = [] ## Number of seats to allocate -num_seats: int = get_positive_integer("Enter the 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 = get_positive_integer("Enter the 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 = get_positive_integer(f"Enter the number of votes for {party_name}: ") + 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/src/utils.py b/Dhondt calculator/src/utils.py index e81ac84c..77f4aecc 100644 --- a/Dhondt calculator/src/utils.py +++ b/Dhondt calculator/src/utils.py @@ -12,4 +12,22 @@ def get_positive_integer(prompt: str) -> int: 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 From 724779983e665cb6ad3ec0e746dfc903f2e59989 Mon Sep 17 00:00:00 2001 From: javi0martinez Date: Mon, 24 Nov 2025 22:52:40 +0100 Subject: [PATCH 6/7] Update readme.md --- Dhondt calculator/readme.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Dhondt calculator/readme.md b/Dhondt calculator/readme.md index e69de29b..acae5ff1 100644 --- a/Dhondt calculator/readme.md +++ 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 From 87f0ebf6f32de763993bd1627ec97324a3d9b14d Mon Sep 17 00:00:00 2001 From: Javier <43440043+javi0martinez@users.noreply.github.com> Date: Mon, 24 Nov 2025 23:00:21 +0100 Subject: [PATCH 7/7] Add D'Hondt calculator to README --- README.md | 1 + 1 file changed, 1 insertion(+) 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. |