Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backtracking/minimax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down