Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Find Sequence by Lachesis_132296
def horizontally(matrix):
for i in matrix:
if len(i) < 4:
break
if len(i) == 4:
if i[0] == i[1] and i[1] == i[2] and i[2] == i[3]:
return True
else:
break
if len(i) > 4:
for j in range(0, len(i) - 3):
if i[j] == i[j+1] and i[j+1] == i[j+2] and i[j+2] == i[j+3]:
return True
return False
def vertically(matrix):
n = len(matrix[0])
if n < 4:
return False
if n == 4:
for i in range(4):
if matrix[0][i] == matrix[1][i] and matrix[1][i] == matrix[2][i] and matrix[2][i] == matrix[3][i]:
return True
if n > 4:
for i in range(n): #column
for j in range(0, n-3): #row
if matrix[j][i] == matrix[j+1][i] and matrix[j+1][i] == matrix[j+2][i] and matrix[j+2][i] == matrix[j+3][i]:
return True
return False
def diagonally(matrix):
n = len(matrix)
if n < 4:
return False
if n == 4:
if matrix[0][0] == matrix[1][1] and matrix[1][1] == matrix[2][2] and matrix[2][2] == matrix[3][3]:
return True
if matrix[0][3] == matrix[1][2] and matrix[1][2] == matrix[2][1] and matrix[2][1] == matrix[3][0]:
return True
if n > 4:
for i in range(0, n-3):
for j in range(0, n-3):
if matrix[i][j] == matrix[i+1][j+1] and matrix[i+1][j+1] == matrix[i+2][j+2] and matrix[i+2][j+2] == matrix[i+3][j+3]:
return True
for i in range(n-1, 2, -1): #column
for j in range(0, n-3): #row
if matrix[j][i] == matrix[j+1][i-1] and matrix[j+1][i-1] == matrix[j+2][i-2] and matrix[j+2][i-2] == matrix[j+3][i-3]:
return True
return False
def checkio(matrix):
if horizontally(matrix) or vertically(matrix) or diagonally(matrix):
return True
else:
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"
Nov. 19, 2016