TheAlgorithms/Python · error · ValueError
Step size must be an integer.
Error message
Step size must be an integer.
What it means
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.
Source
Thrown at matrix/matrix_equalization.py:33
0
>>> array_equalization([22, 22, 22, 33, 33, 33], 2)
2
>>> array_equalization([1, 2, 3], 0)
Traceback (most recent call last):
ValueError: Step size must be positive and non-zero.
>>> array_equalization([1, 2, 3], -1)
Traceback (most recent call last):
ValueError: Step size must be positive and non-zero.
>>> array_equalization([1, 2, 3], 0.5)
Traceback (most recent call last):
ValueError: Step size must be an integer.
>>> array_equalization([1, 2, 3], maxsize)
1
"""
if step_size <= 0:
raise ValueError("Step size must be positive and non-zero.")
if not isinstance(step_size, int):
raise ValueError("Step size must be an integer.")
unique_elements = set(vector)
min_updates = maxsize
for element in unique_elements:
elem_index = 0
updates = 0
while elem_index < len(vector):
if vector[elem_index] != element:
updates += 1
elem_index += step_size
else:
elem_index += 1
min_updates = min(min_updates, updates)
return min_updates
View on GitHub (pinned to f5988cc097)
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.
Example fix
# before step = len(vector) / 4 # e.g. 2.5 array_equalization(vector, step) # after step = len(vector) // 4 # integer array_equalization(vector, step)
Defensive patterns
Strategy: validation
Validate before calling
if not isinstance(step_size, int):
if isinstance(step_size, float) and step_size.is_integer():
step_size = int(step_size)
else:
raise TypeError(f"step_size must be int, got {type(step_size).__name__}")
result = array_equalization(vector, step_size) Type guard
def is_int_step(x) -> bool:
"""Guard: native int (bool excluded); accepts integral floats via caller coercion."""
return isinstance(x, int) and not isinstance(x, bool) Try / catch
try:
result = array_equalization(vector, step)
except ValueError as e:
if "must be an integer" in str(e) and float(step).is_integer():
result = array_equalization(vector, int(step))
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Step computed with true division (/) instead of floor division (//); config values parsed as floats; NumPy scalar leakage into pure-Python logic.
Related errors
- A Matrix can only be multiplied by an int, float, or another
- A Matrix can only be raised to the power of an int
- Step size must be positive and non-zero.
- Expected a matrix, got int/list instead
- The input value of 'num_rows' should be 'int'
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/352dd997cf7af21a.
Report an issue: GitHub.