Multidimensional Matrix Traversal Techniques in Scala

Introduction

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

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×43 \times 4 matrix as illustrated below:

Scala
val matrix = Array(
  Array(1, 2, 3, 4),
  Array(5, 6, 7, 8),
  Array(9, 10, 11, 12)
)

Following the described traversal pattern, your function should return this list: List(12, 8, 4, 3, 7, 11, 10, 6, 2, 1, 5, 9).

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:

Scala
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:

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()
}

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:

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
}

Congratulations, our function is complete! This Scala function will now return the output list, presenting the traverse order through the matrix.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal