{"record":{"id":"352dd997cf7af21a","repo":"TheAlgorithms/Python","slug":"step-size-must-be-an-integer","errorCode":null,"errorMessage":"Step size must be an integer.","messagePattern":"Step size must be an integer\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"matrix/matrix_equalization.py","lineNumber":33,"sourceCode":"    0\n    >>> array_equalization([22, 22, 22, 33, 33, 33], 2)\n    2\n    >>> array_equalization([1, 2, 3], 0)\n    Traceback (most recent call last):\n    ValueError: Step size must be positive and non-zero.\n    >>> array_equalization([1, 2, 3], -1)\n    Traceback (most recent call last):\n    ValueError: Step size must be positive and non-zero.\n    >>> array_equalization([1, 2, 3], 0.5)\n    Traceback (most recent call last):\n    ValueError: Step size must be an integer.\n    >>> array_equalization([1, 2, 3], maxsize)\n    1\n    \"\"\"\n    if step_size <= 0:\n        raise ValueError(\"Step size must be positive and non-zero.\")\n    if not isinstance(step_size, int):\n        raise ValueError(\"Step size must be an integer.\")\n\n    unique_elements = set(vector)\n    min_updates = maxsize\n\n    for element in unique_elements:\n        elem_index = 0\n        updates = 0\n        while elem_index < len(vector):\n            if vector[elem_index] != element:\n                updates += 1\n                elem_index += step_size\n            else:\n                elem_index += 1\n        min_updates = min(min_updates, updates)\n\n    return min_updates\n\n","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/matrix/matrix_equalization.py#L15-L51","documentation":"Raised by array_equalization when step_size is not an int (e.g. 0.5). The algorithm advances an index by step_size each iteration and indexes into a list, which only makes sense for whole-number increments. The isinstance(int) check runs after the positivity check, so a positive float gets this specific error while a negative float gets the positivity error first. Booleans pass (bool subclasses int) even though they are probably not intended.","triggerScenarios":"array_equalization([1, 2, 3], 0.5), passing a numpy float (np.float64(2.0)), or a fractional step derived from a division (e.g. n / 2 where n is odd). Strings and other non-comparable types crash earlier in the <= 0 comparison with a different TypeError.","commonSituations":"Step computed with true division (/) instead of floor division (//); config values parsed as floats; NumPy scalar leakage into pure-Python logic.","solutions":["Use floor division or int() when computing the step: step = total // groups instead of total / groups.","Coerce at the call site: array_equalization(vector, int(step)) once you have confirmed the fractional value is acceptable to truncate.","Validate external input (CLI/config) with isinstance(step, int) and reject or convert explicitly."],"exampleFix":"# before\nstep = len(vector) / 4  # e.g. 2.5\narray_equalization(vector, step)\n\n# after\nstep = len(vector) // 4  # integer\narray_equalization(vector, step)","handlingStrategy":"validation","validationCode":"if not isinstance(step_size, int):\n    if isinstance(step_size, float) and step_size.is_integer():\n        step_size = int(step_size)\n    else:\n        raise TypeError(f\"step_size must be int, got {type(step_size).__name__}\")\nresult = array_equalization(vector, step_size)","typeGuard":"def is_int_step(x) -> bool:\n    \"\"\"Guard: native int (bool excluded); accepts integral floats via caller coercion.\"\"\"\n    return isinstance(x, int) and not isinstance(x, bool)","tryCatchPattern":"try:\n    result = array_equalization(vector, step)\nexcept ValueError as e:\n    if \"must be an integer\" in str(e) and float(step).is_integer():\n        result = array_equalization(vector, int(step))\n    else:\n        raise","preventionTips":["Use // instead of / when computing step counts from lengths.","Convert numpy scalars with int() or .item() before passing them.","Keep in mind bool passes the isinstance(int) check — exclude it explicitly if True/False could reach this API."],"tags":["matrix","typeerror","step-size","integer-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}