Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Numpy solution in 3rd party category for Matrix "Hatching" by U.V
import numpy as np
def hatching(matrix):
m = np.array(matrix)[::-1, ::]
M, N = m.shape
for i in range(-M+1, N):
yield list(m.diagonal(i))
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!")
Jan. 11, 2023