Skip to content

Commit

Permalink
Applied DRY for InputFormatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
varnaa committed Nov 28, 2020
1 parent e5f507a commit f6013b5
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/main/java/com/varnaa/InputFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ class InputFormatter {

protected int[] format1dArray(String input) {
assert input != null : "input to format can not be null";
input = input.replace("[", "").replace("]", "");
input = input.replaceAll(" ", "");
input = formatInput(input);
String[] inputArray = input.split(",");
int[] array = new int[inputArray.length];

for (int i = 0; i < array.length; i++) {
array[i] = Integer.parseInt(inputArray[i]);
}
Expand All @@ -25,10 +23,9 @@ protected int[] format1dArray(String input) {

protected char[][] format2dCharArray(String input, int rowLength, int colLength) {
assert input != null : "input to format can not be null";
input = input.replace("[", "").replace("]", "");
input = input.replaceAll("'", "");
input = input.replaceAll(" ", "");
input = input.replaceAll(",", "");
input = formatInput(input);
int index = 0;
char[][] grid = new char[rowLength][colLength];
for (int i = 0; i < rowLength; i++) {
Expand All @@ -41,8 +38,7 @@ protected char[][] format2dCharArray(String input, int rowLength, int colLength)

protected int[][] format2dIntArray(String input, int rowLength, int colLength) {
assert input != null : "input to format can not be null";
input = input.replace("[", "").replace("]", "");
input = input.replaceAll(" ", "");
input = formatInput(input);
String[] inputArray = input.split(",");
int index = 0;
int[][] matrix = new int[rowLength][colLength];
Expand All @@ -58,8 +54,7 @@ protected int[][] format2dIntArray(String input, int rowLength, int colLength) {
protected Node formatToLinkedList(String input) {

assert input != null : "input to format can not be null";
input = input.replace("[", "").replace("]", "");
input = input.replaceAll(" ", "");
input = formatInput(input);
String[] inputArray = input.split(",");
System.out.println(Arrays.toString(inputArray));
linkedList = new LinkedList();
Expand All @@ -81,10 +76,15 @@ protected Node formatToLinkedList(int[] array) {

protected TreeNode formatBST(String input) {
binarySearchTree = new BinarySearchTree();
input = input.replace("[", "").replace("]", "");
input = input.replaceAll(" ", "");
input = formatInput(input);
return binarySearchTree.deserialize(input);
}

// Todo: Optimization
private String formatInput(String input) {
input = input.replace("[", "").replace("]", "");
input = input.replaceAll(" ", "");
return input;
}

}

0 comments on commit f6013b5

Please sign in to comment.