Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
'Improved' Nickie's solution + Commenting solution in 3rd party category for Find Sequence by shasa.gimblett
from itertools import groupby
from numpy import diagonal, array, fliplr
C = 4
ex = lambda l: any(len(list(c)) >= C for x, c in groupby(l))
"""
This expression returns true if there is a sequence of 4+ in the 'line' (row,col,diag)
list(c) in groupby(l) returns a list of consecutive elements in the 'line'.
Directly from groupby docs...
[list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
any() simply returns TRUE is any of the expressions inside it evaluate to be TRUE, FALSE otherwise
"""
def checkio(A):
"""
I used another users solution (Nickie) to create this solution that I thought
was a little clearer.
In each sub expression in the return expression the grid is searched for a sequence
in the rows, cols, '/' diangonal and then the '\' diagonal.
The searching in the rows an columns is quite simple. list(*zip(A)) simply transposes the matrix.
For the diagonals I reccomend inspecting the documentation for numpy.diagonal
as there is a lot to explain.
"""
n = len(A)
# Search rows, cols, diagonal '\', diagonal '/'
return any(ex(row) for row in A) \
or any(ex(col) for col in list(zip(*A))) \
or any(ex(array(A).diagonal(diag).tolist()) for diag in range(-(n-4),n-3)) \
or any(ex(fliplr(A).diagonal(diag).tolist()) for diag in range(-(n-4),n-3))
Nov. 14, 2016
Comments: