From 55193ac2306c0211d1f1cc900e4bc917b600f4d7 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:24:08 -0700 Subject: [PATCH 1/9] completed oddSum --- src/Practice.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..891dd03 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,7 +11,18 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + int sum = 0; + if (nums == null) { + return 0; + } + + for (int i = 0; i < nums.length; i++) { + if (nums[i] % 2 != 0) { + sum+=nums[i]; + } + } + + return sum; } /** @@ -26,7 +37,7 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + } /** From 452c93beb7e611481a8d6506a3c8ca0f7acf5cbb Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:44:33 -0700 Subject: [PATCH 2/9] added shortestWord --- src/Practice.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 891dd03..f0e29be 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -37,7 +37,21 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - + if (words == null) throw new NullPointerException("Words was not created"); + if (words.isEmpty()) throw new IllegalArgumentException("Words is empty"); + String shortest = null; + for (String word : words) { + if (shortest == null) shortest = word; + if (shortest.length() > word.length()) { + shortest = word; + } else if (shortest.length() == word.length()) { + if (word.compareTo(shortest) < 0) { + shortest = word; + } + } + } + + return shortest; } /** From 9e1ce528d2b63c39770e819647c350a8ac73f795 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Tue, 23 Sep 2025 12:45:31 -0700 Subject: [PATCH 3/9] competed adults; tests passing --- src/Practice.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index f0e29be..ae12e24 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -64,7 +65,17 @@ 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(); + + Set adults = new HashSet(); + + for (String person : ages.keySet()) { + if (ages.get(person) >= 18) { + adults.add(person); + } + } + + return adults; } /** From d3765a9562b2dd0a0bc1afc2b38a009a4aaf35b9 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Tue, 23 Sep 2025 12:53:08 -0700 Subject: [PATCH 4/9] completed biggestNumber --- src/Practice.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index ae12e24..46c21ba 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -66,7 +66,7 @@ public static String shortestWord(Set words) { */ public static Set adults(Map ages) { if (ages == null) throw new NullPointerException(); - + Set adults = new HashSet(); for (String person : ages.keySet()) { @@ -86,7 +86,17 @@ 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 IllegalArgumentException(); + int max = head.data; + + ListNode current = head.next; + + while (current != null) { + if (current.data > max) max = current.data; + current = current.next; + } + + return max; } /** From 3c96db3a04d19a2ea02e3a3467c843a2fbe8afaa Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Tue, 23 Sep 2025 15:58:11 -0700 Subject: [PATCH 5/9] completed frequencies --- src/Practice.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 46c21ba..14d4a8a 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -113,7 +114,21 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map frequency = new HashMap<>(); + if (head == null) return frequency; + + ListNode current = head; + while (current != null) { + T data = current.data; + if (!frequency.containsKey(data)) { + frequency.put(data, 1); + } else { + frequency.put(data, frequency.get(data)+1); + } + current = current.next; + } + + return frequency; } From 34d8c0fc122558cc984049ecb4f2a3568794ef90 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Tue, 23 Sep 2025 16:04:06 -0700 Subject: [PATCH 6/9] added levelCount --- src/Practice.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 14d4a8a..d5dc7bd 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -141,7 +141,9 @@ 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; + + return 1 + Integer.max(levelCount(root.left), levelCount(root.right)); } From d2cba266ff96ee7868bc3feedc6d61689d3ebfc7 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Tue, 23 Sep 2025 16:33:07 -0700 Subject: [PATCH 7/9] sumAtLevel added --- src/Practice.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index d5dc7bd..61ee0ee 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,6 +1,9 @@ import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.Map; +import java.util.Queue; import java.util.Set; public class Practice { @@ -171,9 +174,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) { - return 0; + + return traverse(root, level, 1); } + private static int traverse(BinaryTreeNode root, int targetLevel, int currentLevel) { + if (root == null) return 0; + + if (currentLevel == targetLevel) return root.data; + + return traverse(root.left, targetLevel, currentLevel+1) + traverse(root.right, targetLevel, currentLevel+1); + } + /** * Returns true if the sum of the values in a given tree is equal to the sum From 0bf5fd6dc7140cb5fa50fc57ab709e5180cc6de2 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Wed, 24 Sep 2025 08:44:41 -0700 Subject: [PATCH 8/9] added sumMatch --- src/Practice.java | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 61ee0ee..f3f4409 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,9 +1,6 @@ import java.util.HashMap; import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedList; import java.util.Map; -import java.util.Queue; import java.util.Set; public class Practice { @@ -178,7 +175,7 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { return traverse(root, level, 1); } - private static int traverse(BinaryTreeNode root, int targetLevel, int currentLevel) { + public static int traverse(BinaryTreeNode root, int targetLevel, int currentLevel) { if (root == null) return 0; if (currentLevel == targetLevel) return root.data; @@ -198,9 +195,33 @@ private static int traverse(BinaryTreeNode root, int targetLevel, int c * @return true if the sums are equal, false otherwise */ public static boolean sumMatch(BinaryTreeNode root, ListNode head) { + int treeTotal = traverseTreeSum(root); + int listTotal = traverseListSum(head); + + if (treeTotal == listTotal) return true; + return false; } + public static int traverseTreeSum(BinaryTreeNode root) { + if (root == null) return 0; + + return root.data + traverseTreeSum(root.left) + traverseTreeSum(root.right); + } + + public static int traverseListSum(ListNode head) { + if (head == null) return 0; + + ListNode current = head; + int total = 0; + while (current != null) { + total+=current.data; + current = current.next; + } + + return total; + } + /** * Returns the sum of all the vertices in a graph that are reachable from a given * starting vertex. From 757f23f4ad218a039f51ed58e07658587463baf0 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Wed, 24 Sep 2025 09:12:32 -0700 Subject: [PATCH 9/9] added sinkCount --- src/Practice.java | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index f3f4409..a5f7157 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -232,7 +232,26 @@ public static int traverseListSum(ListNode head) { * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + if (start == null) return 0; + + Set> visited = new HashSet<>(); + + return graphSum(start, visited); + } + + public static int graphSum(Vertex current, Set> visited) { + if (current == null || visited.contains(current)) return 0; + + visited.add(current); + int sum = current.data; + + if (current.neighbors == null) return sum; + + for (Vertex vertex : current.neighbors) { + if (vertex != null) sum+=graphSum(vertex, visited); + } + + return sum; } /** @@ -244,6 +263,24 @@ public static int graphSum(Vertex start) { * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + if (start == null) return 0; + + Set> visited = new HashSet<>(); + + return sinkCount(start, visited); + } + + public static int sinkCount(Vertex current, Set> visited) { + if (current == null || visited.contains(current)) return 0; + + visited.add(current); + if (current.neighbors == null || current.neighbors.isEmpty()) return 1; + + int exitCount = 0; + for (Vertex vertex : current.neighbors) { + exitCount+=sinkCount(vertex, visited); + } + + return exitCount; } } \ No newline at end of file