Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
4 lines 230 symbols solution in Clear category for Xs and Os Referee by CDG.Axel
from typing import List
# short solution
def checkio(game_result: List[str]) -> str:
# coolect list of lines and columns
data = list(map(tuple, game_result)) + list(zip(*game_result))
# add two diagonals at ones
data += zip(*[(el[idx], el[2-idx]) for idx, el in enumerate(game_result)])
# count of X and O wins
cnt = {i: data.count((i, i, i)) for i in 'XO'}
return "DXO"[bool(cnt['X']) - bool(cnt['O'])]
"""
# long solution but 5 long lines
def checkio(game_result: List[str]) -> str:
# collect lines where all symbols is the same
hor = [i[0] for i in game_result if len(set(i)) == 1 and i[0] != '.']
# collect columns where all symbols is the same
ver = [i[0] for i in zip(*game_result) if len(set(i)) == 1 and i[0] != '.']
# make array of diagonal
dia = [(j[i], j[2-i]) for i, j in enumerate(game_result)]
# collect diagonals where all symbols is the same
dia = [i[0] for i in zip(*dia) if len(set(i)) == 1 and i[0] != '.']
# we have only one winner in hor, ver or dia else is draw
return hor[0] if hor else ver[0] if ver else dia[0] if dia else "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!")
Sept. 6, 2021
Comments: