Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Find Sequence by delahere
def rotate90(mat):
rows = cols = len(mat[0])
#
# Make a copy of the matrix provided, rotated 90 degrees clockwise. The input matrix
# must be 2D and num rows must equal num columns.
#
# Create an initial matrix with with an initial value. Choose a value outside
# of the range of values in the parameter matrix, for debug purposes. If the
# value shows up in the final result, then we know we hava a problem.
#
rotated_matrix = [[11] * cols for _ in range(rows)]
row_width = len(mat[0])
max_inx = row_width - 1
for row in range(row_width):
for col in range(row_width):
rotated_matrix[col][max_inx - row] = mat[row][col]
return rotated_matrix
def search_horizontal(mat):
#
# Search each row of the matrix, looking for a sequence of the same value
# for four or more cells. Return True if a match is found.
#
max_inx = len(mat[0]) - 1
for val in range(10):
sequence_length = 0
for row in mat:
for cell in row:
if cell == val:
sequence_length += 1
else:
sequence_length = 0
if sequence_length >= 4:
return True
#
# If we get here, we haven't found a sequence.
#
return False
def search_diagonal(mat):
#
# Search for values along the descending diagonal for a sequence of four
# or more of the same value. Note that the matrix must be 2D and num rows
# must equal the number of columns.
#
# First, create a flattened 1D copy of the matrix. Check every cells that
# are row length + 1 apart.
#
# e.g for a 4x4 matrix, cells at indexes 0,5,10,15 in the flattened area
# are on the diagonal.
#
num_row_cells = num_rows = num_columns = len(mat)
flattened = []
for i in range(num_rows):
for j in range(num_columns):
flattened.append((mat[i][j]))
interval = num_row_cells + 1
for candidate in range(10):
sequence_length = 0
i = 0
while i < len(flattened):
if flattened[i] == candidate:
sequence_length += 1
i += interval
else:
#
# If we were working on a sequence and it was broken, circle
# back to check the next value after the previous match.
#
if sequence_length:
i = i - interval + 1
else:
i += 1
sequence_length = 0
if sequence_length >= 4:
return True
#
# If we get here, we haven't found a sequence.
#
return False
def checkio(matrix: list[list[int]]) -> bool:
# replace this for solution
#
# First, create a rotated copy of the matrix so that the same
# code needed to search horizontally can be used on the vertical axis. Also,
# the same code used to search the descending diagonal can be used to search
# the ascending diagonal.
#
rot_matrix = rotate90(matrix)
if search_horizontal(matrix):
return True
if search_horizontal(rot_matrix):
return True
if search_diagonal(matrix):
return True
if search_diagonal(rot_matrix):
return True
return False
print("Example:")
print(checkio([[1, 2, 1, 1], [1, 1, 4, 1], [1, 3, 1, 6], [1, 7, 2, 5]]))
# These "asserts" are used for self-checking
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
)
assert (
checkio(
[
[2, 6, 2, 2, 7, 6, 5],
[3, 4, 8, 7, 7, 3, 6],
[6, 7, 3, 1, 2, 4, 1],
[2, 5, 7, 6, 3, 2, 2],
[3, 4, 3, 2, 7, 5, 6],
[8, 4, 6, 5, 2, 9, 7],
[5, 8, 3, 1, 3, 7, 8],
]
)
== False
)
assert (
checkio(
[
[1, 7, 6, 1, 8, 5, 1],
[7, 9, 1, 7, 2, 8, 6],
[5, 1, 4, 5, 8, 8, 3],
[8, 6, 3, 9, 7, 6, 9],
[9, 8, 9, 8, 6, 8, 2],
[1, 7, 2, 4, 9, 3, 8],
[9, 9, 8, 6, 9, 2, 6],
]
)
== False
)
assert (
checkio(
[
[6, 9, 1, 1, 6, 2],
[5, 9, 7, 8, 2, 5],
[2, 1, 1, 7, 9, 8],
[1, 8, 1, 4, 7, 4],
[7, 8, 5, 4, 5, 1],
[6, 4, 8, 8, 1, 8],
]
)
== False
)
assert (
checkio(
[
[2, 7, 6, 2, 1, 5, 2, 8, 4, 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3, 4, 1, 8, 8],
[5, 3, 3, 2, 6, 1, 4, 3, 8, 8],
[4, 8, 1, 4, 5, 8, 8, 7, 4, 7],
]
)
== True
)
assert (
checkio(
[
[7, 7, 4, 4, 8],
[7, 4, 5, 5, 6],
[6, 6, 5, 2, 8],
[6, 2, 3, 8, 4],
[6, 1, 3, 1, 2],
]
)
== False
)
assert (
checkio(
[
[7, 9, 1, 7, 6, 7, 5, 9, 6],
[5, 5, 9, 3, 1, 6, 7, 4, 7],
[1, 7, 5, 2, 3, 1, 6, 4, 7],
[2, 2, 2, 8, 7, 2, 6, 6, 9],
[5, 6, 4, 2, 6, 7, 3, 4, 7],
[5, 5, 6, 4, 9, 4, 3, 1, 7],
[7, 3, 2, 3, 2, 4, 4, 7, 3],
[3, 6, 9, 7, 2, 5, 6, 2, 5],
[4, 1, 3, 9, 4, 2, 4, 8, 4],
]
)
== True
)
print("The mission is done! Click 'Check Solution' to earn rewards!")
April 29, 2023
Comments: