Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Xs and Os Referee by linfei2
from typing import List
def checkio(game_result: List[str]) -> str:
#Checking rows
for r in game_result:
if r == "XXX":
return "X"
elif r == "OOO":
return "O"
#Checking columns
for c in list(zip(*game_result)):
if c == ("X", "X", "X"):
return "X"
elif c == ("O", "O", "O"):
return "O"
#Checking diagonals
if (game_result[0][0] == "X" and game_result[1][1] == "X" and game_result[2][2] == "X"
or game_result[0][2] == "X" and game_result[1][1] == "X" and game_result[2][0]== "X"):
return "X"
elif (game_result[0][0] == "O" and game_result[1][1] == "O" and game_result[2][2] == "O"
or game_result[0][2] == "O" and game_result[1][1] == "O" and game_result[2][0]== "O"):
return "O"
return "D"
if __name__ == '__main__':
print("Example:")
print(checkio(["X.O",
"XX.",
"XOO"]))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio([
"X.O",
"XX.",
"XOO"]) == "X", "Xs wins"
assert checkio([
"OO.",
"XOX",
"XOX"]) == "O", "Os wins"
assert checkio([
"OOX",
"XXO",
"OXX"]) == "D", "Draw"
assert checkio([
"O.X",
"XX.",
"XOO"]) == "X", "Xs wins again"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
Feb. 26, 2021