Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
all checker solution in Clear category for Find Sequence by akaka
def checkio(matrix):
for r_idx, row in enumerate(matrix):
for c_idx, val in enumerate(row):
#vertically check
if (len(matrix) - r_idx) > 3:
if (matrix[r_idx][c_idx] == matrix[r_idx+1][c_idx] and
matrix[r_idx][c_idx] == matrix[r_idx+2][c_idx] and
matrix[r_idx][c_idx] == matrix[r_idx+3][c_idx]):
return True
#horizontally check
if (len(row) - c_idx) > 3:
if all(x == row[c_idx] for x in row[c_idx:c_idx+4]):
return True
#diagonally check
if (len(row) - c_idx) > 3 and (len(matrix) - r_idx) > 3:
if (matrix[r_idx][c_idx] == matrix[r_idx+1][c_idx+1] and
matrix[r_idx][c_idx] == matrix[r_idx+2][c_idx+2] and
matrix[r_idx][c_idx] == matrix[r_idx+3][c_idx+3]):
return True
elif c_idx > 2 and (len(matrix) - r_idx) > 3:
if (matrix[r_idx][c_idx] == matrix[r_idx+1][c_idx-1] and
matrix[r_idx][c_idx] == matrix[r_idx+2][c_idx-2] and
matrix[r_idx][c_idx] == matrix[r_idx+3][c_idx-3]):
return True
return False
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio([
[1, 2, 1, 1],
[1, 1, 4, 1],
[1, 3, 1, 6],
[1, 7, 2, 5]
]) == True, "Vertical"
assert checkio([
[7, 1, 4, 1],
[1, 2, 5, 2],
[3, 4, 1, 3],
[1, 1, 8, 1]
]) == False, "Nothing here"
assert checkio([
[2, 1, 1, 6, 1],
[1, 3, 2, 1, 1],
[4, 1, 1, 3, 1],
[5, 5, 5, 5, 5],
[1, 1, 3, 1, 1]
]) == True, "Long Horizontal"
assert checkio([
[7, 1, 1, 8, 1, 1],
[1, 1, 7, 3, 1, 5],
[2, 3, 1, 2, 5, 1],
[1, 1, 1, 5, 1, 4],
[4, 6, 5, 1, 3, 1],
[1, 1, 9, 1, 2, 1]
]) == True, "Diagonal"
Aug. 1, 2018