Hello, advancing coder! Are you excited to dive into a new, intriguing challenge? In this lesson, we're going to explore special traversals of matrices. Matrices are rectangular 2D arrays where we traverse through rows and columns. We'll find our way through a matrix by climbing up and down the columns, zigzagging as we go. Sound exciting? Buckle up, then, and get ready!
Here's the task: you've been given a 2D array consisting of individual cells, each holding a unique integer value. Your goal is to create a function that will traverse this matrix, starting at the bottom-right cell. From there, you'll travel up to the top of the same column, then move left to the next column, and then continue downward from the top of this new column. After reaching the bottom of the column, you again move left and start moving upward. This unique traversal pattern will be performed until all the cells have been visited.
Consider this small 3x4 matrix as an example:
With the described traversal pattern, your function should return this list: [12, 8, 4, 3, 7, 11, 10, 6, 2, 1, 5, 9]
.
The first step toward a solution is understanding the dimensions of the matrix that we're working with. We can do this using array properties. Let's set up our function and identify the matrix size:
Now that we're aware of the matrix dimensions, we should establish the starting point (bottom-right) and the direction of travel (upward initially). Additionally, we'll need an array to keep track of the cells we've visited in order:
