Skip to content
Open
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
42 changes: 33 additions & 9 deletions src/main/java/NumberTriangle.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.io.*;
import java.util.ArrayList;

/**
* This is the provided NumberTriangle class to be used in this coding task.
Expand Down Expand Up @@ -88,8 +89,19 @@ public boolean isLeaf() {
*
*/
public int retrieve(String path) {
// TODO implement this method
return -1;
NumberTriangle current = this;

for (int i = 0; i < path.length(); i++) {
char direction = path.charAt(i);

if (direction == ('r')) {
current = current.right;
} else if (direction == ('l')) {
current = current.left;
}

}
return current.getRoot();
}

/** Read in the NumberTriangle structure from a file.
Expand All @@ -109,20 +121,32 @@ public static NumberTriangle loadTriangle(String fname) throws IOException {
InputStream inputStream = NumberTriangle.class.getClassLoader().getResourceAsStream(fname);
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));


// TODO define any variables that you want to use to store things
ArrayList<NumberTriangle> prevrow = new ArrayList<>();

// will need to return the top of the NumberTriangle,
// so might want a variable for that.
NumberTriangle top = null;

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);

// TODO process the line
while (line != null) {
String[] parts = line.trim().split("\\s+");
ArrayList<NumberTriangle> currrow = new ArrayList<>();

for (String part : parts) {
currrow.add(new NumberTriangle(Integer.parseInt(part)));
}

if (prevrow.size() != 0) {
for (int i = 0; i < prevrow.size(); i++) {
prevrow.get(i).setLeft(currrow.get(i));
prevrow.get(i).setRight(currrow.get(i + 1));
}
} else {
top = currrow.get(0);
}

prevrow = currrow;

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