Greetings, aspiring Scala programmer! Are you eager to delve into the captivating challenge of matrix traversal in Scala? In this lesson, we’ll explore unique traversal patterns of matrices using the expressive and type-safe Scala language. Our journey will navigate through a matrix in a zigzag pattern, moving up and down the columns. Prepare yourself for an exciting journey as we unravel the depths of matrix manipulation in Scala!
Task Statement
Solution Building: Step 1
To begin, we must understand the dimensions of the matrix we're working with. We'll use Scala's array properties to determine this aspect. Let's initiate our function and ascertain the matrix size:
def columnTraverse(matrix: Array[Array[Int]]): List[Int] = { val rows = matrix.length val cols = matrix(0).length}
Solution Building: Step 2
Having established the matrix dimensions, it’s important to set our initial position (bottom-right) and direction (upward to start). Additionally, we'll need a structure to log the cells visited:
def columnTraverse(matrix: Array[Array[Int]]): List[Int] = { val rows = matrix.length val cols = matrix(0).length var direction = "up" var row = rows - 1 var col = cols - 1 var output: List[Int] = List()}
Solution Building: Step 3
Let's embark on our exploration! We'll use a while loop to handle the matrix traversal. The loop continues until all cells in the matrix have been visited. The value at each visited cell is appended to our list as follows:
def columnTraverse(matrix: Array[Array[Int]]): List[Int] = { val rows = matrix.length val cols = matrix(0).length var direction = "up" var row = rows - 1 var col = cols - 1 var output: List[Int] = List() while (output.length < rows * cols) { output = output :+ matrix(row)(col) if (direction == "up") { if (row - 1 < 0) { direction = "down" col -= 1 } else { row -= 1 } } else { if (row + 1 == rows) { direction = "up" col -= 1 } else { row += 1 } } } output}
Congratulations, our function is complete! This Scala function will now return the output list, presenting the traverse order through the matrix.
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal
Here is your challenge: You are given a 2D matrix, with each cell containing a unique integer value. Your objective is to develop a function that starts traversal from the bottom-right cell of the matrix. From there, you'll ascend the current column to its top, move left to the next column, and then descend to its bottom. After reaching the bottom, proceed left to continue the upward traversal. This zigzag pattern is maintained until every cell is visited.
Consider this small 3×4 matrix as illustrated below:
Following the described traversal pattern, your function should return this list: List(12, 8, 4, 3, 7, 11, 10, 6, 2, 1, 5, 9).
Scala
def columnTraverse(matrix: Array[Array[Int]]): List[Int] = { val rows = matrix.length val cols = matrix(0).length}
Scala
def columnTraverse(matrix: Array[Array[Int]]): List[Int] = { val rows = matrix.length val cols = matrix(0).length var direction = "up" var row = rows - 1 var col = cols - 1 var output: List[Int] = List()}
Scala
def columnTraverse(matrix: Array[Array[Int]]): List[Int] = { val rows = matrix.length val cols = matrix(0).length var direction = "up" var row = rows - 1 var col = cols - 1 var output: List[Int] = List() while (output.length < rows * cols) { output = output :+ matrix(row)(col) if (direction == "up") { if (row - 1 < 0) { direction = "down" col -= 1 } else { row -= 1 } } else { if (row + 1 == rows) { direction = "up" col -= 1 } else { row += 1 } } } output}
Traverse Using Decreasing Range
Lesson Summary
Well done! You've successfully navigated through complex matrix traversal patterns using Scala. The functions you’ve crafted not only test your coding aptitude but also challenge your spatial visualization skills.
With continuous practice, you’ll soon master the art of traversing matrices using Scala’s powerful features. Don't hesitate to experiment with various matrix dimensions and values to further hone your skills. Happy coding!
Let's explore another traversal strategy by leveraging Scala's Range utility to traverse a 2D matrix in reverse order. By configuring a decrementing sequence, we can achieve efficient reverse traversal.
To accomplish this, Scala’s Range can take three arguments: start, end, and step. By setting step to -1, we create a decreasing sequence.
With a decrementing Range, our reverse traversal pattern returns this list: List(12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1).
Here’s how we can implement this reverse traversal:
Scala
def reverseTraverse(matrix: Array[Array[Int]]): List[Int] = { val rows = matrix.length val cols = matrix(0).length var output: List[Int] = List() for (row <- (rows - 1) to 0 by -1) { for (col <- (cols - 1) to 0 by -1) { output = output :+ matrix(row)(col) } } output}
In this function, the Range for row starts from rows - 1 and goes until 0. The nested Range for col does the same. This allows us to traverse from bottom-right towards top-left, completing a reverse order cover of the matrix.