+
+
\ No newline at end of file
diff --git a/readme.md b/readme.md
index abd36a26..1bb0666a 100644
--- a/readme.md
+++ b/readme.md
@@ -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:
diff --git a/src/main/java/NumberTriangle.java b/src/main/java/NumberTriangle.java
index 30e10ecb..a38cef9f 100644
--- a/src/main/java/NumberTriangle.java
+++ b/src/main/java/NumberTriangle.java
@@ -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.
@@ -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.
@@ -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 rows = new ArrayList<>();
+ List lowerRow = null;
// will need to return the top of the NumberTriangle,
// so might want a variable for that.
NumberTriangle top = null;
@@ -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 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;
}
@@ -142,3 +174,4 @@ public static void main(String[] args) throws IOException {
System.out.println(mt.getRoot());
}
}
+