From e49cec32ac0cdc7865af4eced193053326188c2c Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 25 Sep 2025 02:05:22 -0500 Subject: [PATCH 01/14] First question complete --- src/Practice.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..9c3efc7 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,7 +11,24 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + if(nums == null) + { + return 0; + } + int odds = 0; + + if(nums == odds) + { + return 0; + } + for(int i = 0; i<=nums.length; i++) + { + if(nums[i] % 2 != 0) + { + odds = odds + nums[i]; + } + } + return odds; } /** From 72ae41da8c4c1993ceb55efb72d899c37928808a Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 25 Sep 2025 02:21:39 -0500 Subject: [PATCH 02/14] adults complete --- src/Practice.java | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 9c3efc7..9d261ba 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -43,7 +43,21 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + String shortWord = ""; + + if(words == null || words.length == 0) + { + throw new NullPointerException; + } + + for(word : words) + { + if(word.length < shortWord.length) + { + shortWord = word; + } + } + return shortWord; } /** @@ -56,7 +70,18 @@ public static String shortestWord(Set words) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { - return null; + if(ages == null) + { + throw new NullPointerException; + } + + for (String names : ages.keySet()) { + if(ages.get(names) >= 18) + { + grownUps.add(names); + } + } + return null; } /** @@ -67,7 +92,21 @@ public static Set adults(Map ages) { * @throws IllegalArgumentException if head is null */ public static int biggestNumber(ListNode head) { - return 0; + if(head = null) + { + throw new NullPointerException; + } + int bigNum = 0; + if(head != null) + { + if(head.data < bigNum) + { + bigNum = head.data; + } + head = head.next; + } + + return 0; } /** From 642e00b2210bd102ba64f89b8dcd4786c10dc4b6 Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 25 Sep 2025 02:29:57 -0500 Subject: [PATCH 03/14] biggest number complete --- src/Practice.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 9d261ba..15ca216 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -72,7 +72,7 @@ public static String shortestWord(Set words) { public static Set adults(Map ages) { if(ages == null) { - throw new NullPointerException; + throw new NullPointerException("Head cannot be null");; } for (String names : ages.keySet()) { @@ -92,21 +92,21 @@ public static Set adults(Map ages) { * @throws IllegalArgumentException if head is null */ public static int biggestNumber(ListNode head) { - if(head = null) + if(head == null) { - throw new NullPointerException; + throw new IllegalArgumentException("Head cannot be null");; } - int bigNum = 0; - if(head != null) + int bigNum = head.data; + while(head != null) { - if(head.data < bigNum) + if(head.data > bigNum) { bigNum = head.data; } head = head.next; } - return 0; + return bigNum; } /** From 55e0b5e5c9289fe196ac254349baf59b05b78215 Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 25 Sep 2025 02:34:53 -0500 Subject: [PATCH 04/14] frequencies complete --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 15ca216..ab2f405 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -123,7 +123,20 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map counts = new HashMap<>(); + while(head != null) + { + if(counts.containsKey(head.data)) + { + counts.put(head.data, counts.get(head.data) + 1); + } + else + { + counts.put(head.data, 1); + } + head = head.next; + } + return counts; } From 7bf459715cf71abda8efec17007d6620396736b9 Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 25 Sep 2025 02:38:51 -0500 Subject: [PATCH 05/14] level count starting --- src/Practice.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ab2f405..eb05ef5 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -149,7 +149,10 @@ public static Map frequencies(ListNode head) { * @return the number of levels in the tree */ public static int levelCount(BinaryTreeNode root) { - return 0; + if(root == null) + { + return 0; + } } From b0bf761738c6703a18482736062556ad92b0973a Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 25 Sep 2025 02:42:05 -0500 Subject: [PATCH 06/14] level count complete --- src/Practice.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index eb05ef5..03b6447 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -153,6 +153,14 @@ public static int levelCount(BinaryTreeNode root) { { return 0; } + int left = levelCount(root.left); + int right = levelCount(root.right); + if (left > right) { + return left + 1; + } + else { + return right + 1; + } } From bd066f361195da90245837dc36238b8987e8e9db Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 25 Sep 2025 09:42:27 -0500 Subject: [PATCH 07/14] starting sum at level --- src/Practice.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index 03b6447..2113296 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -188,6 +188,10 @@ public static int levelCount(BinaryTreeNode root) { * @return the sum of the nodes at the given level */ public static int sumAtLevel(BinaryTreeNode root, int level) { + if(root == null || level < 1) + { + return 0; + } return 0; } From cfac11190bdaf5b95c0dc000bb86516542c41d79 Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 25 Sep 2025 09:47:18 -0500 Subject: [PATCH 08/14] sum at level bfs --- src/Practice.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index 2113296..6b427ad 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -188,10 +188,18 @@ public static int levelCount(BinaryTreeNode root) { * @return the sum of the nodes at the given level */ public static int sumAtLevel(BinaryTreeNode root, int level) { + if(root == null || level < 1) { return 0; } + Queue> queue = new LinkedList<>(); + queue.add(root); + int currentLevel = 1; + int sum = 0; + while(!queue.isEmpty()) { + + }) return 0; } From 4c92bd656fcbc09a06aa4feeb8d3ca2062c10db6 Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 25 Sep 2025 09:57:13 -0500 Subject: [PATCH 09/14] sum at level bug --- src/Practice.java | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 6b427ad..f1efc85 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -198,10 +198,26 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { int currentLevel = 1; int sum = 0; while(!queue.isEmpty()) { - - }) + int size = queue.size(); + for(int = 0; i < size; i++) { + BinaryTreeNode node = queue.poll(); + if(currentLevel == level) { + sum += node.data; + } + if(node.left == null) { + queue.add(node.left); + } + if(node.right == null) { + queue.add(node.right); + } + } + if(currentLevel == level) { + return sum; + } + currentLevel++; + } return 0; - } + } /** From 507a259363fa66b10ad6b03c1adac0d8d740949e Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 25 Sep 2025 10:01:46 -0500 Subject: [PATCH 10/14] sum at level bug fix and graph sum dfs start --- src/Practice.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index f1efc85..2e3a054 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -204,10 +204,10 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { if(currentLevel == level) { sum += node.data; } - if(node.left == null) { + if(node.left != null) { queue.add(node.left); } - if(node.right == null) { + if(node.right != null) { queue.add(node.right); } } @@ -244,9 +244,23 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @return the sum of all the vertices */ public static int graphSum(Vertex start) { + if(start == null) + { + return 0; + } + Set> visited = new HashSet<>(); return 0; } + public static int graphSum(Vertex start, Set> visited) { + if(start == null) + { + return 0; + } + + } + + /** * Returns the count of vertices in a graph that have an outdegree of 0. * From 6a20d1dce85f81c550b50b484ff328c308264123 Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 25 Sep 2025 10:05:39 -0500 Subject: [PATCH 11/14] traversing the vertex --- src/Practice.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 2e3a054..e8a9265 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -252,13 +252,19 @@ public static int graphSum(Vertex start) { return 0; } - public static int graphSum(Vertex start, Set> visited) { - if(start == null) - { - return 0; - } - + public static int graphSum(Vertex node, Set> visited) { + if(node == null || visited.contains(node)) + { + return 0; + } + visited.add(node); + int sum = node.data; + for(Vertex neighbor : node.getNeighbors()) + { + graphSum(neighbor, visited); } + return sum; + } /** From 1c21d92f59f325324990f87702774be3a63de62e Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 25 Sep 2025 10:07:17 -0500 Subject: [PATCH 12/14] graph sum complete --- src/Practice.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index e8a9265..5506589 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -249,7 +249,7 @@ public static int graphSum(Vertex start) { return 0; } Set> visited = new HashSet<>(); - return 0; + return graphSum(start, visited); } public static int graphSum(Vertex node, Set> visited) { @@ -258,10 +258,11 @@ public static int graphSum(Vertex node, Set> visited) { return 0; } visited.add(node); + int sum = node.data; for(Vertex neighbor : node.getNeighbors()) { - graphSum(neighbor, visited); + sum+=graphSum(neighbor, visited); } return sum; } From 95741939a491c37d6387703782797c1ad966c014 Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 25 Sep 2025 10:40:05 -0500 Subject: [PATCH 13/14] sumMatch complete --- src/Practice.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 5506589..477ddcc 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -231,7 +231,27 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { * @return true if the sums are equal, false otherwise */ public static boolean sumMatch(BinaryTreeNode root, ListNode head) { - return false; + if(treeSum(root) == listSum(head)) + { + return true; + } + else return false; + } + public static int treeSum(BinaryTreeNode node) { + if(node == null) + { + return 0; + } + return node.data + treeSum(node.left) + treeSum(node.right); + } + public static int listSum(ListNode head) { + int sum = 0; + while(head != null) + { + sum += head.data; + head = head.next; + } + return sum; } /** From c42ac6f46d72c06780697a74cb26161869336577 Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Thu, 2 Oct 2025 18:21:30 -0500 Subject: [PATCH 14/14] updated needed fixes --- src/Practice.java | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 477ddcc..4a0e7c5 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,4 +1,8 @@ +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; import java.util.Map; +import java.util.Queue; import java.util.Set; public class Practice { @@ -17,10 +21,6 @@ public static int oddSum(int[] nums) { } int odds = 0; - if(nums == odds) - { - return 0; - } for(int i = 0; i<=nums.length; i++) { if(nums[i] % 2 != 0) @@ -45,14 +45,14 @@ public static int oddSum(int[] nums) { public static String shortestWord(Set words) { String shortWord = ""; - if(words == null || words.length == 0) + if(words == null || words.size() == 0) { - throw new NullPointerException; + throw new IllegalArgumentException("Not allowed"); } - for(word : words) + for(var word : words) { - if(word.length < shortWord.length) + if(word.length() < shortWord.length()) { shortWord = word; } @@ -72,9 +72,9 @@ public static String shortestWord(Set words) { public static Set adults(Map ages) { if(ages == null) { - throw new NullPointerException("Head cannot be null");; + throw new NullPointerException("Head cannot be null"); } - + Set grownUps = new HashSet<>(); for (String names : ages.keySet()) { if(ages.get(names) >= 18) { @@ -94,7 +94,7 @@ public static Set adults(Map ages) { public static int biggestNumber(ListNode head) { if(head == null) { - throw new IllegalArgumentException("Head cannot be null");; + throw new IllegalArgumentException("Head cannot be null"); } int bigNum = head.data; while(head != null) @@ -199,7 +199,7 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { int sum = 0; while(!queue.isEmpty()) { int size = queue.size(); - for(int = 0; i < size; i++) { + for(int i = 0; i < size; i++) { BinaryTreeNode node = queue.poll(); if(currentLevel == level) { sum += node.data; @@ -280,7 +280,7 @@ public static int graphSum(Vertex node, Set> visited) { visited.add(node); int sum = node.data; - for(Vertex neighbor : node.getNeighbors()) + for(Vertex neighbor : node.neighbors) { sum+=graphSum(neighbor, visited); }