Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Matrix in the Java Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

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.java

import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

public class MatrixTest {
	
	@Test
	public void extractRowFromOneNumberMatrixTest() {
		String matrixAsString = "1";
		int rowIndex = 0;
		int[] expectedRow = {1};
		
		Matrix matrix = new Matrix(matrixAsString);
		
		assertArrayEquals(expectedRow, matrix.getRow(rowIndex));
	}
	
	@Ignore("Remove to run test")
	@Test
	public void extractRowFromMatrixTest() {
		String matrixAsString = "1 2\n3 4";
		int rowIndex = 1;
		int[] expectedRow = {3, 4};
		
		Matrix matrix = new Matrix(matrixAsString);
		
		assertArrayEquals(expectedRow, matrix.getRow(rowIndex));
	}
	
	@Ignore("Remove to run test")
	@Test
	public void extractRowFromDiffWidthsMatrixTest() {
		String matrixAsString = "1 2\n10 20";
		int rowIndex = 1;
		int[] expectedRow = {10, 20};
		
		Matrix matrix = new Matrix(matrixAsString);
		
		assertArrayEquals(expectedRow, matrix.getRow(rowIndex));
	}
	
	@Ignore("Remove to run test")
	@Test
	public void extractRowFromNonSquareMatrixTest() {
		String matrixAsString = "1 2 3\n4 5 6\n7 8 9\n8 7 6";
		int rowIndex = 2;
		int[] expectedRow = {7, 8, 9};
		
		Matrix matrix = new Matrix(matrixAsString);
		
		assertArrayEquals(expectedRow, matrix.getRow(rowIndex));
	}
	
	@Ignore("Remove to run test")
	@Test
	public void extractColumnFromOneNumberMatrixTest() {
		String matrixAsString = "1";
		int columnIndex = 0;
		int[] expectedColumn = {1};
		
		Matrix matrix = new Matrix(matrixAsString);
		
		assertArrayEquals(expectedColumn, matrix.getColumn(columnIndex));
	}
	
	@Ignore("Remove to run test")
	@Test
	public void extractColumnMatrixTest() {
		String matrixAsString = "1 2 3\n4 5 6\n7 8 9";
		int columnIndex = 2;
		int[] expectedColumn = {3, 6, 9};
		
		Matrix matrix = new Matrix(matrixAsString);
		
		assertArrayEquals(expectedColumn, matrix.getColumn(columnIndex));
	}
	
	@Ignore("Remove to run test")
	@Test
	public void extractColumnFromNonSquareMatrixTest() {
		String matrixAsString = "1 2 3\n4 5 6\n7 8 9\n8 7 6";
		int columnIndex = 2;
		int[] expectedColumn = {3, 6, 9, 6};
		
		Matrix matrix = new Matrix(matrixAsString);
		
		assertArrayEquals(expectedColumn, matrix.getColumn(columnIndex));
	}
	
	@Ignore("Remove to run test")
	@Test
	public void extractColumnFromDiffWidthsMatrixTest() {
		String matrixAsString = "89 1903 3\n18 3 1\n9 4 800";
		int columnIndex = 1;
		int[] expectedColumn = {1903, 3, 4};
		
		Matrix matrix = new Matrix(matrixAsString);
		
		assertArrayEquals(expectedColumn, matrix.getColumn(columnIndex));
	}
}
import org.apache.commons.lang3.StringUtils;

public class Matrix {

  int[][] matrix;

  public Matrix(String matrixAsString) {
    matrix = populateMatrix(matrixAsString);
  }

  public int[] getRow(int rowIndex) {
    return matrix[rowIndex];
  }

  public int[] getColumn(int colIndex) {
    int[][] transposedMatrix = transposeMatrix(matrix);
    return transposedMatrix[colIndex];
  }

  public int getRowsCount() {
    return matrix.length;
  }

  public int getColumnsCount() {
    return matrix[0].length;
  }

  private int[][] populateMatrix(String matrixAsString) {
    int rowCount = rowCount(matrixAsString);
    int columnCount = columnCount(matrixAsString);
    int[][] matrix = new int[rowCount][columnCount];

    String[] rows = matrixAsString.split("\n");
    for (int rowIndex = 0; rowIndex < rows.length; rowIndex += 1) {
      String row = rows[rowIndex];
      String[] columns = row.split(" ");
      for (int columnIndex = 0; columnIndex < columns.length; columnIndex += 1) {
        matrix[rowIndex][columnIndex] = Integer.parseInt(columns[columnIndex]);
      }
    }

    return matrix;
  }

  private int[][] transposeMatrix(int[][] matrix) {
    int[][] transposedMatrix = new int[getColumnsCount()][getRowsCount()];

    for (int rowIndex = 0; rowIndex < getRowsCount(); rowIndex += 1) {
      for (int columnIndex = 0; columnIndex < getColumnsCount(); columnIndex += 1) {
        transposedMatrix[columnIndex][rowIndex] = matrix[rowIndex][columnIndex];
      }
    }

    return transposedMatrix;
  }

  private int rowCount(String matrixAsString) {
    return StringUtils.countMatches(matrixAsString, "\n") + 1;
  }

  private int columnCount(String matrixAsString) {
    return StringUtils.countMatches(firstRow(matrixAsString), " ") + 1;
  }

  private String firstRow(String matrixAsString) {
    return matrixAsString.split( "\n")[0];
  }

  private void print(int[][] matrix) {
    for (int rowIndex = 0; rowIndex < getRowsCount(); rowIndex += 1) {
      for (int columnIndex = 0; columnIndex < getColumnsCount(); columnIndex += 1) {
        System.out.print(matrix[rowIndex][columnIndex] + " ");
      }
      System.out.println();
    }
    System.out.println("-----");
  }

}

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?