Last active
May 25, 2020 06:44
-
-
Save Daniiarz/d9b8c63a056e9c4189b4b3286ed6c444 to your computer and use it in GitHub Desktop.
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 java.util.ArrayList; | |
import java.util.List; | |
public class Flattener { | |
public List flatten(List list) { | |
List result = new ArrayList(); | |
for (Object o : list) { | |
if (o == null) { | |
continue; | |
} else if (o instanceof List) { | |
result.addAll(flatten((List) o)); | |
} else { | |
result.add(o); | |
} | |
} | |
return result; | |
} | |
} |
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 java.util.Arrays; | |
class Matrix { | |
private int[][] a; | |
Matrix(String matrixAsString) { | |
String[] rows = matrixAsString.split("\\n+"); | |
int ncols = rows[0].split("\\s+").length; | |
a = new int[rows.length][ncols]; | |
for (int i = 0; i < rows.length; i++) { | |
String[] row = rows[i].split("\\s+"); | |
for (int j = 0; j < row.length; j++) | |
a[i][j] = Integer.parseInt(row[j]); | |
} | |
} | |
int[] getRow(int rowNumber) { | |
return a[rowNumber-1]; | |
} | |
int[] getColumn(int columnNumber) { | |
int[] col = new int[a.length]; | |
for (int i = 0; i < a.length; i++) | |
col[i] = a[i][columnNumber-1]; | |
return col; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment