{"record":{"id":"7d91eb9c9c44b1fb","repo":"TheAlgorithms/Python","slug":"partitions-must-be-a-positive-number","errorCode":null,"errorMessage":"partitions must be a positive number!","messagePattern":"partitions must be a positive number!","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/allocation_number.py","lineNumber":33,"sourceCode":"    :param number_of_bytes: the total of bytes.\n    :param partitions: the number of partition need to be allocated.\n    :return: list of bytes to be assigned to each worker thread\n\n    >>> allocation_num(16647, 4)\n    ['1-4161', '4162-8322', '8323-12483', '12484-16647']\n    >>> allocation_num(50000, 5)\n    ['1-10000', '10001-20000', '20001-30000', '30001-40000', '40001-50000']\n    >>> allocation_num(888, 999)\n    Traceback (most recent call last):\n        ...\n    ValueError: partitions can not > number_of_bytes!\n    >>> allocation_num(888, -4)\n    Traceback (most recent call last):\n        ...\n    ValueError: partitions must be a positive number!\n    \"\"\"\n    if partitions <= 0:\n        raise ValueError(\"partitions must be a positive number!\")\n    if partitions > number_of_bytes:\n        raise ValueError(\"partitions can not > number_of_bytes!\")\n    bytes_per_partition = number_of_bytes // partitions\n    allocation_list = []\n    for i in range(partitions):\n        start_bytes = i * bytes_per_partition + 1\n        end_bytes = (\n            number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition\n        )\n        allocation_list.append(f\"{start_bytes}-{end_bytes}\")\n    return allocation_list\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/allocation_number.py#L15-L51","documentation":"Raised by allocation_num in maths/allocation_number.py when the partitions argument is zero or negative. The function splits a byte range into 'partitions' contiguous chunks; a non-positive partition count makes the loop and the floor division undefined, so it is rejected first (before the partitions > number_of_bytes check).","triggerScenarios":"Calling allocation_num(888, -4) or allocation_num(50000, 0). Non-integer partitions like 2.5 would not raise here but produce wrong chunking (range() over a float raises TypeError).","commonSituations":"User-supplied split counts from CLI/config parsed as 0 or negative, computed partition counts like number_of_bytes // chunk_size underflowing to 0 for tiny inputs, or defaults left unset.","solutions":["Validate partitions >= 1 at the call site before invoking allocation_num.","If partitions is derived (e.g. total // chunk_size), clamp with max(1, ...) or guard chunk_size > total.","Validate and cast config values to positive ints when loading."],"exampleFix":"# before\nparts = allocation_num(total, total // chunk_size)  # chunk_size > total -> 0\n\n# after\npartitions = max(1, total // chunk_size) if chunk_size <= total else 1\nparts = allocation_num(total, partitions)","handlingStrategy":"validation","validationCode":"partitions = int(partitions)\nif partitions < 1:\n    raise ValueError(f'partitions must be >= 1, got {partitions}')\nranges = allocation_num(number_of_bytes, partitions)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp derived partition counts with max(1, ...).","Validate split counts from user config as positive integers."],"tags":["math","partitioning","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}