Skip to content

Commit 29e9d5c

Browse files
authored
Validate scores for minimax function
Add validation for scores in minimax function.
1 parent f5988cc commit 29e9d5c

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

backtracking/minimax.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ def minimax(
4646
Traceback (most recent call last):
4747
...
4848
ValueError: Scores cannot be empty
49+
>>> minimax(0, 0, True, [1, 2, 3], 1.58)
50+
Traceback (most recent call last):
51+
...
52+
ValueError: Number of scores must be a power of 2
4953
>>> scores = [3, 5, 2, 9, 12, 5, 23, 23]
5054
>>> height = math.log(len(scores), 2)
5155
>>> minimax(0, 0, True, scores, height)
@@ -56,6 +60,8 @@ def minimax(
5660
raise ValueError("Depth cannot be less than 0")
5761
if len(scores) == 0:
5862
raise ValueError("Scores cannot be empty")
63+
if not (len(scores) & (len(scores) - 1) == 0):
64+
raise ValueError("Number of scores must be a power of 2")
5965

6066
# Base case: If the current depth equals the height of the tree,
6167
# return the score of the current node.

0 commit comments

Comments
 (0)