🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of SergiiVlasiuk

SergiiVlasiuk's solution

to Matrix in the Scala Track

Published at Sep 01 2019 · 0 comments
Instructions
Test suite
Solution

Given a string representing a matrix of numbers, return the rows and columns of that matrix.

So given a string with embedded newlines like:

9 8 7
5 3 2
6 6 7

representing this matrix:

    0  1  2
  |---------
0 | 9  8  7
1 | 5  3  2
2 | 6  6  7

your code should be able to spit out:

  • A list of the rows, reading each row left-to-right while moving top-to-bottom across the rows,
  • A list of the columns, reading each column top-to-bottom while moving from left-to-right.

The rows for our example matrix:

  • 9, 8, 7
  • 5, 3, 2
  • 6, 6, 7

And its columns:

  • 9, 5, 6
  • 8, 3, 6
  • 7, 2, 7

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.

Source

Warmup to the saddle-points warmup. http://jumpstartlab.com

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

MatrixTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 1.0.0 */
class MatrixTest extends FunSuite with Matchers {

  test("extract row from one number matrix") {
    Matrix("1").row(0) should be(Vector(1))
  }

  test("can extract row") {
    pending
    Matrix("1 2\n" +
           "3 4").row(1) should be(Vector(3, 4))
  }

  test("extract row where numbers have different widths") {
    pending
    Matrix("1 2\n" +
           "10 20").row(1) should be(Vector(10, 20))
  }

  test("can extract row from non-square matrix") {
    pending
    Matrix("1 2 3\n"
         + "4 5 6\n"
         + "7 8 9\n"
         + "8 7 6").row(2) should be(
      Vector(7, 8, 9))
  }

  test("extract column from one number matrix") {
    pending
    Matrix("1").column(0) should be(Vector(1))
  }

  test("can extract column") {
    pending
    Matrix("1 2 3\n"
         + "4 5 6\n"
         + "7 8 9").column(2) should be(Vector(3, 6, 9))
  }

  test("can extract column from non-square matrix") {
    pending
    Matrix("1 2 3\n"
         + "4 5 6\n"
         + "7 8 9\n"
         + "8 7 6").column(2) should be(Vector(3, 6, 9, 6))
  }

  test("extract column where numbers have different widths") {
    pending
    Matrix("89 1903 3\n"
         + "18 3 1\n"
         + "9 4 800").column(1) should be(Vector(1903, 3, 4))
  }
}
case class Matrix[T](matrix: Seq[Array[T]]) {
  val row: Int => Array[T] = matrix
  val column: Int => Seq[T] = matrix.transpose
}

object Matrix {
  def apply(matrix: String): Matrix[Int] =
    Matrix(matrix.linesIterator.map(_.split(' ').map(_.toInt)).toVector)
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?