Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
list comprehension solution in Clear category for Matrix "Hatching" by Sim0000
from typing import Iterable
def hatching(matrix: list[list[int]]) -> Iterable[list[int]]:
R, C = len(matrix), len(matrix[0])
for n in range(R + C - 1):
yield [matrix[n - c][c] for c in range(max(0, n - R + 1), min(n + 1, C))]
print("Example:")
print(list(hatching([[1, 2], [3, 4]])))
# These "asserts" are used for self-checking
assert list(hatching([[0]])) == [[0]]
assert list(hatching([[1, 2], [3, 4]])) == [[1], [3, 2], [4]]
assert list(hatching([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]])) == [
[1],
[6, 2],
[7, 3],
[8, 4],
[9, 5],
[0],
]
assert list(hatching([[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]])) == [
[1],
[3, 2],
[5, 4],
[7, 6],
[9, 8],
[0],
]
print("The mission is done! Click 'Check Solution' to earn rewards!")
Nov. 30, 2022
Comments: