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.

16 changes: 16 additions & 0 deletions .idea/checkstyle-idea.xml

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.

4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ what you are tasked with for this task.
- To ensure that you have learned the basics of using git and GitHub to be able to contribute to your team.
- To practice reading in data from an outside source and creating objects to represent that data.

## The Task:
## The Task:

- [ ] Fork this repo and clone a local copy to work in.
- [] 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
methods and variables we have defined to represent the structure.
Expand Down
38 changes: 27 additions & 11 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 @@ -108,22 +110,36 @@ public static NumberTriangle loadTriangle(String fname) throws IOException {
// are more convenient to work with when reading the file contents.
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

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

List<NumberTriangle> prevRow = new ArrayList<>();
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

line = line.trim();
if (!line.isEmpty()) {
// parse the current row of ints (allowing multiple spaces)
String[] parts = line.split("\\s+");
List<NumberTriangle> curRow = new ArrayList<>(parts.length);
for (String p : parts) {
int val = Integer.parseInt(p);
curRow.add(new NumberTriangle(val));
}
if (top == null) {
// first row: single element, becomes the top
top = curRow.get(0);
} else {
// connect each parent in prevRow to two children in curRow
// parent j -> left = curRow[j], right = curRow[j+1]
for (int j = 0; j < prevRow.size(); j++) {
NumberTriangle parent = prevRow.get(j);
parent.setLeft(curRow.get(j));
parent.setRight(curRow.get(j + 1));
}
}
// move down one level
prevRow = curRow;
}
//read the next line
line = br.readLine();
}
Expand Down