Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-223

Tic-Tac-Toe Winner

Problem

Given a 3 by 3 tic-tac-toe board of X, O and space characters, return the winning symbol, or the empty string when there is no winner.

Check all three rows, three columns and two diagonals for three equal non-space marks. Assume the position is valid with at most one winner.

Hint

Only non-empty lines can win, so check the symbol of the first cell.

Test cases

case 1
in: [[["X","X","X"],[" "," "," "],["O"," ","O"]]]
expected: "X"
case 2
in: [[["O","X","X"],["X","O","X"],[" "," ","O"]]]
expected: "O"
case 3
in: [[["X","O","X"],["X","O","O"],["O","X","X"]]]
expected: ""
case 4
in: [[[" "," "," "],[" "," "," "],[" "," "," "]]]
expected: ""
case 5
in: [[["X","X","O"],["X","O","X"],["O","X","X"]]]
expected: "O"
Starter codePython
def tic_tac_toe_winner(board):
    # Your code here
    pass

5 test cases · run your code against them in the editor