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 amandel
from typing import List
def checkio(game_result: List[str]) -> str:
# arithmetize
d = {'O':-1, '.':0, 'X':1}
a = [[d[t] for t in x] for x in game_result]
# generate index pairs
r3 = range(3) # convenience
# Could just hard code pairs, but this should be more understandable
pairs = [[(i,j) for j in r3] for i in r3] + [[(i,j) for i in r3] for j in r3] +[[(i,i) for i in r3]] + [[(i,2-i) for i in r3]]
v =[sum(a[i][j] for i,j in p) for p in pairs]
return "X" if 3 in v else "O" if -3 in v 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", "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!")
March 20, 2021
Comments: