ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# bwielk's solution

## to Matrix in the Java Track

Published at Jan 16 2020 · 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:

``````    1  2  3
|---------
1 | 9  8  7
2 | 5  3  2
3 | 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

## Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

# Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

``````\$ gradle test
``````

Use `gradlew.bat` if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the `@Ignore("Remove to run test")` annotation.

## 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 static org.junit.Assert.assertArrayEquals;

public class MatrixTest {

@Test
public void extractRowFromOneNumberMatrixTest() {
String matrixAsString = "1";
int rowIndex = 1;
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 = 2;
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 = 2;
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 = 4;
int[] expectedRow = {8, 7, 6};

Matrix matrix = new Matrix(matrixAsString);

assertArrayEquals(expectedRow, matrix.getRow(rowIndex));
}

@Ignore("Remove to run test")
@Test
public void extractColumnFromOneNumberMatrixTest() {
String matrixAsString = "1";
int columnIndex = 1;
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 = 3;
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 4\n5 6 7 8\n9 8 7 6";
int columnIndex = 4;
int[] expectedColumn = {4, 8, 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 = 2;
int[] expectedColumn = {1903, 3, 4};

Matrix matrix = new Matrix(matrixAsString);

assertArrayEquals(expectedColumn, matrix.getColumn(columnIndex));
}
}``````

### src/main/java/Matrix.java

``````import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

class Matrix {

private String matrixAtString;
private List<List<String>> matrixes = new ArrayList<>();

Matrix(String matrixAsString) {
this.matrixAtString = matrixAsString;
List<String> splitMatrixStrings = Arrays.asList(matrixAsString.split("\n"));
for(String row : splitMatrixStrings) {
}
}

int[] getRow(int rowNumber) {
return matrixes.get(rowNumber-1).stream().mapToInt(Integer::parseInt).toArray();
}

int[] getColumn(int columnNumber) {
return matrixes.stream().map(matrixRow -> matrixRow.get(columnNumber-1)).collect(Collectors.toList())
.stream().mapToInt(Integer::parseInt).toArray();
}
}``````

### src/test/java/MatrixTest.java

``````import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;

public class MatrixTest {

@Test
public void extractRowFromOneNumberMatrixTest() {
String matrixAsString = "1";
int rowIndex = 1;
int[] expectedRow = {1};

Matrix matrix = new Matrix(matrixAsString);

assertArrayEquals(expectedRow, matrix.getRow(rowIndex));
}
@Test
public void extractRowFromMatrixTest() {
String matrixAsString = "1 2\n3 4";
int rowIndex = 2;
int[] expectedRow = {3, 4};

Matrix matrix = new Matrix(matrixAsString);

assertArrayEquals(expectedRow, matrix.getRow(rowIndex));
}
@Test
public void extractRowFromDiffWidthsMatrixTest() {
String matrixAsString = "1 2\n10 20";
int rowIndex = 2;
int[] expectedRow = {10, 20};

Matrix matrix = new Matrix(matrixAsString);

assertArrayEquals(expectedRow, matrix.getRow(rowIndex));
}
@Test
public void extractRowFromNonSquareMatrixTest() {
String matrixAsString = "1 2 3\n4 5 6\n7 8 9\n8 7 6";
int rowIndex = 4;
int[] expectedRow = {8, 7, 6};

Matrix matrix = new Matrix(matrixAsString);

assertArrayEquals(expectedRow, matrix.getRow(rowIndex));
}
@Test
public void extractColumnFromOneNumberMatrixTest() {
String matrixAsString = "1";
int columnIndex = 1;
int[] expectedColumn = {1};

Matrix matrix = new Matrix(matrixAsString);

assertArrayEquals(expectedColumn, matrix.getColumn(columnIndex));
}
@Test
public void extractColumnMatrixTest() {
String matrixAsString = "1 2 3\n4 5 6\n7 8 9";
int columnIndex = 3;
int[] expectedColumn = {3, 6, 9};

Matrix matrix = new Matrix(matrixAsString);

assertArrayEquals(expectedColumn, matrix.getColumn(columnIndex));
}
@Test
public void extractColumnFromNonSquareMatrixTest() {
String matrixAsString = "1 2 3 4\n5 6 7 8\n9 8 7 6";
int columnIndex = 4;
int[] expectedColumn = {4, 8, 6};

Matrix matrix = new Matrix(matrixAsString);

assertArrayEquals(expectedColumn, matrix.getColumn(columnIndex));
}
@Test
public void extractColumnFromDiffWidthsMatrixTest() {
String matrixAsString = "89 1903 3\n18 3 1\n9 4 800";
int columnIndex = 2;
int[] expectedColumn = {1903, 3, 4};

Matrix matrix = new Matrix(matrixAsString);

assertArrayEquals(expectedColumn, matrix.getColumn(columnIndex));
}
}``````