{"record":{"id":"366fb9dedb25975d","repo":"TheAlgorithms/Python","slug":"step-size-must-be-positive-and-non-zero","errorCode":null,"errorMessage":"Step size must be positive and non-zero.","messagePattern":"Step size must be positive and non-zero\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"matrix/matrix_equalization.py","lineNumber":31,"sourceCode":"    5\n    >>> array_equalization([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 5)\n    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","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/matrix/matrix_equalization.py#L13-L49","documentation":"Raised by array_equalization when the step_size argument is <= 0. The function scans the vector in increments of step_size to count updates needed to equalize elements, so a zero or negative step would cause an infinite loop or backwards scan; the guard rejects it up front. Valid steps are strictly positive integers (a separate check enforces the integer part).","triggerScenarios":"Calling array_equalization(vector, 0), array_equalization(vector, -1), or passing a computed step like len(sublist) - len(other_list) that evaluates to 0 or negative. Note the <= 0 check runs before the isinstance check, so a non-numeric step that cannot be compared (e.g. a string) raises TypeError from the comparison instead.","commonSituations":"Deriving step_size from user input or array lengths without clamping; passing -1 as an 'all elements' sentinel from another API's convention; off-by-one errors where an empty segment yields step 0.","solutions":["Clamp the computed step to at least 1 before calling: step = max(1, step).","Validate the value at the boundary where it enters your code (CLI arg, config, function parameter) and reject <= 0 with a clear message.","If the step is computed from lengths, debug why it became 0 or negative (e.g. len(a) - len(b) with a shorter than b)."],"exampleFix":"# before\nstep = len(batch_a) - len(batch_b)  # can be <= 0\narray_equalization(vector, step)\n\n# after\nstep = max(1, len(batch_a) - len(batch_b))\narray_equalization(vector, step)","handlingStrategy":"validation","validationCode":"if not isinstance(step_size, int) or step_size <= 0:\n    raise ValueError(f\"step_size must be a positive int, got {step_size!r}\")\nresult = array_equalization(vector, step_size)","typeGuard":"def is_positive_int(x) -> bool:\n    \"\"\"Guard: strictly positive native integer.\"\"\"\n    return isinstance(x, int) and not isinstance(x, bool) and x > 0","tryCatchPattern":"try:\n    result = array_equalization(vector, step)\nexcept ValueError as e:\n    if \"positive and non-zero\" in str(e):\n        step = max(1, step)\n        result = array_equalization(vector, step)\n    else:\n        raise","preventionTips":["Clamp computed steps with max(1, computed_step).","Validate step values where they enter your program (CLI/config), not deep in call chains.","Remember the function checks positivity before type, so non-numeric steps fail with a comparison TypeError instead."],"tags":["matrix","validation","step-size","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}