Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

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.

60 changes: 54 additions & 6 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.*;


/**
* This is the provided NumberTriangle class to be used in this coding task.
Expand Down Expand Up @@ -87,11 +89,26 @@ public boolean isLeaf() {
* @return the root value at the location indicated by path
*
*/

public int retrieve(String path) {
// TODO implement this method
return -1;
NumberTriangle cur = this;
for (int i = 0; i < path.length(); i++) {
char c = path.charAt(i);
if (c == 'l') {
cur = cur.left;
} else if (c == 'r') {
cur = cur.right;
} else {
throw new IllegalArgumentException("Invalid path char: " + c);
}
if (cur == null) {
throw new IndexOutOfBoundsException("Path goes past the triangle at index " + i);
}
}
return cur.root;
}


/** Read in the NumberTriangle structure from a file.
*
* You may assume that it is a valid format with a height of at least 1,
Expand All @@ -110,7 +127,7 @@ 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
List<List<Integer>> rows = new ArrayList<>();

// will need to return the top of the NumberTriangle,
// so might want a variable for that.
Expand All @@ -119,14 +136,45 @@ public static NumberTriangle loadTriangle(String fname) throws IOException {
String line = br.readLine();
while (line != null) {

// remove when done; this line is included so running starter code prints the contents of the file
System.out.println(line);
// System.out.println(line);

line = line.trim();
if (!line.isEmpty()) {
String[] parts = line.split("\\s+");
List<Integer> row = new ArrayList<>();
for (String p : parts) {
row.add(Integer.parseInt(p));
}
rows.add(row);
}

// TODO process the line

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


List<NumberTriangle> prevRow = new ArrayList<>();
List<Integer> lastRow = rows.get(rows.size() - 1);
for (int n : lastRow) {
prevRow.add(new NumberTriangle(n));
}

for (int i = rows.size() - 2; i >= 0; i--) {
List<Integer> current = rows.get(i);
List<NumberTriangle> newRow = new ArrayList<>();
for (int j = 0; j < current.size(); j++) {
NumberTriangle node = new NumberTriangle(current.get(j));
node.setLeft(prevRow.get(j));
node.setRight(prevRow.get(j + 1));
newRow.add(node);
}
prevRow = newRow;
}

top = prevRow.get(0);


br.close();
return top;
}
Expand Down