{"record":{"id":"7e6e69ef1f253a77","repo":"TheAlgorithms/Python","slug":"num-people-or-step-size-is-not-a-positive-integer","errorCode":null,"errorMessage":"num_people or step_size is not a positive integer.","messagePattern":"num_people or step_size is not a positive integer\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/josephus_problem.py","lineNumber":72,"sourceCode":"        Traceback (most recent call last):\n            ...\n        ValueError: num_people or step_size is not a positive integer.\n        >>> josephus_recursive(1_000, 0.01)\n        Traceback (most recent call last):\n            ...\n        ValueError: num_people or step_size is not a positive integer.\n        >>> josephus_recursive(\"cat\", \"dog\")\n        Traceback (most recent call last):\n            ...\n        ValueError: num_people or step_size is not a positive integer.\n    \"\"\"\n    if (\n        not isinstance(num_people, int)\n        or not isinstance(step_size, int)\n        or num_people <= 0\n        or step_size <= 0\n    ):\n        raise ValueError(\"num_people or step_size is not a positive integer.\")\n\n    if num_people == 1:\n        return 0\n\n    return (josephus_recursive(num_people - 1, step_size) + step_size) % num_people\n\n\ndef find_winner(num_people: int, step_size: int) -> int:\n    \"\"\"\n    Find the winner of the Josephus problem for num_people and a step_size.\n\n    Args:\n        num_people (int): Number of people.\n        step_size (int): Step size for elimination.\n\n    Returns:\n        int: The position of the last person remaining (1-based index).\n","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/josephus_problem.py#L54-L90","documentation":"Raised by josephus_recursive in maths/josephus_problem.py when num_people or step_size is not an int, or either is <= 0. The Josephus recurrence (winner of n people = (winner of n-1 + step) % n) requires at least one person and a positive step; the combined guard rejects floats, strings, zero, and negatives with ValueError before recursion starts.","triggerScenarios":"Calling josephus_recursive(0, 3), josephus_recursive(5, 0), josephus_recursive(-2, 3), josephus_recursive(5.0, 2), or josephus_recursive('cat', 'dog'). Any one invalid argument triggers the raise.","commonSituations":"Simulation parameters read as strings from config/CLI; step_size defaulting to 0 or None; counts computed as floats from averaging logic; passing num_people from len() of an empty collection (0).","solutions":["Validate both parameters as positive ints before calling: int(n) >= 1 and int(k) >= 1.","Guard against empty groups: if not people: skip instead of calling with 0.","Coerce numeric config values (e.g. int(step_cfg)) at load time so the hot path always sees ints."],"exampleFix":"// before\nwinner = josephus_recursive(num_people, step_size)  # raw config values\n\n// after\nnum_people, step_size = int(num_people), int(step_size)\nif num_people < 1 or step_size < 1:\n    raise ValueError(f\"need positive ints, got {num_people=}, {step_size=}\")\nwinner = josephus_recursive(num_people, step_size)","handlingStrategy":"validation","validationCode":"num_people, step_size = int(num_people), int(step_size)\nif num_people < 1 or step_size < 1:\n    raise ValueError(\"num_people and step_size must be positive integers\")\nw = josephus_recursive(num_people, step_size)","typeGuard":"def valid_josephus_args(n, k) -> bool:\n    return (\n        isinstance(n, int) and isinstance(k, int)\n        and not isinstance(n, bool) and not isinstance(k, bool)\n        and n > 0 and k > 0\n    )","tryCatchPattern":"try:\n    w = josephus_recursive(n, k)\nexcept ValueError:\n    n, k = int(n), int(k)\n    w = josephus_recursive(n, k)","preventionTips":["Coerce config/CLI values to int once at load","Guard len()-derived counts against empty collections"],"tags":["math","josephus","recursion","validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}