Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
numpy solution in 3rd party category for Zigzag Array by danersow
from typing import List
import numpy as np
def create_zigzag(rows: int, cols: int, start: int = 1) -> List[List[int]]:
lst = np.arange((start or 0),(start or 0)+rows*cols).reshape(rows,cols)
for i in range(rows):
if i % 2 != 0:
lst[i] = np.flip(lst[i])
return lst.tolist()
if __name__ == '__main__':
print("Example:")
print(list(create_zigzag(3, 3, 5)))
# These "asserts" are used for self-checking and not for an auto-testing
assert create_zigzag(3, 5) == [
[1,2,3,4,5],
[10,9,8,7,6],
[11,12,13,14,15]
]
assert create_zigzag(5, 1) == [
[1],
[2],
[3],
[4],
[5]
]
assert create_zigzag(3, 3, 5) == [
[5, 6, 7],
[10, 9, 8],
[11, 12, 13]
]
# Edge cases
assert create_zigzag(0, 3) == []
assert create_zigzag(3, 0) == [[], [], []]
assert create_zigzag(0, 0) == []
print("Coding complete? Click 'Check' to earn cool rewards!")
Oct. 19, 2020
Comments: