From 916af6ec1f38c772402361221206d6148660adb4 Mon Sep 17 00:00:00 2001 From: Abdujabbar Mirkhalikov Date: Fri, 25 Sep 2020 13:49:06 +0500 Subject: [PATCH 1/3] added type hints and doctests for minimax algorithm --- backtracking/minimax.py | 61 +++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/backtracking/minimax.py b/backtracking/minimax.py index 4cec0e403ddf..531d04362200 100644 --- a/backtracking/minimax.py +++ b/backtracking/minimax.py @@ -1,4 +1,7 @@ +from __future__ import annotations import math +from typing import List + """ Minimax helps to achieve maximum score in a game by checking all possible moves depth is current depth in game tree. @@ -9,26 +12,62 @@ """ -def minimax(Depth, nodeIndex, isMax, scores, height): +def minimax(depth: int, node_index: int, is_max: bool, + scores: List[int], height: float) -> int: + """ + >>> import math + >>> scores = [90, 23, 6, 33, 21, 65, 123, 34423] + >>> height = math.log(len(scores), 2) + >>> minimax(0, 0, True, scores, height) + 65 + >>> minimax(-1, 0, True, scores, height) + Traceback (most recent call last): + ... + ValueError: Depth cannot be less than 0 + >>> minimax(0, 0, True, [], 2) + Traceback (most recent call last): + ... + ValueError: Scores cannot be empty + >>> scores = [3, 5, 2, 9, 12, 5, 23, 23] + >>> height = math.log(len(scores), 2) + >>> minimax(0, 0, True, scores, height) + 12 + >>> minimax('1', 2, True, [], 2 ) + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' + """ + + if depth < 0: + raise ValueError("Depth cannot be less than 0") + + if len(scores) == 0: + raise ValueError("Scores cannot be empty") - if Depth == height: - return scores[nodeIndex] + if depth == height: + return scores[node_index] - if isMax: + if is_max: return max( - minimax(Depth + 1, nodeIndex * 2, False, scores, height), - minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height), + minimax(depth + 1, node_index * 2, False, scores, height), + minimax(depth + 1, node_index * 2 + 1, False, scores, height), ) + return min( - minimax(Depth + 1, nodeIndex * 2, True, scores, height), - minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height), + minimax(depth + 1, node_index * 2, True, scores, height), + minimax(depth + 1, node_index * 2 + 1, True, scores, height), ) -if __name__ == "__main__": - +def main(): scores = [90, 23, 6, 33, 21, 65, 123, 34423] height = math.log(len(scores), 2) - print("Optimal value : ", end="") print(minimax(0, 0, True, scores, height)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + main() From d257d9b9d448823d3dd9cf1dfeeb3e0b3becd5df Mon Sep 17 00:00:00 2001 From: Abdujabbar Mirkhalikov Date: Fri, 25 Sep 2020 17:33:22 +0500 Subject: [PATCH 2/3] Update backtracking/minimax.py Co-authored-by: Christian Clauss --- backtracking/minimax.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backtracking/minimax.py b/backtracking/minimax.py index 531d04362200..34e5a2c96187 100644 --- a/backtracking/minimax.py +++ b/backtracking/minimax.py @@ -1,7 +1,6 @@ from __future__ import annotations -import math -from typing import List +import math """ Minimax helps to achieve maximum score in a game by checking all possible moves depth is current depth in game tree. From af2b2ed9d5681adbf677f582027af46503fab577 Mon Sep 17 00:00:00 2001 From: Abdujabbar Mirkhalikov Date: Fri, 25 Sep 2020 17:35:06 +0500 Subject: [PATCH 3/3] last fix --- backtracking/minimax.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backtracking/minimax.py b/backtracking/minimax.py index 34e5a2c96187..056447256395 100644 --- a/backtracking/minimax.py +++ b/backtracking/minimax.py @@ -1,5 +1,4 @@ from __future__ import annotations - import math """ Minimax helps to achieve maximum score in a game by checking all possible moves @@ -12,7 +11,7 @@ def minimax(depth: int, node_index: int, is_max: bool, - scores: List[int], height: float) -> int: + scores: list[int], height: float) -> int: """ >>> import math >>> scores = [90, 23, 6, 33, 21, 65, 123, 34423]