{"id":10065,"library":"prtpy","title":"prtpy - Number Partitioning","description":"prtpy is a Python library for number partitioning. It provides various algorithms to divide a list of numbers into a specified number of subsets, typically aiming to minimize the largest sum of a subset. It is actively maintained, with the current version being 0.8.3, and releases occur as new algorithms, improvements, or bug fixes are introduced.","status":"active","version":"0.8.3","language":"en","source_language":"en","source_url":"https://github.com/erelsgl/prtpy","tags":["number-partitioning","optimization","algorithms","mathematics"],"install":[{"cmd":"pip install prtpy","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for the `ilp` (Integer Linear Programming) partitioning algorithm.","package":"pulp","optional":true}],"imports":[{"note":"Imports the main partitioning wrapper function.","symbol":"partition","correct":"from prtpy import partition"},{"note":"Imports a specific partitioning algorithm (e.g., greedy).","symbol":"greedy","correct":"from prtpy.partition import greedy"},{"note":"Imports the ILP partitioning algorithm, which requires the `pulp` package and an installed solver.","symbol":"ilp","correct":"from prtpy.partition import ilp"}],"quickstart":{"code":"from prtpy import partition\nfrom prtpy.partition import greedy\n\nnumbers = [3, 4, 5, 6, 7, 8, 9, 10]\nnum_bins = 3\n\n# Partition the numbers into 3 bins using the greedy algorithm\nresult = partition(algorithm=greedy, num_bins=num_bins, items=numbers)\n\nprint(f\"Original numbers: {numbers}\")\nprint(f\"Number of bins: {num_bins}\")\nprint(f\"Partition result (greedy): {result}\")\n# Expected output for the given input might be similar to: [[10, 8, 3], [9, 7, 4], [6, 5]]","lang":"python","description":"This quickstart demonstrates how to use `prtpy` to partition a list of numbers into a specified number of bins using the greedy algorithm. The `partition` function acts as a general interface, accepting the algorithm as a parameter."},"warnings":[{"fix":"Install `pulp` via `pip install pulp` and ensure a compatible solver (e.g., `cbc` is often included with `pulp` or can be installed separately) is available in your system's PATH.","message":"The `ilp` (Integer Linear Programming) algorithm requires the `pulp` library and an external solver (e.g., CBC, GLPK) to be installed. Without these, using `prtpy.partition.ilp` will result in a `ModuleNotFoundError` for `pulp` or a `PulpSolverError` if no solver is found.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For large datasets or performance-critical applications, prefer polynomial-time algorithms like `greedy`, `balancer`, or `complete_greedy`. Consult the documentation for the complexity characteristics of each algorithm.","message":"Some partitioning algorithms, such as `prtpy.partition.recursive` and `prtpy.partition.schroeppel`, have exponential time complexity. They are highly efficient for small inputs but can become extremely slow or unresponsive for larger lists of numbers.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always ensure that the list of items you pass to `prtpy` algorithms contains only numbers. Validate or sanitize your input data if it originates from external sources.","message":"The `items` argument for partitioning functions expects an iterable of numerical values (integers or floats). Providing non-numerical types (e.g., strings) will lead to `TypeError` or unexpected behavior during comparisons or arithmetic operations.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Install the `pulp` library using `pip install pulp`.","cause":"You are attempting to use the `prtpy.partition.ilp` algorithm without having the `pulp` library installed.","error":"ModuleNotFoundError: No module named 'pulp'"},{"fix":"Ensure all items in the list passed to `prtpy` algorithms are numerical (integers or floats).","cause":"One or more elements in your `items` list are strings (or other non-numeric types) instead of numbers, which `prtpy` algorithms expect.","error":"TypeError: '<' not supported between instances of 'str' and 'int'"},{"fix":"Switch to a polynomial-time complexity algorithm for large inputs, such as `greedy`, `balancer`, or `complete_greedy`, which are much more performant.","cause":"You are likely using an exponential-time complexity algorithm (e.g., `recursive`, `schroeppel`) with a large number of input items.","error":"Program runs indefinitely or extremely slowly for large inputs."}]}