From 2cc6363c7db5b76f2585445466b180f088571b8f Mon Sep 17 00:00:00 2001 From: elena5100 <194572363+elena5100@users.noreply.github.com> Date: Wed, 1 Oct 2025 21:31:36 -0700 Subject: [PATCH 1/9] return sum of odd numbers in array (0 if null/none) --- src/Practice.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..cb5bbda 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; } /** From bb02ea96ca4f7559e15c4c30f1479e8988ae84c7 Mon Sep 17 00:00:00 2001 From: elena5100 <194572363+elena5100@users.noreply.github.com> Date: Wed, 1 Oct 2025 21:48:25 -0700 Subject: [PATCH 2/9] Add shortestWord method to find smallest word with tie --- src/Practice.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index cb5bbda..2c55b5a 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -37,7 +37,19 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + + if (words == null) throw new NullPointerException(); + if (words.isEmpty()) throw new IllegalArgumentException(); + + String shortest = null; + for (String word : words) { + if (shortest == null) shortest = word; + if (word.length() < shortest.length()) shortest = word; + if (word.length() == shortest.length() && word.compareTo(shortest) < 0) shortest = word; + } + return shortest; + + } /** From 7665d21965def707f37a61db40736e8b335c5da4 Mon Sep 17 00:00:00 2001 From: elena5100 <194572363+elena5100@users.noreply.github.com> Date: Wed, 1 Oct 2025 22:12:26 -0700 Subject: [PATCH 3/9] return names of people age 18 or older --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 2c55b5a..e6b5d8e 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; @@ -62,7 +63,16 @@ 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 result = new HashSet<>(); + for (String name : ages.keySet()) { + if (ages.get(name) >= 18) { + result.add(name); + } + } + return result; + } /** From 819cb9117c443951e1d4e611c5bf44487f0101f2 Mon Sep 17 00:00:00 2001 From: elena5100 <194572363+elena5100@users.noreply.github.com> Date: Wed, 1 Oct 2025 22:24:54 -0700 Subject: [PATCH 4/9] return biggest number in linked list --- src/Practice.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index e6b5d8e..d217a83 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -83,7 +83,18 @@ 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 0ee93cb4cbd5b9ad607c4507424f1737bd06a318 Mon Sep 17 00:00:00 2001 From: elena5100 <194572363+elena5100@users.noreply.github.com> Date: Wed, 1 Oct 2025 22:41:31 -0700 Subject: [PATCH 5/9] return map of item counts in linked list --- src/Practice.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index d217a83..db6ac45 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; @@ -111,7 +112,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 c11efb09549641778ddf4b780e17ae89732bf9cf Mon Sep 17 00:00:00 2001 From: elena5100 <194572363+elena5100@users.noreply.github.com> Date: Wed, 1 Oct 2025 23:32:01 -0700 Subject: [PATCH 6/9] return number of levels in tree(levelCount) & return sum of nodes at given level(sumAtLevel) --- src/Practice.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index db6ac45..af77179 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -139,7 +139,12 @@ 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; + + int left = levelCount(root.left); + int right = levelCount(root.right); + + return 1 + Math.max(left, right); } @@ -167,7 +172,13 @@ 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; + if(root == null) return 0; + // at this level, return value + if(level == 1) return root.data; + + int sum = sumAtLevel(root.left, level -1) + sumAtLevel(root.right, level -1); + + return sum; } From e587fbd4b7817646853e315a47ff80be1354b799 Mon Sep 17 00:00:00 2001 From: elena5100 <194572363+elena5100@users.noreply.github.com> Date: Thu, 2 Oct 2025 00:34:30 -0700 Subject: [PATCH 7/9] return true if tree sum == list sum --- src/Practice.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index af77179..8358ca2 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -193,8 +193,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; + int treeTotal = treeSum(root); + int listTotal = listSum(head); + return treeTotal == listTotal; +} + +private static int treeSum(BinaryTreeNode root) { + if (root == null) return 0; // empty tree = 0 + return root.data + treeSum(root.left) + treeSum(root.right); +} + +private static int listSum(ListNode head) { + int sum = 0; + ListNode current = head; + while (current != null) { + sum += current.data; + current = current.next; } + return sum; +} + + /** * Returns the sum of all the vertices in a graph that are reachable from a given From 717c67fca0933ab503891ba3e883714636fb5957 Mon Sep 17 00:00:00 2001 From: elena5100 <194572363+elena5100@users.noreply.github.com> Date: Thu, 2 Oct 2025 01:20:12 -0700 Subject: [PATCH 8/9] return sum of all reachable vertices --- src/Practice.java | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 8358ca2..02e31bd 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,6 +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 { @@ -225,8 +227,22 @@ private static int listSum(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 dfsSum(start, visited); } + + private static int dfsSum(Vertex v, Set> visited) { + // already visited + if (!visited.add(v)) return 0; + int sum = v.data; + for (Vertex next : v.neighbors) { + sum += dfsSum(next, visited); + } + return sum; + + } /** * Returns the count of vertices in a graph that have an outdegree of 0. @@ -237,6 +253,6 @@ public static int graphSum(Vertex start) { * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; - } + + } \ No newline at end of file From a78b4f1115e36588dcf1edd26202767d02d4362a Mon Sep 17 00:00:00 2001 From: elena5100 <194572363+elena5100@users.noreply.github.com> Date: Thu, 2 Oct 2025 01:47:46 -0700 Subject: [PATCH 9/9] Add sinkCount method to count sink vertices in graph --- src/Practice.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 02e31bd..5b28e24 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -253,6 +253,21 @@ private static int dfsSum(Vertex v, Set> visited) { * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - + if (start == null) return 0; + + int count = 0; + Set> seen = new HashSet<>(); + Queue> skin1 = new LinkedList<>(); + skin1.add(start); + + while (!skin1.isEmpty()) { + Vertex node = skin1.poll(); + if (!seen.add(node)) continue; + + if (node.neighbors == null || node.neighbors.isEmpty()) count++; + else skin1.addAll(node.neighbors); + } + return count; +} } \ No newline at end of file