From 7c87494dd7c8dec6285c0cadeef3a9a81d04307d Mon Sep 17 00:00:00 2001 From: BradyLeg <184549450+BradyLeg@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:26:42 -0700 Subject: [PATCH 1/9] Completed oddSum method --- src/Practice.java | 48 +++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..d640bbf 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,7 +11,16 @@ 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 sum; + } + for (int i = 0; i < nums.length; i++) { + if (nums[i] % 2 != 0) { + sum += nums[i]; + } + } + return sum; } /** @@ -23,7 +32,7 @@ public static int oddSum(int[] nums) { * @param words a set of words * @return the shortest word in the set with a lexicographic tiebreaker * @throws IllegalArgumentException if words is empty - * @throws NullPointerException if words is null + * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { return null; @@ -54,15 +63,16 @@ public static int biggestNumber(ListNode head) { } /** - * Returns a frequency map counting how frequently items appear in a linked list. + * Returns a frequency map counting how frequently items appear in a linked + * list. * * Example: - * Input: a -> x -> a -> a -> x -> y - * Output: {a:3, x:2, y: 1} + * Input: a -> x -> a -> a -> x -> y + * Output: {a:3, x:2, y: 1} * * Returns an empty map if head is null * - * @param the type of data held by the list + * @param the type of data held by the list * @param head the head of the list * @return a frequency map of values in the list */ @@ -70,7 +80,6 @@ public static Map frequencies(ListNode head) { return null; } - /** * Returns the number of levels in the tree. * @@ -83,27 +92,26 @@ public static int levelCount(BinaryTreeNode root) { return 0; } - /** * Returns the sum at a specified level in a binary tree. * * For example, if the given level was 3: - * 5 - * / \ - * 8 4 - * / \ / - * 7 9 2 - * / - * 1 + * 5 + * / \ + * 8 4 + * / \ / + * 7 9 2 + * / + * 1 * * Nodes at level 3: 7, 9, and 2 - * Sum of nodes at level 3: 18 + * Sum of nodes at level 3: 18 * * The root is considered to be at level 1. * * Returns 0 if the tree is empty or if the level is not present in the tree. * - * @param root the root of the binary tree + * @param root the root of the binary tree * @param level the level to sum * @return the sum of the nodes at the given level */ @@ -111,10 +119,9 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { return 0; } - /** * Returns true if the sum of the values in a given tree is equal to the sum - * of the values in the given list. + * of the values in the given list. * * An empty tree or list is considered to have a sum of 0. * @@ -127,7 +134,8 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h } /** - * Returns the sum of all the vertices in a graph that are reachable from a given + * Returns the sum of all the vertices in a graph that are reachable from a + * given * starting vertex. * * Returns 0 if the starting vertex is null. From c98057dac2a86b52fbe1c14187eb6e2ee255359d Mon Sep 17 00:00:00 2001 From: BradyLeg <184549450+BradyLeg@users.noreply.github.com> Date: Tue, 23 Sep 2025 12:21:26 -0700 Subject: [PATCH 2/9] Completed shortestWord method --- src/Practice.java | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index d640bbf..52f2b29 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -35,7 +35,32 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + if (words.isEmpty()) { + throw new IllegalArgumentException(); + } + String shortWord = ""; + for (String word : words) { + if (word == null) { + throw new NullPointerException(); + } + if (shortWord.equals("") || word.length() < shortWord.length()) { + shortWord = word; + } else if (word.length() == shortWord.length()) { + + for (int i = 0; i < word.length(); i++) { + + if (word.charAt(i) != shortWord.charAt(i)) { + + if ((int) word.charAt(i) < (int) shortWord.charAt(i)) { + shortWord = word; + } + break; + } + } + } + } + + return shortWord; } /** From 38b73cf2e72c8d6f46f66d6e98534561432f5595 Mon Sep 17 00:00:00 2001 From: BradyLeg <184549450+BradyLeg@users.noreply.github.com> Date: Tue, 23 Sep 2025 12:30:56 -0700 Subject: [PATCH 3/9] Completed adults method --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 52f2b29..b70c08e 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; @@ -73,7 +74,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 nameSet = new HashSet<>(); + for (String name : ages.keySet()) { + if (ages.get(name) >= 18) { + nameSet.add(name); + } + } + return nameSet; } /** From e8f6ce2526c60446dd09a7b2ae0d442d2bb0c8b2 Mon Sep 17 00:00:00 2001 From: BradyLeg <184549450+BradyLeg@users.noreply.github.com> Date: Wed, 24 Sep 2025 14:21:52 -0700 Subject: [PATCH 4/9] Completed biggestNumber method --- src/Practice.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index b70c08e..580804c 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -94,7 +94,19 @@ 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 biggest = Integer.MIN_VALUE; + ListNode current = head; + while (current != null) { + if (current.data > biggest) { + biggest = current.data; + } + current = current.next; + } + return biggest; } /** From 21c2f371a0f27a9aa2c2aca387217f27243f26d2 Mon Sep 17 00:00:00 2001 From: BradyLeg <184549450+BradyLeg@users.noreply.github.com> Date: Wed, 24 Sep 2025 14:40:47 -0700 Subject: [PATCH 5/9] Completed frequencies method --- src/Practice.java | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 580804c..9324504 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; @@ -124,7 +125,24 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map frequencyMap = new HashMap<>(); + if (head == null) { + return frequencyMap; + } + + ListNode current = head; + while (current != null) { + if (frequencyMap.containsKey(current.data)) { + int count = frequencyMap.get(current.data); + count += 1; + frequencyMap.put(current.data, count); + } else { + frequencyMap.put(current.data, 1); + } + current = current.next; + } + + return frequencyMap; } /** From d9b31a90f3dc79a2432238e1cc28ea9fee431b57 Mon Sep 17 00:00:00 2001 From: BradyLeg <184549450+BradyLeg@users.noreply.github.com> Date: Wed, 24 Sep 2025 14:56:06 -0700 Subject: [PATCH 6/9] Completed levelCount method --- src/Practice.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 9324504..8e337fc 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -154,7 +154,16 @@ public static Map frequencies(ListNode head) { * @return the number of levels in the tree */ public static int levelCount(BinaryTreeNode root) { - return 0; + int levels = 0; + if (root == null) { + return levels; + } + levels++; + + int maxDepth = Math.max(levelCount(root.left), levelCount(root.right)); + levels += maxDepth; + + return levels; } /** From d90e730b0a212bdf0ae6dcffac820591f219ecfb Mon Sep 17 00:00:00 2001 From: BradyLeg <184549450+BradyLeg@users.noreply.github.com> Date: Wed, 24 Sep 2025 15:10:04 -0700 Subject: [PATCH 7/9] Completed sumAtLevel method --- src/Practice.java | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 8e337fc..547baab 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -190,7 +190,28 @@ 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; + } + + return sumAtLevel(root, level, 1); + } + + public static int sumAtLevel(BinaryTreeNode root, int level, int currentLevel) { + int sum = 0; + if (root == null) { + return sum; + } + + if (currentLevel == level) { + sum = root.data; + } + + currentLevel++; + sum += sumAtLevel(root.left, level, currentLevel); + sum += sumAtLevel(root.right, level, currentLevel); + + return sum; } /** From 027cf44b009f6f86989deaea0128c7c0830e245a Mon Sep 17 00:00:00 2001 From: BradyLeg <184549450+BradyLeg@users.noreply.github.com> Date: Wed, 24 Sep 2025 15:25:39 -0700 Subject: [PATCH 8/9] Completed sumMatch method --- src/Practice.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index 547baab..6723bbc 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -225,9 +225,37 @@ public static int sumAtLevel(BinaryTreeNode root, int level, int curren * @return true if the sums are equal, false otherwise */ public static boolean sumMatch(BinaryTreeNode root, ListNode head) { + int lSum = 0; + int tSum = 0; + if (head != null) { + ListNode current = head; + while (current != null) { + lSum += current.data; + current = current.next; + } + } + if (root != null) { + tSum = treeSum(root); + } + + if (lSum == tSum) { + return true; + } return false; } + public static int treeSum(BinaryTreeNode root) { + int sum = 0; + if (root == null) { + return sum; + } + + sum = root.data; + sum += treeSum(root.left); + sum += treeSum(root.right); + return sum; + } + /** * Returns the sum of all the vertices in a graph that are reachable from a * given From 389920f367c1577ce7993199286324e74e407b36 Mon Sep 17 00:00:00 2001 From: BradyLeg <184549450+BradyLeg@users.noreply.github.com> Date: Wed, 24 Sep 2025 16:26:28 -0700 Subject: [PATCH 9/9] Completed graphSum and sinkCount methods --- src/Practice.java | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 6723bbc..8e6181b 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -267,7 +267,23 @@ public static int treeSum(BinaryTreeNode root) { * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + Set> visited = new HashSet<>(); + return graphSum(start, visited); + } + + public static int graphSum(Vertex start, Set> visited) { + int sum = 0; + if (start == null || visited.contains(start)) { + return sum; + } + + visited.add(start); + sum += start.data; + for (Vertex neighbor : start.neighbors) { + sum += graphSum(neighbor, visited); + } + + return sum; } /** @@ -279,6 +295,26 @@ 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 start, Set> visited) { + int total = 0; + if (start == null || visited.contains(start)) { + return 0; + } + + visited.add(start); + if (start.neighbors.isEmpty()) { + total++; + } + for (Vertex neighbor : start.neighbors) { + total += sinkCount(neighbor, visited); + } + return total; } } \ No newline at end of file