From a7af6dc3a3f7642d35ef5b0b4ead0d5e032e8816 Mon Sep 17 00:00:00 2001 From: Gabby-Moon <194023457+Gabby-Moon@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:26:02 -0700 Subject: [PATCH 1/8] Finished oddSum not tested --- src/Practice.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..de12b09 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.length == 0){ + return 0; + } + for(int i = 0; i < nums.length; i++) { + if(nums[i] % 2 == 1) { + sum += nums[i]; + } + } + return sum; } /** From 15b6496f7bda74450c091b09020d21ae19798fb1 Mon Sep 17 00:00:00 2001 From: Gabby-Moon <194023457+Gabby-Moon@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:42:59 -0700 Subject: [PATCH 2/8] Finished shortestWord, untested --- src/Practice.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index de12b09..5e2ba4a 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -35,7 +35,26 @@ 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 string : words) { + if(string == null){ + throw new NullPointerException(); + } + if(string.length() < shortWord.length()){ + shortWord = string; + } + else if(string.length() == shortWord.length()) { + for (int i = 0; i < string.length(); i++) { + if((int)string.charAt(i) < (int)shortWord.charAt(i)){ + shortWord = string; + } + } + } + } + return shortWord; } /** From 4788c9ffbb9bb8098b5b6133847cf06e3451867e Mon Sep 17 00:00:00 2001 From: Gabby-Moon <194023457+Gabby-Moon@users.noreply.github.com> Date: Tue, 23 Sep 2025 12:18:24 -0700 Subject: [PATCH 3/8] oddSum and shortestWord now pass tests --- src/Practice.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 5e2ba4a..79dd55f 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -12,11 +12,11 @@ public class Practice { */ public static int oddSum(int[] nums) { int sum = 0; - if(nums.length == 0){ + if(nums == null){ return 0; } for(int i = 0; i < nums.length; i++) { - if(nums[i] % 2 == 1) { + if(nums[i] % 2 != 0) { sum += nums[i]; } } @@ -38,7 +38,7 @@ public static String shortestWord(Set words) { if(words.isEmpty()){ throw new IllegalArgumentException(); } - String shortWord = ""; + String shortWord = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; for (String string : words) { if(string == null){ throw new NullPointerException(); @@ -48,9 +48,13 @@ public static String shortestWord(Set words) { } else if(string.length() == shortWord.length()) { for (int i = 0; i < string.length(); i++) { - if((int)string.charAt(i) < (int)shortWord.charAt(i)){ - shortWord = string; + if(string.charAt(i) != shortWord.charAt(i)){ + if((int) string.charAt(i) < (int) shortWord.charAt(i)){ + shortWord = string; + } + break; } + continue; } } } @@ -67,6 +71,10 @@ else if(string.length() == shortWord.length()) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { + if(ages.isEmpty()) { + throw new NullPointerException(); + } + return null; } From bdc6b7eadff76bbad95f2112285efcc48920bd57 Mon Sep 17 00:00:00 2001 From: Gabby-Moon <194023457+Gabby-Moon@users.noreply.github.com> Date: Tue, 23 Sep 2025 12:28:20 -0700 Subject: [PATCH 4/8] Finished biggestNumber and passed --- src/Practice.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 79dd55f..c686f46 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -86,7 +86,18 @@ public static Set adults(Map ages) { * @throws IllegalArgumentException if head is null */ public static int biggestNumber(ListNode head) { - return 0; + if(head == null || head.data == null){ + throw new IllegalArgumentException(); + } + int largest = head.data; + int prevLarge = Integer.MIN_VALUE; + if(head.next != null){ + prevLarge = biggestNumber(head.next); + } + if(largest < prevLarge){ + largest = prevLarge; + } + return largest; } /** From d6b4900555ba2ff095d521f584189afa89a1c618 Mon Sep 17 00:00:00 2001 From: Gabby-Moon <194023457+Gabby-Moon@users.noreply.github.com> Date: Wed, 24 Sep 2025 15:30:58 -0700 Subject: [PATCH 5/8] Finished frequencies and passed tests --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index c686f46..5218e2b 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -114,7 +115,19 @@ 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<>(); + frequenciesHelper(head, frequency); + return frequency; + } + public static void frequenciesHelper(ListNode head, Map frequency) { + if(head == null) return; + if(!frequency.containsKey(head.data)) { + frequency.put(head.data, 1); + } + else{ + frequency.put(head.data, frequency.get(head.data) + 1); + } + frequenciesHelper(head.next, frequency); } From 346280c8e10d9ef239f67572f91f0d7a46c84b22 Mon Sep 17 00:00:00 2001 From: Gabby-Moon <194023457+Gabby-Moon@users.noreply.github.com> Date: Wed, 24 Sep 2025 20:43:10 -0700 Subject: [PATCH 6/8] Finished graphSum and passed test --- src/Practice.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 5218e2b..ddc4ddc 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,4 +1,6 @@ import java.util.HashMap; +import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -196,7 +198,22 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + Set> used = new HashSet<>(); + if(start == null) return 0; + int sum = start.data; + used.add(start); + sum = graphSumHelper(start.neighbors, sum, used); + return sum; + } + public static int graphSumHelper(List> nums, int sum, Set> used) { + for (Vertex num : nums) { + if(!used.contains(num)){ + sum += num.data; + used.add(num); + sum += graphSumHelper(num.neighbors, 0, used); + } + } + return sum; } /** From a07ed5cb6b96ad5ed08984b5fb0f08747a5983f4 Mon Sep 17 00:00:00 2001 From: Gabby-Moon <194023457+Gabby-Moon@users.noreply.github.com> Date: Wed, 24 Sep 2025 20:58:28 -0700 Subject: [PATCH 7/8] Finished sinkCount and passed tests --- src/Practice.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ddc4ddc..f9b9802 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -225,6 +225,25 @@ public static int graphSumHelper(List> nums, int sum, Set start) { - return 0; + if(start == null) return 0; + Set> used = new HashSet<>(); + used.add(start); + int total = sinkCountHelper(start.neighbors, used); + return total; + } + public static int sinkCountHelper(List> nums, Set> used) { + int total = 0; + // Have to use is empty + // doesn't recognize == null (IDK why) + if(nums.isEmpty()){ + total = 1; + } + for (Vertex vertex : nums) { + if(!used.contains(vertex)){ + used.add(vertex); + total += sinkCountHelper(vertex.neighbors, used); + } + } + return total; } } \ No newline at end of file From 7184721e95b48af68056728da57ca4ef9a9c61f3 Mon Sep 17 00:00:00 2001 From: Gabby-Moon <194023457+Gabby-Moon@users.noreply.github.com> Date: Wed, 24 Sep 2025 21:02:46 -0700 Subject: [PATCH 8/8] Added some comments --- src/Practice.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index f9b9802..bf7484c 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -41,6 +41,7 @@ public static String shortestWord(Set words) { if(words.isEmpty()){ throw new IllegalArgumentException(); } + // unsure how to do this without a long word at the start String shortWord = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; for (String string : words) { if(string == null){ @@ -202,6 +203,7 @@ public static int graphSum(Vertex start) { if(start == null) return 0; int sum = start.data; used.add(start); + // maybe error if .neighbors is empty sum = graphSumHelper(start.neighbors, sum, used); return sum; } @@ -210,6 +212,8 @@ public static int graphSumHelper(List> nums, int sum, Set