Hello, fellow coder! Are you excited to dive into a new, intriguing coding challenge? In this lesson, we're going to explore special traversals of matrices. Using the Ruby programming language, 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 matrix 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, move left to the next column, and continue downwards from the top of this new column. After reaching the bottom of the column, you again move left and start moving upwards. This unique traversal pattern will be performed until all the cells have been visited.
Consider this small 3 × 4 matrix as an example:
Ruby1matrix = [ 2 [1, 2, 3, 4], 3 [5, 6, 7, 8], 4 [9, 10, 11, 12] 5]
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 towards a solution is understanding the dimensions of the matrix with which we're working. We can do this using Ruby's size
method. Let's set up our function and identify the matrix size:
Ruby1def column_traverse(matrix) 2 rows = matrix.size 3 cols = matrix[0].size 4end
Now that we're aware of the matrix dimensions, we should establish the starting point (bottom-right) and the direction of travel (upwards initially). Additionally, we'll need an array to keep track of the cells we've visited in order:
Ruby1def column_traverse(matrix) 2 rows = matrix.size 3 cols = matrix[0].size 4 direction = 'up' 5 row = rows - 1 6 col = cols - 1 7 output = [] 8end
It's time to go exploring! We'll now implement a while
loop to traverse the matrix. This loop will continue until we have covered all the cells in the matrix. As we "visit" each cell, we'll add the value in the cell to our array.
Ruby1def column_traverse(matrix) 2 rows = matrix.size 3 cols = matrix[0].size 4 direction = 'up' 5 row = rows - 1 6 col = cols - 1 7 output = [] 8 9 while output.size < rows * cols 10 output << matrix[row][col] 11 12 if direction == 'up' 13 if row - 1 < 0 14 direction = 'down' 15 col -= 1 16 else 17 row -= 1 18 end 19 else 20 if row + 1 == rows 21 direction = 'up' 22 col -= 1 23 else 24 row += 1 25 end 26 end 27 end 28 29 output 30end
- If
direction == 'up'
:- Move up within the same column (
row -= 1
) unless the top of the column is reached (row - 1 < 0
). - Switch to 'down' and move left to the next column (
col -= 1
) if the top of the column is reached.
- Move up within the same column (
- If
direction == 'down'
:- Move down within the same column (
row += 1
) unless the bottom of the column is reached (row + 1 == rows
). - Switch to 'up' and move left to the next column (
col -= 1
) if the bottom of the column is reached.
- Move down within the same column (
- End the loop when all cells are visited.
- The loop continues until
output.size
matches the total number of cells (rows * cols
).
That's it, we've completed the function! This Ruby function will return the output
array, which gives us the order of traversal through the matrix.
Let's explore one more way of traversal. We can leverage the utility of Ruby's range to traverse a 2D matrix in reverse order. This can be accomplished by using a range with a step of -1
.
Consider our familiar 3 × 4 matrix:
Ruby1matrix = [ 2 [1, 2, 3, 4], 3 [5, 6, 7, 8], 4 [9, 10, 11, 12] 5]
Using a decrementing range, the reverse traversal pattern would produce this list: [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
.
Here's how we can implement this reverse traversal:
Ruby1def reverse_traverse(matrix) 2 rows = matrix.size 3 cols = matrix[0].size 4 output = [] 5 6 (rows - 1).downto(0) do |row| 7 (cols - 1).downto(0) do |col| 8 output << matrix[row][col] 9 end 10 end 11 12 output 13end
In this function, we use downto
to create a decreasing range. This allows us to start from the bottom-right corner and traverse leftwards, then upwards, covering the entire matrix in reverse order.
Congratulations! You have made it through a challenging task in which you maneuvered your way through complex matrix traversal patterns. The functions you've designed not only test your Ruby coding skills but also your ability to visualize spatial patterns.
It's time to put your new knowledge to the test! The next step is to tackle similar challenges on your own. I encourage you to use this lesson as your guide and don't forget to experiment with different matrix sizes and cell values. With plenty of practice, you'll soon master these traversing patterns!