You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: Minimax crashes with RecursionError on non-power-of-2 input sizes #14886
backtracking/minimax.py uses math.log(len(scores), 2) to compute tree height, then checks depth == height as the base case. Since math.log returns a float, the comparison int == float never succeeds for non-power-of-2 input sizes, causing infinite recursion.
height=math.log(len(scores), 2) # e.g. math.log(6, 2) = 2.584...# ...ifdepth==height: # depth is int, height is float 2.584 -- NEVER TRUEreturnscores[node_index]
The doctests only pass because they use size 8 (2^3).
Suggested Fix
Either validate input or use integer height:
importmathdefminimax(depth, node_index, is_max, scores, height):
ifdepth<0:
raiseValueError("Depth cannot be less than 0")
ifnotscores:
raiseValueError("Scores cannot be empty")
ifnot (len(scores) & (len(scores) -1) ==0):
raiseValueError("Number of scores must be a power of 2")
# ...
Or rewrite to use array slicing instead of index arithmetic, which works for any size.
Impact
Any user who passes a non-power-of-2 list gets an unhandled RecursionError. There is no documentation about this constraint.
Activity