Skip to content

added type hints for backtracking/minimax.py #2313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 57 commits into from
Closed
Changes from 4 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
3b12438
added type hints for backtracking/minimax.py
Abdujabbar Aug 15, 2020
9282106
added some simple test cases
Abdujabbar Aug 26, 2020
3616821
flake fix
Abdujabbar Aug 26, 2020
862545f
fixes doctests
Abdujabbar Aug 26, 2020
332c519
Merge branch 'master' of https://github.com/TheAlgorithms/Python
Abdujabbar Aug 30, 2020
80daa57
Fix bugs and add related tests (#2375)
Littlecowherd Aug 31, 2020
e92e433
Update CONTRIBUTING.md (#2378)
muskanvk Aug 31, 2020
a1d1a44
added idf-smooth (#2174)
ShubhamShaswat Sep 2, 2020
c38dec0
capitalize (#2389)
mohammadreza490 Sep 3, 2020
1385e47
Create hexadecimal_to_decimal (#2393)
mohammadreza490 Sep 4, 2020
3b1c4f7
changed a typo (#2396)
NEERAJAP2001 Sep 5, 2020
c0dcc55
Update triplet_sum.py (#2404)
NEERAJAP2001 Sep 6, 2020
25946e4
Update scoring_functions.py (#2291)
tanujdhiman Sep 9, 2020
4d0a8f2
Optimized recursive_bubble_sort (#2410)
realDuYuanChao Sep 10, 2020
696cd47
octal_to_decimal converter (#2399)
mohammadreza490 Sep 10, 2020
1b3fec3
binary_to_decimal converter (#2400)
mohammadreza490 Sep 11, 2020
a191f89
Fix Non Recursive Depth First Search (#2207)
marcoscannabrava Sep 11, 2020
c676956
lxmlCovidDataFetch (#2416)
santoshrajkumar Sep 11, 2020
2e790ce
file-transfer: writing tests and ensuring that all is going well (#2413)
meysam81 Sep 11, 2020
f754c0d
Jump search (#2415)
grochedix Sep 12, 2020
20e98fc
Fix some warnings from LGTM (#2420)
Hasenn Sep 13, 2020
d6bff5c
Renamed files and fixed Doctest (#2421)
realDuYuanChao Sep 13, 2020
44b8cb0
Updated Stack (#2414)
realDuYuanChao Sep 13, 2020
4e5b730
recaptchaVerification (#2417)
santoshrajkumar Sep 13, 2020
799fde4
Update linear_search.py (#2422)
Ashley-J-George Sep 14, 2020
10aa214
Docstrings and formatting improvements (#2418)
Hasenn Sep 14, 2020
cbbc43b
Updated problem_04 in project_euler (#2427)
realDuYuanChao Sep 14, 2020
1ac75f4
Create priority_queue_using_list.py (#2435)
Ashley-J-George Sep 16, 2020
86fb299
Corrected filename and include static types (#2440)
Sep 17, 2020
2de2267
Updated problem_06 in Project Euler (#2439)
realDuYuanChao Sep 17, 2020
0dea049
Added static type checking to polynom-for-points.py towards issue #21…
avych Sep 18, 2020
dc415ec
Added double linear search recursion (#2445)
realDuYuanChao Sep 18, 2020
ecac7b0
Contains loops.py add (#2442)
kanthuc Sep 18, 2020
363858e
hyphen_files = [file for file in filepaths if "-" in file] (#2447)
cclauss Sep 19, 2020
697495b
Fix copy / paste oversight (#2448)
cclauss Sep 19, 2020
b22596c
bin_to_octal (#2431)
mohammadreza490 Sep 19, 2020
b05081a
Update and rename bin_to_octal.py to binary_to_octal.py (#2449)
cclauss Sep 19, 2020
9b73884
Added a function that checks if given string can be rearranged to for…
susmith98 Sep 19, 2020
ea0759d
Create problem_54 in project Euler (#2451)
dhruvmanila Sep 20, 2020
718be54
Update sol1.py (#2455)
atarax665 Sep 20, 2020
a1ea76b
Optimization problem_10 in project_euler (#2453)
realDuYuanChao Sep 22, 2020
6e6a49d
Config Travis CI for two jobs (#2463)
dhruvmanila Sep 23, 2020
9200a2e
from __future__ import annotations (#2464)
cclauss Sep 23, 2020
de3b68d
added main method for calling on lunch
Abdujabbar Sep 23, 2020
5f9be0a
Add Python type hints and doctests to other/two_sum.py (#2467)
spamegg1 Sep 23, 2020
4a3b8d6
Added binary_xor_operator.py and binary_and_operator.py (#2433)
Sep 24, 2020
902fe1c
Fixed reverse words algorithm (#2469)
realDuYuanChao Sep 24, 2020
3a275ca
Fixed remove duplicate (#2470)
realDuYuanChao Sep 24, 2020
08eb1ef
Add solution() for problem 54 of Project Euler (#2472)
dhruvmanila Sep 24, 2020
d3c3f90
added type hints for backtracking/minimax.py
Abdujabbar Aug 15, 2020
584d1da
added some simple test cases
Abdujabbar Aug 26, 2020
8e3c12f
flake fix
Abdujabbar Aug 26, 2020
bb84f97
fixes doctests
Abdujabbar Aug 26, 2020
549c798
added main method for calling on lunch
Abdujabbar Sep 23, 2020
7d1724a
Merge branch 'master' of github.com:Abdujabbor/Python
Abdujabbar Sep 24, 2020
b006adf
prepare for python 3.9
Abdujabbar Sep 24, 2020
1338305
fix imports
Abdujabbar Sep 24, 2020
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
60 changes: 44 additions & 16 deletions backtracking/minimax.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import math
from typing import List
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase with master to pick up the #2464 changes.


""" Minimax helps to achieve maximum score in a game by checking all possible moves
depth is current depth in game tree.
Expand All @@ -9,26 +9,54 @@
"""


def minimax(Depth, nodeIndex, isMax, scores, height):

if Depth == height:
return scores[nodeIndex]

if isMax:
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[node_index]

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__":
import doctest

scores = [90, 23, 6, 33, 21, 65, 123, 34423]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These five lines show the user something when they run the program. Removing them means that the program shows the user nothing when run.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cclauss thank you for your feedback, I have added these lines as method main, look at it if is it right way or not?

height = math.log(len(scores), 2)

print("Optimal value : ", end="")
print(minimax(0, 0, True, scores, height))
doctest.testmod()