Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Find Sequence by Machinero
def vertical(matrix, i, j):
count = 0
if i+3 < len(matrix):
if matrix[i+1][j] == matrix[i][j] and \
matrix[i+2][j] == matrix[i][j] and \
matrix[i+3][j] == matrix[i][j]:
count += 1
return count
def horizontal(matrix, i, j):
count = 0
if j+3 < len(matrix):
if matrix[i][j+1] == matrix[i][j] and \
matrix[i][j+2] == matrix[i][j] and \
matrix[i][j+3] == matrix[i][j]:
count += 1
return count
def diagonal(matrix, i, j):
count = 0
if j+3 < len(matrix) and i-1 > 0 :
if matrix[i-1][j+1] == matrix[i][j] and \
matrix[i-2][j+2] == matrix[i][j] and \
matrix[i-3][j+3] == matrix[i][j]:
count += 1
return count
def diagonal2(matrix, i, j):
count = 0
if j-1 > 0 and i-1 > 0:
if matrix[i-1][j-1] == matrix[i][j] and \
matrix[i-2][j-2] == matrix[i][j] and \
matrix[i-3][j-3] == matrix[i][j]:
count += 1
return count
def checkio(matrix):
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if vertical(matrix, i, j) == 1 or \
horizontal(matrix, i, j) == 1 or\
diagonal(matrix, i, j) == 1 or\
diagonal2(matrix, i, j) == 1:
return True
return False
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio([[2,6,2,2,7,6,5],[3,4,8,7,7,3,6],[6,7,3,1,2,4,1],[2,5,7,6,3,2,2],[3,4,3,2,7,5,6],[8,4,6,5,2,9,7],[5,8,3,1,3,7,8]]) == False
Dec. 5, 2016