{"record":{"id":"5cbbe12de9759272","repo":"TheAlgorithms/Python","slug":"partitions-can-not-number-of-bytes","errorCode":null,"errorMessage":"partitions can not > number_of_bytes!","messagePattern":"partitions can not > number_of_bytes!","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/allocation_number.py","lineNumber":35,"sourceCode":"    :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":17,"sourceCodeEnd":51,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/allocation_number.py#L17-L51","documentation":"Raised by allocation_num when the requested number of partitions exceeds number_of_bytes. With floor division each partition would get 0 bytes, producing empty/degenerate ranges, so the function refuses. The positive-partition check at line 33 runs first, so this error only fires for partitions >= 1 that are simply too numerous.","triggerScenarios":"Calling allocation_num(888, 999) - any call where 1 <= partitions <= 0-edge is fine but partitions > number_of_bytes raises. Minimum viable input is partitions == number_of_bytes (each chunk gets 1 byte).","commonSituations":"Auto-scaling split counts from a target chunk size without bounding by total size, hardcoding partition counts for small test files, or unit-of-measure confusion (KB chunk count vs byte total).","solutions":["Cap partitions at number_of_bytes: partitions = min(partitions, number_of_bytes).","Derive partitions from the size instead of a fixed constant, e.g. partitions = max(1, min(requested, number_of_bytes)).","Add a call-site assertion so oversized partition requests fail with your own clearer error."],"exampleFix":"# before\nranges = allocation_num(888, 999)\n\n# after\nranges = allocation_num(888, min(999, 888))  # cap at total size","handlingStrategy":"validation","validationCode":"partitions = min(int(partitions), number_of_bytes)\nif partitions < 1:\n    raise ValueError('partitions must be >= 1')\nranges = allocation_num(number_of_bytes, partitions)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always cap partitions by the total size before calling.","Derive partition counts from the input size rather than fixed constants.","Add call-site assertions for small test inputs where hardcoded counts overshoot."],"tags":["math","partitioning","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}