{"record":{"id":"5cfc9c210dd222eb","repo":"TheAlgorithms/Python","slug":"n-must-be-greater-than-or-equal-to-0-got-n-n","errorCode":null,"errorMessage":"n must be greater than or equal to 0. Got n = {n}","messagePattern":"n must be greater than or equal to 0\\. Got n = (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"dynamic_programming/rod_cutting.py","lineNumber":190,"sourceCode":"        max_rev[i] = max_revenue_i\n\n    return max_rev[n]\n\n\ndef _enforce_args(n: int, prices: list):\n    \"\"\"\n    Basic checks on the arguments to the rod-cutting algorithms\n\n    * `n`: int, the length of the rod\n    * `prices`: list, the price list for each piece of rod.\n\n    Throws ``ValueError``:\n        if `n` is negative or there are fewer items in the price list than the length of\n        the rod\n    \"\"\"\n    if n < 0:\n        msg = f\"n must be greater than or equal to 0. Got n = {n}\"\n        raise ValueError(msg)\n\n    if n > len(prices):\n        msg = (\n            \"Each integral piece of rod must have a corresponding price. \"\n            f\"Got n = {n} but length of prices = {len(prices)}\"\n        )\n        raise ValueError(msg)\n\n\ndef main():\n    prices = [6, 10, 12, 15, 20, 23]\n    n = len(prices)\n\n    # the best revenue comes from cutting the rod into 6 pieces, each\n    # of length 1 resulting in a revenue of 6 * 6 = 36.\n    expected_max_revenue = 36\n\n    max_rev_top_down = top_down_cut_rod(n, prices)","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/dynamic_programming/rod_cutting.py#L172-L208","documentation":"Raised by the shared argument-check helper (validate _style function at dynamic_programming/rod_cutting.py:190) used by top_down_cut_rod, bottom_up_cut_rod, and related rod-cutting entry points. The rod length n must be a non-negative integer because it indexes the memo/table dimension. Negative n is meaningless physically and would break indexing.","triggerScenarios":"Calling any rod-cutting function with negative n, e.g. top_down_cut_rod(-1, [6, 10, 12]) or bottom_up_cut_rod(-5, prices). Note n = 0 is valid and returns 0 revenue.","commonSituations":"Computing n as len(prices) - k or n - 1 in a loop that underflows to -1; passing a user-supplied length without a lower-bound check; subtracting a cut count larger than n.","solutions":["Fix the caller's arithmetic so n is always >= 0 (guard loop bounds, clamp with max(0, n)).","Validate n at the boundary: if n < 0: raise ValueError early in your own code with context.","Remember n = 0 is legal (empty rod, revenue 0) — only negative values fail."],"exampleFix":"# before\nn = len(prices) - cuts  # can go negative\nprint(top_down_cut_rod(n, prices))\n\n# after\nn = max(0, len(prices) - cuts)\nprint(top_down_cut_rod(n, prices))","handlingStrategy":"validation","validationCode":"def valid_rod_args(n: int, prices: list[float]) -> bool:\n    return isinstance(n, int) and n >= 0 and len(prices) >= n","typeGuard":"def is_valid_n(n: object) -> TypeGuard[int]:\n    return type(n) is int and n >= 0","tryCatchPattern":null,"preventionTips":["Clamp loop-derived lengths with max(0, n).","Treat n=0 as valid (revenue 0); only negatives fail.","Check n >= 0 where the value is computed, not inside a deep loop."],"tags":["dynamic-programming","input-validation","arithmetic"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}