From bbc867a9ab47934fd8597aad166a6fb7a6819e02 Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Tue, 23 Sep 2025 11:32:13 -0700 Subject: [PATCH 01/11] Completed oddSum exercise --- src/Practice.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..54e7d10 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,7 +11,20 @@ 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 num : nums) + { + if (num % 2 != 0) + { + sum += num; + } + } + return sum; } /** @@ -20,7 +33,7 @@ public static int oddSum(int[] nums) { * If multiple words are tied for shortest, returns the one that is smallest * lexicographically. * - * @param words a set of words + * @param words a set of word * @return the shortest word in the set with a lexicographic tiebreaker * @throws IllegalArgumentException if words is empty * @throws NullPointerException if words is null From c7ea16307086e26855a7b76ce38d86bbd2bf1d1a Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Wed, 24 Sep 2025 13:33:02 -0700 Subject: [PATCH 02/11] Completed some of the shortestWord exercise --- src/Practice.java | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 54e7d10..9b578ae 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -39,7 +39,25 @@ 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(); + } + else if (words.isEmpty()) + { + throw new IllegalArgumentException(); + } + + String shortest = ""; + for (String word : words) { + if (shortest == null || word.length() < shortest.length()) { + shortest = word; + } + // else if (word.length() == shortest.length() && word.compareTo(shortest) < 0) { + // shortest = word; + // } + } + return shortest; } /** From 2ff26b66d769061a3ab3d81adf580842deacfe58 Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Wed, 24 Sep 2025 14:36:23 -0700 Subject: [PATCH 03/11] Completed shortestWord --- src/Practice.java | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 9b578ae..e6abade 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -12,15 +12,11 @@ public class Practice { */ public static int oddSum(int[] nums) { int sum = 0; - - if (nums == null) - { + if (nums == null) { return 0; } - for (int num : nums) - { - if (num % 2 != 0) - { + for (int num : nums) { + if (num % 2 != 0){ sum += num; } } @@ -39,23 +35,19 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - if (words == null) - { + if (words == null) { throw new NullPointerException(); } - else if (words.isEmpty()) - { + if (words.isEmpty()) { throw new IllegalArgumentException(); } - String shortest = ""; + String shortest = null; for (String word : words) { - if (shortest == null || word.length() < shortest.length()) { + if (shortest == null || word.length() < shortest.length() + || (word.length() == shortest.length() && word.compareTo(shortest) < 0)) { shortest = word; } - // else if (word.length() == shortest.length() && word.compareTo(shortest) < 0) { - // shortest = word; - // } } return shortest; } From 0de5ca9d67760c7a670309460a4ebd09acdbd42d Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Wed, 24 Sep 2025 14:48:11 -0700 Subject: [PATCH 04/11] Completed adults --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index e6abade..3ebfbc6 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,19 @@ 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()) { + Integer age = ages.get(name); + + if (age >= 18) { + result.add(name); + } + } + return result; } /** From fba4b8d7ffcbb9e74a86cb34e514aba0d4665571 Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Wed, 24 Sep 2025 15:17:22 -0700 Subject: [PATCH 05/11] Completed biggestNumber --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 3ebfbc6..269a968 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -86,7 +86,20 @@ 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 = head.data; + ListNode current = head.next; + + while (current != null) { + if (current.data > biggest) { + biggest = current.data; + } + current = current.next; + } + return biggest; } /** From 642a999f7db1e1c227f923b1348a2fc093cde9fb Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Wed, 24 Sep 2025 15:45:18 -0700 Subject: [PATCH 06/11] completed frequencies --- src/Practice.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 269a968..e42c545 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; @@ -116,7 +117,22 @@ 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, frequency.get(data) + 1); + } else { + frequency.put(data, 1); + } + current = current.next; + } + return frequency; } From b66de3e64a2b9cebdcadf6080d60cca1070d3b5f Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Wed, 24 Sep 2025 15:52:18 -0700 Subject: [PATCH 07/11] completed levelCount --- src/Practice.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index e42c545..947a024 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -145,7 +145,14 @@ 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 Math.max(left, right) + 1; } From 3b4ff73ef41da162cff8314815eef83d238ad33e Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Wed, 24 Sep 2025 16:23:30 -0700 Subject: [PATCH 08/11] completed sumAtLevel --- src/Practice.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 947a024..600f108 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -180,7 +180,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; + if (root == null) { + return 0; + } + + if (level == 1) { + return root.data; + } + + int left = sumAtLevel(root.left, level - 1); + int right = sumAtLevel(root.right, level - 1); + + return left + right; } From 0764c7d3cfe821550451dd5e408a0f9e602fb9d5 Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Wed, 24 Sep 2025 16:53:11 -0700 Subject: [PATCH 09/11] completed sunMatch --- src/Practice.java | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 600f108..4ef0ed0 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -206,7 +206,32 @@ 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 treeSum = 0; + + if (root != null) { + treeSum = root.data; + if (root.left != null) { + treeSum += sumMatchHelper(root.left); + } + if (root.right != null) { + treeSum += sumMatchHelper(root.right); + } + } + + int listSum = 0; + ListNode current = head; + while (current != null) { + listSum += current.data; + current = current.next; + } + return treeSum == listSum; + } + + public static int sumMatchHelper(BinaryTreeNode node) { + if (node == null) { + return 0; + } + return node.data + sumMatchHelper(node.left) + sumMatchHelper(node.right); } /** From 61656e3094be6c8335b9eeec55c971035b6d81a6 Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Wed, 24 Sep 2025 21:27:23 -0700 Subject: [PATCH 10/11] completed graphSum --- src/Practice.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 4ef0ed0..7711b3b 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -244,7 +244,24 @@ public static int sumMatchHelper(BinaryTreeNode node) { * @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 graphSumHelper(start, visited); + } + + public static int graphSumHelper(Vertex current, Set> visited) { + if (visited.contains(current)) { + return 0; + } + visited.add(current); + + int sum = current.data; + for (Vertex neighbor : current.neighbors) { + sum += graphSumHelper(neighbor, visited); + } + return sum; } /** From 04a8ed9411ae84e5f5008071b563357aa71a21e0 Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Wed, 24 Sep 2025 21:51:54 -0700 Subject: [PATCH 11/11] completed sinkCount --- src/Practice.java | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 7711b3b..3c0335a 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -273,6 +273,29 @@ public static int graphSumHelper(Vertex current, Set> v * @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 sinkCountHelper(start, visited); + } + + public static int sinkCountHelper(Vertex current, Set> visited) { + // visited vertex + if (visited.contains(current)) { + return 0; + } + visited.add(current); + + int count = 0; + if (current.neighbors.isEmpty()) { + count = 1; + } + + for (Vertex neighbor : current.neighbors) { + count += sinkCountHelper(neighbor, visited); + } + return count; } } \ No newline at end of file