Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Nice numpy solution checking properly rows, columns, diagonal and anti-diagonal solution in 3rd party category for Xs and Os Referee by bsquare
import numpy as np
def checkio(game_result):
data = np.array([d for item in game_result for d in item]).reshape(3, 3)
all_same_mark = lambda arr: np.where(arr)[0].size > 0
for mark in ('X', 'O'):
if any(all_same_mark(to_check) for to_check in
[(data == mark).all(axis=0), \
(data == mark).all(axis=1), \
(data.diagonal() == mark).all(axis=0), \
(np.fliplr(data).diagonal() == mark).all(axis=0)]):
return mark
return 'D'
if __name__ == '__main__':
assert checkio([
"O.X",
"XX.",
"XOO"]) == "X", "Xs wins again"
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio([
"X.O",
"XX.",
"XOO"]) == "X", "Xs wins"
exit(0)
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!")
Aug. 29, 2019
Comments: