Skip to content
7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ what you are tasked with for this task.

## The Task:

- [ ] Fork this repo and clone a local copy to work in.
- [X] Fork this repo and clone a local copy to work in.

- [ ] Read the provided `NumberTriange.java` file to understand the definition of a number triangle and what
- [X] Read the provided `NumberTriange.java` file to understand the definition of a number triangle and what
methods and variables we have defined to represent the structure.

- [ ] Implement the static method for loading a number triangle from a file.
- [X] Implement the static method for loading a number triangle from a file.
- Create an issue for this in your repo, write your code on a branch, make a PR once finished, and merge your PR.

- [ ] Implement the retrieve method.
- [X] Implement the retrieve method.
- Create an issue for this in your repo, write your code on a branch, make a PR once finished, and merge your PR.

## Requirements:
Expand Down
43 changes: 38 additions & 5 deletions src/main/java/NumberTriangle.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
* This is the provided NumberTriangle class to be used in this coding task.
Expand Down Expand Up @@ -88,8 +90,17 @@ public boolean isLeaf() {
*
*/
public int retrieve(String path) {
// TODO implement this method
return -1;
NumberTriangle cur = this;
for (int i = 0; i < path.length(); i ++) {
char ch = path.charAt(i);
if (ch == 'l') {
cur = cur.left;
}
else if (ch == 'r') {
cur = cur.right;
}
}
return cur.root;
}

/** Read in the NumberTriangle structure from a file.
Expand All @@ -110,8 +121,8 @@ public static NumberTriangle loadTriangle(String fname) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));


// TODO define any variables that you want to use to store things

ArrayList<String[]> rows = new ArrayList<>();
List<NumberTriangle> lowerRow = null;
// will need to return the top of the NumberTriangle,
// so might want a variable for that.
NumberTriangle top = null;
Expand All @@ -122,11 +133,32 @@ public static NumberTriangle loadTriangle(String fname) throws IOException {
// remove when done; this line is included so running starter code prints the contents of the file
System.out.println(line);

// TODO process the line
String trimmed = line.trim();
if (!trimmed.isEmpty()) {
rows.add(trimmed.split("\\s+"));
}

//read the next line
line = br.readLine();
}

for (int r = rows.size() - 1; r >= 0; r--) {
String[] parts = rows.get(r);
java.util.ArrayList<NumberTriangle> currentRow = new java.util.ArrayList<>(parts.length);
for (int i = 0; i < parts.length; i++) {
NumberTriangle node = new NumberTriangle(Integer.parseInt(parts[i]));
if (lowerRow != null) {
node.setLeft(lowerRow.get(i));
node.setRight(lowerRow.get(i + 1));
}
currentRow.add(node);
}
lowerRow = currentRow;
}
if (lowerRow != null && !lowerRow.isEmpty()) {
top = lowerRow.get(0);
}

br.close();
return top;
}
Expand All @@ -142,3 +174,4 @@ public static void main(String[] args) throws IOException {
System.out.println(mt.getRoot());
}
}