{"record":{"id":"ea5446ae7ec84b30","repo":"TheAlgorithms/Python","slug":"the-given-input-must-be-positive","errorCode":null,"errorMessage":"The given input must be positive","messagePattern":"The given input must be positive","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/gray_code_sequence.py","lineNumber":38,"sourceCode":"    [0, 1]\n\n    >>> gray_code(3)\n    [0, 1, 3, 2, 6, 7, 5, 4]\n\n    >>> gray_code(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: The given input must be positive\n\n    >>> gray_code(10.6)\n    Traceback (most recent call last):\n        ...\n    TypeError: unsupported operand type(s) for <<: 'int' and 'float'\n    \"\"\"\n\n    # bit count represents no. of bits in the gray code\n    if bit_count < 0:\n        raise ValueError(\"The given input must be positive\")\n\n    # get the generated string sequence\n    sequence = gray_code_sequence_string(bit_count)\n    #\n    # convert them to integers\n    for i in range(len(sequence)):\n        sequence[i] = int(sequence[i], 2)\n\n    return sequence\n\n\ndef gray_code_sequence_string(bit_count: int) -> list:\n    \"\"\"\n    Will output the n-bit grey sequence as a\n    string of bits\n\n    >>> gray_code_sequence_string(2)\n    ['00', '01', '11', '10']","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/gray_code_sequence.py#L20-L56","documentation":"Raised by gray_code when bit_count is negative. Gray code sequences are defined for a non-negative bit width; a negative width cannot generate a sequence, so it is rejected. Note: floats like 10.6 pass this guard and fail later with TypeError from the `<<` operator inside gray_code_sequence_string.","triggerScenarios":"Calling gray_code(-1) or any negative int. gray_code(10.6) does NOT raise this — it raises TypeError from `1 << bit_count` in the helper.","commonSituations":"Deriving the bit width from data (e.g., width = maxlen - k) that goes negative on degenerate input; passing a size parameter from user config without validation.","solutions":["Clamp or validate the computed width: only call gray_code when bit_count >= 0.","Use max(0, bit_count) if an empty/zero-width result is acceptable for your use case.","For float widths, int() the value first so you fail fast with this clear ValueError instead of the later operand TypeError."],"exampleFix":"# before\ngray_code(bits - offset)  # ValueError when offset > bits\n\n# after\nwidth = max(0, bits - offset)\ngray_code(width)","handlingStrategy":"validation","validationCode":"bit_count = int(bit_count)\nif bit_count < 0:\n    raise ValueError(\"bit_count must be non-negative\")","typeGuard":"def is_non_negative_int(n: object) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n >= 0","tryCatchPattern":null,"preventionTips":["Clamp derived widths with max(0, width).","int() float widths so you get this clear ValueError instead of a later operand TypeError.","Validate width parameters from config before generating sequences."],"tags":["bit-manipulation","input-validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}