Xs and OS referee
1 def checkio(game_result): 2 winner = ‘D‘ 3 4 for row in game_result: 5 if row[0] == row[1] == row[2] and row[0] != ‘.‘: 6 winner = row[0] 7 8 for col in range(0, 3): 9 if game_result[0][col] == game_result[1][col] == game_result[2][col] and game_result[0][col] != ‘.‘:10 winner = game_result[0][col]11 12 if game_result[0][0] == game_result[1][1] == game_result[2][2] and game_result[0][0] != ‘.‘:13 winner = game_result[0][0]14 15 if game_result[0][2] == game_result[1][1] == game_result[2][0] and game_result[0][2] != ‘.‘:16 winner = game_result[0][2]17 18 return winner
The conclusion of this question is that python supports the judgment of the same mode: Row [0] = row [1] = row [2], that is, supports connections, etc.
Let's take a look at the great god code.
1 def checkio(result):2 rows = result3 cols = map(‘‘.join, zip(*rows))4 diags = map(‘‘.join, zip(*[(r[i], r[2 - i]) for i, r in enumerate(rows)]))5 lines = rows + list(cols) + list(diags)6 7 return ‘X‘ if (‘XXX‘ in lines) else ‘O‘ if (‘OOO‘ in lines) else ‘D‘
The zip function softens two arrays, such as the student name = ['bob', 'jenny '], grade = [80, 90], zip (name, grade) = [('bob', 80), ('jenny ', 90)]. In function calls, use * List/tuple to separate list/tuple, pass the parameter as a location parameter to the corresponding function (provided that the corresponding function supports an indefinite number of location parameters), such as test = ["XXX", "ooo ","... "], zip (* test) = [('x', 'O ','. '), ('x', 'O ','. '), ('x', 'O ','. ')]
The map function accepts two parameters. The first parameter is the function name, and the second parameter is an iterative object, for example, array = [1, 2, 3], map (STR, array) = ['1', '2', '3'], that is, the first function is applied to the second object.