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 Trailblazer
from typing import List
def checkio(game_result: List[str]) -> str:
"""
This algorithm can be easily adopted for grid of any size
by replacing integers with variable dependant on len(game_result[0]).
"""
# Convert list of strings to a plain string.
s = ''.join(game_result)
for i in range(3):
# Check columns.
if s[i] != '.' and s[i::3].count(s[i]) == 3:
return s[i]
# Check rows.
i *= 3
if s[i] != '.' and s[i:(i + 3)].count(s[i]) == 3:
return s[i]
# Check diagonals.
# s[4] - central element, common for both diagonals.
# For grids with even size every diagonal should have it's own if statement.
if s[4] != '.' and (s[::4].count(s[4]) == 3 or s[2:7:2].count(s[4]) == 3):
return s[4]
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", "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!")
June 24, 2019
Comments: