{"record":{"id":"02c37a2c3aca1017","repo":"TheAlgorithms/Python","slug":"max-weight-must-greater-than-zero","errorCode":null,"errorMessage":"max_weight must greater than zero.","messagePattern":"max_weight must greater than zero\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"knapsack/greedy_knapsack.py","lineNumber":36,"sourceCode":"\n\ndef calc_profit(profit: list, weight: list, max_weight: int) -> int:\n    \"\"\"\n    Function description is as follows-\n    :param profit: Take a list of profits\n    :param weight: Take a list of weight if bags corresponding to the profits\n    :param max_weight: Maximum weight that could be carried\n    :return: Maximum expected gain\n\n    >>> calc_profit([1, 2, 3], [3, 4, 5], 15)\n    6\n    >>> calc_profit([10, 9 , 8], [3 ,4 , 5], 25)\n    27\n    \"\"\"\n    if len(profit) != len(weight):\n        raise ValueError(\"The length of profit and weight must be same.\")\n    if max_weight <= 0:\n        raise ValueError(\"max_weight must greater than zero.\")\n    if any(p < 0 for p in profit):\n        raise ValueError(\"Profit can not be negative.\")\n    if any(w < 0 for w in weight):\n        raise ValueError(\"Weight can not be negative.\")\n\n    # List created to store profit gained for the 1kg in case of each weight\n    # respectively.  Calculate and append profit/weight for each element.\n    profit_by_weight = [p / w for p, w in zip(profit, weight)]\n\n    # Creating a copy of the list and sorting profit/weight in ascending order\n    sorted_profit_by_weight = sorted(profit_by_weight)\n\n    # declaring useful variables\n    length = len(sorted_profit_by_weight)\n    limit = 0\n    gain = 0\n    i = 0\n","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/knapsack/greedy_knapsack.py#L18-L54","documentation":"Thrown by calc_profit() when max_weight <= 0. A knapsack with zero or negative capacity can carry nothing (or is nonsensical), and the greedy loop's invariant remaining_capacity > 0 would be violated immediately, so the function rejects it with ValueError rather than returning a misleading 0.","triggerScenarios":"Calling calc_profit(profit, weight, 0) or with a negative max_weight; passing a computed capacity like limit - current_load that has reached zero or gone below.","commonSituations":"Capacity derived from a budget/remaining-space calculation that hit zero; config files defaulting capacity to 0; unit tests using 0 as a 'no capacity' placeholder.","solutions":["Pass a strictly positive max_weight.","Short-circuit at the caller: if max_weight <= 0, return 0 gain without calling.","Use None (not 0) as the 'unset capacity' sentinel and validate config before the call."],"exampleFix":"# before\ncalc_profit(profits, weights, remaining_space)  # remaining_space may be 0\n\n# after\nif remaining_space <= 0:\n    gain = 0\nelse:\n    gain = calc_profit(profits, weights, remaining_space)","handlingStrategy":"validation","validationCode":"if max_weight <= 0:\n    gain = 0  # nothing can be carried\nelse:\n    gain = calc_profit(profit, weight, max_weight)","typeGuard":"def is_positive_capacity(max_weight: int | float) -> bool:\n    return isinstance(max_weight, (int, float)) and max_weight > 0","tryCatchPattern":"try:\n    gain = calc_profit(profit, weight, max_weight)\nexcept ValueError as e:\n    if \"max_weight\" in str(e):\n        gain = 0\n    else:\n        raise","preventionTips":["Short-circuit zero/negative capacity at the caller.","Use None as the unset-capacity sentinel in configs.","Compute remaining capacity once and validate it before reuse."],"tags":["knapsack","greedy","validation","capacity"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}