Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using groupby solution in Clear category for Find Sequence by nickie
from itertools import groupby
C = 4
ex = lambda l: any(len(list(c)) >= C for x, c in groupby(l))
def checkio(A):
n = len(A)
return any(ex(row) for row in A) \
or any(ex([A[i][j] for i in range(n)]) for j in range(n)) \
or any(ex([A[k-i][i] for i in range(k+1)]) for k in range(C-1, n)) \
or any(ex([A[n-i-1][n-k+i-1] for i in range(k+1)]) for k in range(C-1, n-1)) \
or any(ex([A[n-k+i-1][i] for i in range(k+1)]) for k in range(C-1, n)) \
or any(ex([A[i][n-k+i-1] for i in range(k+1)]) for k in range(C-1, n-1))
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
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. 17, 2013
Comments: