Skip to main content

Largest Remainder (Hamilton method) algorithm for rounding values while preserving the total.

Project description

largest-remainder codecov

Utilities for working with the Largest Remainder (Hamilton) apportionment algorithm. The package exposes a single LargestRemainder helper that rounds floating point allocations to integers while preserving the requested total.

Features

  • ✅ Deterministic rounding that always matches the requested total
  • ✅ Supports both sequences and mappings while keeping the original order of keys
  • ✅ Fully type annotated API with comprehensive unit tests

Installation

The package is published on PyPI. Install it with your preferred Python package manager:

pip install largest-remainder
# or
uv add largest-remainder

Usage

from largest_remainder import LargestRemainder

# Round a list of quotas to an integer total
LargestRemainder.round([1, 2, 3], total=10)
# -> [2, 3, 5]

# Round a mapping and keep the original keys
LargestRemainder.round({"a": 1.0, "b": 2.0}, total=10)
# -> {"a": 3, "b": 7}

Values can be any non-negative numbers. The algorithm scales the inputs so that their sum matches the requested total and then distributes the remaining units to the elements with the largest fractional parts.

Development

  1. Install the development dependencies using uv:
    uv sync --all-extras --dev
    
  2. Run the linters and unit tests:
    uv run ruff check .
    uv run mypy
    uv run pytest
    

Contributing and community

Contributions are welcome! Please read CONTRIBUTING.md for instructions on setting up a development environment and submitting changes. Participation in this project is governed by the Code of Conduct.

Security

If you discover a security issue, please follow the process described in SECURITY.md.

License

This project is licensed under the terms of the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

largest_remainder-0.1.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

largest_remainder-0.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file largest_remainder-0.1.0.tar.gz.

File metadata

  • Download URL: largest_remainder-0.1.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for largest_remainder-0.1.0.tar.gz
Algorithm Hash digest
SHA256 aafe96c856e04705432c0174b55b3d05df03cc49b2d80f1293b72954d710cf0c
MD5 40ad728f0a36eb7be05da225184aee97
BLAKE2b-256 ebb3bc99fdc6a0648e5705f752c10a929e41d726f31cc5e59cbe4f5408358e26

See more details on using hashes here.

Provenance

The following attestation bundles were made for largest_remainder-0.1.0.tar.gz:

Publisher: release.yml on fido-id/largest-remainder-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file largest_remainder-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for largest_remainder-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f1918ca72fa7e2c66790f348f49c5bb06511512c247566d2735a2a57fd7e8fd
MD5 b1d39ffeae50b0d0edc1b7b9b904a4c6
BLAKE2b-256 3aea8d145e08efcebd680560c7d9ee87bb90513549b2f81531a784dd70105770

See more details on using hashes here.

Provenance

The following attestation bundles were made for largest_remainder-0.1.0-py3-none-any.whl:

Publisher: release.yml on fido-id/largest-remainder-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page