Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second: improved readability while maintaining correct handling of special cases solution in Clear category for Xs and Os Referee by piter239
from typing import List
def checkio(field: List[str]) -> str:
rows = field
cols = map(''.join, zip(*rows))
diags = map(''.join, zip(*[(r[i], r[2 - i]) for i, r in enumerate(rows)]))
lines = rows + list(cols) + list(diags)
score = {sym: lines.count(sym * 3) for sym in 'XO'}
# now we can correctly handle situations where both sides have scored
return "DXO"[bool(score['X']) - bool(score['O'])]
if __name__ == '__main__':
print("Example:")
print(checkio(["X.O",
"XX.",
"XOO"]))
assert checkio([
"OOO",
"XXX",
"OX."]) == "D", "Draw"
assert checkio([
"XOO",
"XOO",
"XOO"]) == "D", "Draw again"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
April 28, 2020
Comments: