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 jake-g
# migrated from python 2.7
def checkio(rows):
cols = list(zip(*rows))
diags = [(rows[0][0] + rows[1][1] + rows[2][2]),
(rows[0][2] + rows[1][1] + rows[2][0]),
'...'] # added dummy row for dimensional agreement in loop
for i in range(len(rows)):
# look for three in a row (not '.' though)
r, c, d = set(rows[i]), set(cols[i]), set(diags[i])
if (len(r) == 1) and ('.' not in r):
return r.pop()
if (len(c) == 1) and ('.' not in c):
return c.pop()
if (len(d) == 1) and ('.' not in d):
return d.pop()
return 'D'
if __name__ == '__main__':
#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"
Jan. 24, 2016
Comments: