From d7f9221b9906e3daacaad20c706718ab7a6d622c Mon Sep 17 00:00:00 2001 From: Justin Chiam <235694260+justin-chiam@users.noreply.github.com> Date: Thu, 2 Jul 2026 15:50:44 +1000 Subject: [PATCH] fix: validate scores length is a power of 2 in minimax --- backtracking/minimax.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/backtracking/minimax.py b/backtracking/minimax.py index 4eef90b75483..96fd88cd5afa 100644 --- a/backtracking/minimax.py +++ b/backtracking/minimax.py @@ -46,6 +46,10 @@ def minimax( Traceback (most recent call last): ... ValueError: Scores cannot be empty + >>> minimax(0, 0, True, [1, 2, 3], 2) + Traceback (most recent call last): + ... + ValueError: Number of scores must be a power of 2 >>> scores = [3, 5, 2, 9, 12, 5, 23, 23] >>> height = math.log(len(scores), 2) >>> minimax(0, 0, True, scores, height) @@ -56,6 +60,8 @@ def minimax( raise ValueError("Depth cannot be less than 0") if len(scores) == 0: raise ValueError("Scores cannot be empty") + if len(scores) & (len(scores) - 1) != 0: + raise ValueError("Number of scores must be a power of 2") # Base case: If the current depth equals the height of the tree, # return the score of the current node.