Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (47.6k points)

I'm using Python's max and min functions on lists for a minimax algorithm, and I need the index of the value returned by max() or min(). In other words, I need to know which move produced the max (at a first player's turn) or min (second player) value.

for i in range(9):

newBoard = currentBoard.newBoardWithMove([i / 3, i % 3], player)

if newBoard:

temp = minMax(newBoard, depth + 1, not isMinLevel) values.append(temp)

if isMinLevel:

return min(values)

else:

return max(values)

I need to be able to return the actual index of the min or max value, not just the value.

1 Answer

0 votes
by (106k points)

You can use the following piece of code for getting the index of the returned max or min item using max()/min() on a list:-

if isMinLevel:

    return values.index(min(values))

else:

    return values.index(max(values))

Related questions

0 votes
1 answer
0 votes
1 answer
asked Sep 26, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Aug 1, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Oct 14, 2019 in Python by Sammy (47.6k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...