Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
use matrix diagonal and horizontal flip solution in Clear category for Find Sequence by kdim
from typing import List
import copy
def diag(matrix): # matrix diagonal flip
m = copy.deepcopy(matrix) # [[1,2,3], [[1,0,1],
for y in range(len(m)): # [0,0,0], ----> [2,0,1],
for x in range(y+1): # [1,1,1]] [3,0,1]]
m[y][x], m[x][y] = m[x][y], m[y][x] #
return m
def horiz(matrix): # matrix horizontal flip
m = copy.deepcopy(matrix) # [[1,2,3], [[1,1,1],
for y in range(len(m)//2): # [0,0,0], ----> [0,0,0],
for x in range(len(m)): # [1,1,1]] [1,2,3]]
m[y][x], m[len(m)-1-y][x] = m[len(m)-1-y][x], m[y][x]
return m
def diag4(matrix): # get matrix diagonal element len of > 4
m = copy.deepcopy(matrix) # [[1,2,3,.,.,.],
a= [] # [1,2,3,4,.,.],
for i in range(len(m)): # [1,2,3,4,5,.],
b = [ m[i+j][j] for j in range(len(m)-i)] # [.,2,3,4,5,6],
c = [ m[j][i+j] for j in range(len(m)-i)] # [.,.,3,4,5,6],
if len(b)>3: a.append(b) # [.,.,.,4,5,6]]
if len(c)>3: a.append(c)
return a
def checkio(matrix: List[List[int]]) -> bool:
m = matrix
n = m + diag(m) + diag4(m) + diag4(horiz(m)) # all combination
for i in range(1,10): # find a sequence
if any([str(i)*4 in "".join(list(map(str,j))) for j in n]):
return True
return False
Jan. 11, 2021
Comments: