Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
matrix solution in Clear category for Find Sequence by Adrian_W
def checkio(matrix):
n=len(matrix)
last=0
series=0
for i in range(0, n):
series=0
for j in range(0, n):
if j==0:
last=matrix[i][j]
series=1
elif matrix[i][j]!=last:
series=1
last=matrix[i][j]
else:
series+=1
if series>=4:
return True
for i in range(0, n):
series=0
for j in range(0, n):
if j==0:
last=matrix[j][i]
series=1
elif matrix[j][i]!=last:
series=1
last=matrix[j][i]
else:
series+=1
if series>=4:
return True
for i in range(0, n):
series=0
for j in range(0, n):
helper=0
#case toright
while i+helper=4:
return True
#toleft
helper=0
series=0
while i-helper=0:
if helper==0:
last=matrix[i-helper][j+helper]
series=1
elif matrix[i-helper][j+helper]!=last:
series=1
last=matrix[i-helper][j+helper]
else:
series+=1
helper+=-1
if series>=4:
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"
Oct. 13, 2016