Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
groupby() solution in Clear category for Find Sequence by martin.pilka
def has_n_seq(l, n):
# Returns True if list 'l' has a sequence of 'n' or more matching digits, False otherwise
from itertools import groupby
if len(l) > 0:
return max(len(list(g)) for _, g in groupby(l)) >= n
else:
return False
def checkio(matrix):
# Check rows
for row in matrix:
if has_n_seq(row, 4):
return True
# Check columns
for col in zip(*matrix):
if has_n_seq(col, 4):
return True
# Check diagonals
n = len(matrix)
for p in range(2*n-1):
# Based on https://stackoverflow.com/questions/6313308/get-all-the-diagonals-in-a-matrix-list-of-lists-in-python
# Check lower-left-to-upper-right diagonal
if has_n_seq([matrix[p-q][q] for q in range(max(0, p - n + 1), min(p, n - 1) + 1)], 4):
return True
# Check upper-left-to-lower-right diagonal
if has_n_seq([matrix[n-p+q-1][q] for q in range(max(0, p - n + 1), min(p, n - 1) + 1)], 4):
return True
# Nothing found
return False
Jan. 23, 2019
Comments: