Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using numpy and itertools/groupby solution in 3rd party category for Find Sequence by H0r4c3
import numpy as np
from itertools import groupby
from typing import List
def check_consecutives(my_list):
result = any(sum(1 for _ in rep) >= 4 for _, rep in groupby(my_list))
return result
def checkio(matrix: List[List[int]]) -> bool:
matrix_arr = np.array(matrix)
all_lists = list()
# rows
rows = list()
for row in matrix_arr:
rows.append(list(row))
print(f'rows = {rows} \n')
# columns
columns = list()
for i in range(len(matrix[0])):
columns.append(list(matrix_arr[:,i]))
print(f'columns = {columns} \n')
# diagonals
diagonals_1_up = list()
for i in range(len(matrix[0])):
diagonals_1_up.append(list(matrix_arr.diagonal(i, 0, 1)))
print(f'diagonals_1_up = {diagonals_1_up} \n')
diagonals_1_down = list()
for i in range(len(matrix[0])):
diagonals_1_down.append(list(matrix_arr.diagonal(i, 1, 0)))
print(f'diagonals_1_down = {diagonals_1_down} \n')
matrix_fliplr = np.fliplr(matrix_arr)
diagonals_2_up = list()
for i in range(len(matrix[0])):
diagonals_2_up.append(list(matrix_fliplr.diagonal(i, 0, 1)))
print(f'diagonals_2_up = {diagonals_2_up} \n')
diagonals_2_down = list()
for i in range(len(matrix[0])):
diagonals_2_down.append(list(matrix_fliplr.diagonal(i, 1, 0)))
print(f'diagonals_2_down = {diagonals_2_down} \n')
for item in [rows, columns, diagonals_1_up, diagonals_1_down, diagonals_2_up, diagonals_2_down]:
all_lists.extend(item)
print(f'all_lists = {all_lists} \n')
for item in all_lists:
if check_consecutives(item):
return True
else:
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
assert checkio([
[7, 1, 4, 1],
[1, 2, 5, 2],
[3, 4, 1, 3],
[1, 1, 8, 1]
]) == False
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
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
print('All Done! Time to check!')
July 17, 2022
Comments: