{"record":{"id":"55da08fda0d2a03b","repo":"TheAlgorithms/Python","slug":"the-position-should-be-an-integer","errorCode":null,"errorMessage":"The position should be an integer","messagePattern":"The position should be an integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_structures/arrays/kth_largest_element.py","lineNumber":97,"sourceCode":"        >>> kth_largest_element([3.1, 1.2, 5.6, 4.7,7.9,5,0], 2)\n        5.6\n        >>> kth_largest_element([-2, -5, -4, -1], 1)\n        -1\n        >>> kth_largest_element([], 1)\n        -1\n        >>> kth_largest_element([3.1, 1.2, 5.6, 4.7, 7.9, 5, 0], 1.5)\n        Traceback (most recent call last):\n        ...\n        ValueError: The position should be an integer\n        >>> kth_largest_element((4, 6, 1, 2), 4)\n        Traceback (most recent call last):\n        ...\n        TypeError: 'tuple' object does not support item assignment\n    \"\"\"\n    if not arr:\n        return -1\n    if not isinstance(position, int):\n        raise ValueError(\"The position should be an integer\")\n    if not 1 <= position <= len(arr):\n        raise ValueError(\"Invalid value of 'position'\")\n    low, high = 0, len(arr) - 1\n    while low <= high:\n        if low > len(arr) - 1 or high < 0:\n            return -1\n        pivot_index = partition(arr, low, high)\n        if pivot_index == position - 1:\n            return arr[pivot_index]\n        elif pivot_index > position - 1:\n            high = pivot_index - 1\n        else:\n            low = pivot_index + 1\n    return -1\n\n\nif __name__ == \"__main__\":\n    import doctest","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/arrays/kth_largest_element.py#L79-L115","documentation":"Raised by kth_largest_element() in data_structures/arrays/kth_largest_element.py when position is not an int (e.g. 1.5, '2', None). Note it raises ValueError, not TypeError, despite being a type problem — a quirk of this implementation. It fires only for non-empty arrays, since empty arrays return -1 first.","triggerScenarios":"Calling kth_largest_element([3,1,5,4,7,5,0], 1.5) or passing position as a string ('2'), a float parsed from input, or numpy float scalar.","commonSituations":"Position read from CLI args (always strings) without int() conversion, or from JSON where it deserializes as a float (e.g. 2.0 — note isinstance(2.0, int) is False so even whole floats raise).","solutions":["Convert to int at the call site: kth_largest_element(arr, int(position)).","For float sources, verify the value is integral first: if position != int(position): reject.","Parse CLI/config values with int() at the boundary."],"exampleFix":"# before\nkth_largest_element([3, 1, 5, 4, 7, 5, 0], 1.5)  # ValueError\n\n# after\nkth_largest_element([3, 1, 5, 4, 7, 5, 0], 2)  # 5","handlingStrategy":"type-guard","validationCode":"if not isinstance(position, int):\n    position = int(position)  # or reject\nresult = kth_largest_element(arr, position)","typeGuard":"def is_int(value: object) -> bool:\n    return isinstance(value, int) and not isinstance(value, bool)","tryCatchPattern":null,"preventionTips":["Convert CLI/config k values with int() at the boundary","Note this raises ValueError for type problems, not TypeError","Even 2.0 (whole float) is rejected — use real ints"],"tags":["type-validation","array","selection","quickselect"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}