diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml new file mode 100644 index 00000000..d14ab4fa --- /dev/null +++ b/.idea/checkstyle-idea.xml @@ -0,0 +1,16 @@ + + + + 11.0.1 + JavaOnly + true + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 00000000..aa00ffab --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..9cc7e822 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/readme.md b/readme.md index abd36a26..db3324b0 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/src/main/java/NumberTriangle.java b/src/main/java/NumberTriangle.java index 30e10ecb..2dfc25d7 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. @@ -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 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 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(); }