From f958b310b0b2130b3143acc16bd8becd5f7d3ab4 Mon Sep 17 00:00:00 2001 From: DevRudy <55453823+SketchRudy@users.noreply.github.com> Date: Wed, 24 Sep 2025 21:04:40 -0700 Subject: [PATCH 1/7] Implement oddSum and shortestWord methods with proper logic and error handling --- src/Practice.java | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..d403a3f 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,7 +11,17 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + int oddTotal = 0; + if (nums == null || nums.length == 0) { + return 0; + } + + for (int i: nums) { + if (i % 2 != 0) { + oddTotal += i; + } + } + return oddTotal; } /** @@ -26,7 +36,23 @@ 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("Words is null"); + } + if (words.isEmpty()) { + throw new IllegalArgumentException("Words is empty"); + } + + String shortest = ""; + for (String i : words) { + if (i.length() < shortest.length()) { + shortest = i; + } + else if (i.length() == shortest.length() && i.compareTo(shortest) < 0) { + shortest = i; + } + } + return shortest; } /** From b5884015b043cb1eec7aa0b49b0ae7cfe44cda72 Mon Sep 17 00:00:00 2001 From: DevRudy <55453823+SketchRudy@users.noreply.github.com> Date: Thu, 25 Sep 2025 00:52:12 -0700 Subject: [PATCH 2/7] Finished function that returns adults older than 18 --- src/Practice.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index d403a3f..6fe1ecb 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,6 @@ import java.util.Map; import java.util.Set; +import java.util.HashSet; public class Practice { /** @@ -43,7 +44,7 @@ public static String shortestWord(Set words) { throw new IllegalArgumentException("Words is empty"); } - String shortest = ""; + String shortest = words.iterator().next(); for (String i : words) { if (i.length() < shortest.length()) { shortest = i; @@ -65,7 +66,18 @@ else if (i.length() == shortest.length() && i.compareTo(shortest) < 0) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { - return null; + if (ages == null) { + throw new NullPointerException("no age"); + } + + Set result = new HashSet<>(); + for (Map.Entry entry : ages.entrySet()) { + Integer age = entry.getValue(); + if (age != null && age >= 18) { + result.add(entry.getKey()); + } + } + return result; } /** From b3e857ca7998e17937205905235dfb2e69d72c2e Mon Sep 17 00:00:00 2001 From: DevRudy <55453823+SketchRudy@users.noreply.github.com> Date: Thu, 25 Sep 2025 01:02:44 -0700 Subject: [PATCH 3/7] Implement error handling and logic for biggestNumber and frequencies methods --- src/Practice.java | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 6fe1ecb..7556f28 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,6 @@ import java.util.Map; import java.util.Set; +import java.util.HashMap; import java.util.HashSet; public class Practice { @@ -88,6 +89,19 @@ public static Set adults(Map ages) { * @throws IllegalArgumentException if head is null */ public static int biggestNumber(ListNode head) { + if (head == null) { + throw new IllegalArgumentException("Head is null"); + } + + int max = head.data; + ListNode curr = head.next; + + while (curr != null) { + if (curr.data > max) { + max = curr.data; + } + curr = curr.next; + } return 0; } @@ -105,7 +119,15 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map freq = new HashMap<>(); + ListNode curr = head; + + while (curr != null) { + T val = curr.data; + freq.put(val, freq.getOrDefault(val, 0) + 1); + curr = curr.next; + } + return freq; } From 8447b5c881d25eab106a58e713d61a451a42d791 Mon Sep 17 00:00:00 2001 From: DevRudy <55453823+SketchRudy@users.noreply.github.com> Date: Thu, 25 Sep 2025 01:05:35 -0700 Subject: [PATCH 4/7] Implement logic for frequency map, level count, and sum at level methods --- src/Practice.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 7556f28..a866b59 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -126,7 +126,8 @@ public static Map frequencies(ListNode head) { T val = curr.data; freq.put(val, freq.getOrDefault(val, 0) + 1); curr = curr.next; - } + } + return freq; } @@ -140,7 +141,11 @@ public static Map frequencies(ListNode head) { * @return the number of levels in the tree */ public static int levelCount(BinaryTreeNode root) { + if (root == null) { return 0; + } + + return 1 + Math.max(levelCount(root.left), levelCount(root.right)); } @@ -168,7 +173,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 || level < 1) { + return 0; + } + if (level == 1) { + return root.data; + } + return sumAtLevel(root.left, level - 1) + sumAtLevel(root.right, level - 1); } From 0ae1e71202568888f8526170de8a3824dfe19bc8 Mon Sep 17 00:00:00 2001 From: DevRudy <55453823+SketchRudy@users.noreply.github.com> Date: Thu, 25 Sep 2025 01:09:38 -0700 Subject: [PATCH 5/7] Implement sumMatch method and helper functions for tree and list sums --- src/Practice.java | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index a866b59..4d268e4 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,7 @@ import java.util.Map; import java.util.Set; +import java.util.ArrayDeque; +import java.util.Deque; import java.util.HashMap; import java.util.HashSet; @@ -194,9 +196,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; + return treeSum(root) == listSum(head);; } + private static int treeSum(BinaryTreeNode node) { + if (node == null) return 0; + + int v = node.data; + + return v + treeSum(node.left) + treeSum(node.right); +} + + private static int listSum(ListNode node) { + int total = 0; + ListNode curr = node; + while (curr != null) { + total += curr.data; // use curr.data if that's your field + curr = curr.next; + } + return total; +} + /** * Returns the sum of all the vertices in a graph that are reachable from a given * starting vertex. @@ -207,7 +227,12 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + if (start == null) { + return 0; + } + + int sum = 0; + } /** From e942d36ac9b4b40ac5185d53be9e1738c03d4281 Mon Sep 17 00:00:00 2001 From: DevRudy <55453823+SketchRudy@users.noreply.github.com> Date: Thu, 25 Sep 2025 01:32:51 -0700 Subject: [PATCH 6/7] Refactor sumMatch method and implement sinkCount method with breadth-first search for vertex counting --- src/Practice.java | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 4d268e4..2abc3e5 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,9 +1,12 @@ import java.util.Map; +import java.util.Queue; import java.util.Set; import java.util.ArrayDeque; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; public class Practice { /** @@ -196,7 +199,7 @@ 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 treeSum(root) == listSum(head);; + return treeSum(root) == listSum(head); } private static int treeSum(BinaryTreeNode node) { @@ -232,7 +235,22 @@ public static int graphSum(Vertex start) { } int sum = 0; + Set> seen = new HashSet<>(); + Queue> queue = new LinkedList<>(); + queue.add(start); + seen.add(start); + while (!queue.isEmpty()) { + Vertex v = queue.poll(); + sum += v.data; + + for (Vertex n : v.neighbors) { + if (n != null && seen.add(n)) { + queue.add(n); + } + } + } + return sum; } /** @@ -244,6 +262,28 @@ public static int graphSum(Vertex start) { * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { + if (start == null) return 0; + + int sinks = 0; + Set> seen = new HashSet<>(); + Queue> q = new LinkedList<>(); + q.add(start); + seen.add(start); + + while (!q.isEmpty()) { + Vertex v = q.poll(); + List> neighbor = v.neighbors; + + if (neighbor == null || neighbor.isEmpty()) { + sinks++; + } else { + for (Vertex n : neighbor) { + if (n != null && seen.add(n)) { + q.add(n); + } + } + } + } return 0; } } \ No newline at end of file From 0509423b83936b0f427a134f58febc00632fdbeb Mon Sep 17 00:00:00 2001 From: DevRudy <55453823+SketchRudy@users.noreply.github.com> Date: Thu, 25 Sep 2025 01:33:11 -0700 Subject: [PATCH 7/7] Return the count of sinks instead of 0 in the Practice class --- src/Practice.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 2abc3e5..bfeeef7 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -284,6 +284,6 @@ public static int sinkCount(Vertex start) { } } } - return 0; + return sinks; } } \ No newline at end of file