Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Easy and clear soultion solution in Clear category for Xs and Os Referee by piotr.sawicki
def checkio(game_result):
list_global = []
for n in game_result:
list_local = []
for i in n:
for k in i.lower():
if k == "x": list_local.append(-1)
if k == ".": list_local.append(0)
if k == "o": list_local.append(1)
list_global.append(list_local)
# part of the code below check what is the result for the diagonals.
# if the results for the diagonals is positive (there is 3 the same signs)
# then there is no need to check results for horizontal and vertical lines
# ================>> START <<================ for checking the results for the diagonals
sum_diagonally_1 = 0
sum_diagonally_2 = 0
for i in range(len(list_global)):
sum_diagonally_1 += list_global[i][i]
for i in range(len(list_global)):
sum_diagonally_2 += list_global[len(list_global) - i - 1][i]
if sum_diagonally_1 == -3 or sum_diagonally_2 == -3: return "X"
if sum_diagonally_1 == 3 or sum_diagonally_2 == 3: return "O"
# ================>> END <<================ for checking the results for the diagonals
# if the result for diagonals are not satisfactory then let`s check results for horizontal and vertical lines
# ===========HORIZONTAL=========== let`s make a list of results for the horizontal lines and check the results:
result_horizontally = []
for i in list_global:
result_horizontally.append(sum(i))
if min(result_horizontally) == -3: return "X"
if max(result_horizontally) == 3: return "O"
# ===========VERTICAL============= let`s make a list of results for the vertical lines and check the results:
result_vertically = []
for n in range(len(list_global)):
sum_vertically = 0
for i in range(len(list_global)):
sum_vertically += list_global[i][n]
result_vertically.append(sum_vertically)
if min(result_vertically) == -3: return "X"
if max(result_vertically) == 3: return "O"
return "D"
Oct. 22, 2017