Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
numpy attempt solution in 3rd party category for Zigzag Array by HeNeArKr
# Just playing with numpy. I feel like there is a more efficient way to do this,
# but have not been able to find it yet.
from typing import List
import numpy as np
def create_zigzag(rows: int, cols: int, start: int = 1) -> List[List[int]]:
""" Return list of lists of ints, starting with start, increasing by one,
with odd-numbered rows increasing from right to left.
"""
if rows == 0:
return []
filter_e = np.array([[i % 2 == 0] for i in range(rows)])
filter_o = np.invert(filter_e)
result_e = np.arange(start, start+rows*cols).reshape(rows, cols)
return (result_e * filter_e + np.fliplr(result_e) * filter_o).tolist()
Oct. 15, 2018
Comments: