Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
find all lines and use bultin.all solution in Clear category for Xs and Os Referee by hbczmxy
from typing import List
def checkio(game_result: List[str]) -> str:
horizontals = []
for line in game_result:
horizontal = [c for c in line]
horizontals.append(horizontal)
verticals = []
for i in range(3):
vertical = [game_result[j][i] for j in range(3)]
verticals.append(vertical)
diagonals = []
diagonals.append( [game_result[i][i] for i in range(3)] )
diagonals.append( [game_result[i][j] for i, j in zip(range(3), range(2,-1,-1))] )
all_lines = horizontals + verticals + diagonals
for line in all_lines:
if all(c == 'X' for c in line):
return 'X'
elif all(c == 'O' for c in line):
return 'O'
else:
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", "X wins"
assert checkio(["OO.", "XOX", "XOX"]) == "O", "O wins"
assert checkio(["OOX", "XXO", "OXX"]) == "D", "Draw"
assert checkio(["O.X", "XX.", "XOO"]) == "X", "X wins again"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
Oct. 22, 2021
Comments: