Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Find Sequence by Michael_Zilberman
def checkio(matrix):
seqlen = 4
n = len(matrix)
if n < seqlen:
return False
#
# Find columns
#
for i in range(n):
for j in range(n-seqlen+1):
if len(set(matrix[k][i] for k in range(j, j+seqlen))) == 1:
return True
#
# Find rows
#
for i in range(n):
for j in range(n-seqlen+1):
if len(set(matrix[i][k] for k in range(j, j+seqlen))) == 1:
return True
#
# Find NW-SE
#
for i in range(n-seqlen+1):
for j in range(n-seqlen+1):
if len(set(matrix[j+k][i+k] for k in range(0, seqlen))) == 1:
return True
#
# Find NE-SW
#
for i in range(seqlen-1, n):
for j in range(n-seqlen+1):
if len(set(matrix[j+k][i-k] for k in range(0, seqlen))) == 1:
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"
April 4, 2018