Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
One liner solution in Clear category for Zigzag Array by amandel
def create_zigzag(rows: int, cols: int, start: int = 1) -> list[list[int]]:
# First solution
# return [list(range(*[(start+i*cols,start+i*cols+cols),(start+i*cols+cols-1,start+i*cols-1,-1)][i%2])) for i in range(rows)]
return [list(range(start+i*cols+i%2*(cols-1),start+(i+1)*cols-i%2*(cols+1),1-i%2*2)) for i in range(rows)]
print("Example:")
print(create_zigzag(3, 5))
# These "asserts" are used for self-checking
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]]
print("The mission is done! Click 'Check Solution' to earn rewards!")
Jan. 25, 2025
Comments: