{"record":{"id":"b0e676890edeb991","repo":"TheAlgorithms/Python","slug":"numbers-must-be-an-iterable-of-integers","errorCode":null,"errorMessage":"numbers must be an iterable of integers","messagePattern":"numbers must be an iterable of integers","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"dynamic_programming/max_product_subarray.py","lineNumber":39,"sourceCode":"    0\n    >>> max_product_subarray(None)\n    0\n    >>> max_product_subarray([2, 3, -2, 4.5, -1])\n    Traceback (most recent call last):\n        ...\n    ValueError: numbers must be an iterable of integers\n    >>> max_product_subarray(\"ABC\")\n    Traceback (most recent call last):\n        ...\n    ValueError: numbers must be an iterable of integers\n    \"\"\"\n    if not numbers:\n        return 0\n\n    if not isinstance(numbers, (list, tuple)) or not all(\n        isinstance(number, int) for number in numbers\n    ):\n        raise ValueError(\"numbers must be an iterable of integers\")\n\n    max_till_now = min_till_now = max_prod = numbers[0]\n\n    for i in range(1, len(numbers)):\n        # update the maximum and minimum subarray products\n        number = numbers[i]\n        if number < 0:\n            max_till_now, min_till_now = min_till_now, max_till_now\n        max_till_now = max(number, max_till_now * number)\n        min_till_now = min(number, min_till_now * number)\n\n        # update the maximum product found till now\n        max_prod = max(max_prod, max_till_now)\n\n    return max_prod\n","sourceCodeStart":21,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/dynamic_programming/max_product_subarray.py#L21-L55","documentation":"Raised by max_product_subarray(numbers) when numbers is not a list/tuple or contains any non-int element. Empty input is special-cased earlier to return 0, so this ValueError means you passed a non-sequence (e.g. an int or string) or a sequence with float/string/None elements. The isinstance((list, tuple)) check also rejects generators and numpy arrays even if their elements are ints.","triggerScenarios":"max_product_subarray('ABC') as in the doctest; max_product_subarray([1, 2.5]) with a float element; max_product_subarray(numpy_array) or max_product_subarray(x for x in data) — both fail the list/tuple check.","commonSituations":"Numeric data parsed as floats from JSON/CSV ('2.5', '3.0'); numpy arrays from ML pipelines; generator expressions chained from upstream transformations.","solutions":["Pass a plain list of ints: max_product_subarray([int(x) for x in numbers]).","Convert numpy arrays with array.tolist().","Materialize generators with list(...) before the call."],"exampleFix":"# before\nbest = max_product_subarray(arr)  # numpy array -> ValueError\n\n# after\nbest = max_product_subarray(arr.tolist())","handlingStrategy":"type-guard","validationCode":"if not isinstance(numbers, (list, tuple)):\n    numbers = list(numbers)\nnumbers = [int(n) for n in numbers]\nbest = max_product_subarray(numbers)","typeGuard":"def is_int_sequence(values: object) -> bool:\n    return isinstance(values, (list, tuple)) and all(\n        isinstance(v, int) and not isinstance(v, bool) for v in values\n    )","tryCatchPattern":"try:\n    best = max_product_subarray(numbers)\nexcept ValueError as exc:\n    if 'iterable of integers' in str(exc):\n        best = max_product_subarray([int(n) for n in list(numbers)])\n    else:\n        raise","preventionTips":["Normalize numeric pipelines to list[int] before calling pure-Python DP helpers.","Use .tolist() when bridging from numpy.","Coerce float parses to int only after confirming values are integral."],"tags":["python","input-validation","type-error","dynamic-programming"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}