Last active
October 22, 2018 13:24
-
-
Save zantetsuken88/645a2f3ac2a4f12a732f4bce9947dd89 to your computer and use it in GitHub Desktop.
exercism/java/matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Matrix { | |
private int[][] matrixAsArray; | |
Matrix(String matrixAsString) { | |
this.matrixAsArray = convertToArray(matrixAsString); | |
} | |
int[] getRow(int rowNumber) { | |
return matrixAsArray[rowNumber]; | |
} | |
int[] getColumn(int columnNumber) { | |
int[] column = new int[matrixAsArray.length]; | |
for (int i = 0; i < column.length; i++) { | |
column[i] = getRow(i)[columnNumber]; | |
} | |
return column; | |
} | |
private int[][] convertToArray(String matrixAsString) { | |
String[] rows = matrixAsString.split("[\\n]+"); | |
int[][] matrixArray = new int[rows.length][]; | |
for ( int i = 0; i < rows.length; i++) { | |
String[] rowChars = rows[i].split("\\s"); | |
int[] row = new int[rowChars.length]; | |
for (int c = 0; c < row.length; c++) { | |
row[c] = Integer.valueOf(rowChars[c]); | |
} | |
matrixArray[i] = row; | |
} | |
return matrixArray; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import static org.junit.Assert.assertArrayEquals; | |
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)); | |
} | |
@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)); | |
} | |
@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)); | |
} | |
@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)); | |
} | |
@Test | |
public void extractColumnFromOneNumberMatrixTest() { | |
String matrixAsString = "1"; | |
int columnIndex = 0; | |
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 = 2; | |
int[] expectedColumn = {3, 6, 9}; | |
Matrix matrix = new Matrix(matrixAsString); | |
assertArrayEquals(expectedColumn, matrix.getColumn(columnIndex)); | |
} | |
@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)); | |
} | |
@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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mentor discussion
zantetsuken88
Hi, my first attempt at 2d arrays so maybe there's a better way of doing it, but all the tests pass
dusan-rychnovsky
Much better :)
I think it would be better to internally keep the data in the parsed array form, rather than as string. This way you don't need to parse the matrix again every time. Especially in the getColumn method you parse the string number of rows + 1 times!
zantetsuken88
Hi, I don't really understand the feedback. Would you perhaps be able to explain in more detail please?
Thanks
dusan-rychnovsky
Yes, I mean, what if you store int[][] matrix instead of String matrixAsString? Then in getRow you can just return matrix[rowNumber]. This is a much more efficient solution, considering that your current solution needs to parse the matrix string (row + 1) times when you call getColumn.