From fda95fab557debfa9f093cfc7bc5a79c0e84a3f2 Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Tue, 23 Sep 2025 12:07:08 -0700 Subject: [PATCH 01/10] Completed oddSumm and passed all tests. --- src/Practice.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..60d0b31 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -10,8 +10,23 @@ public class Practice { * @param nums an array of numbers * @return the sum of the odd numbers in the array */ - public static int oddSum(int[] nums) { - return 0; + public static int oddSum(int[] nums) + { + // Set up sum variable + int odds = 0; + + // Null handling + if (nums == null) return odds; + + // Loop through each item in the array + for (int i = 0; i < nums.length; i++) + { + // If odd, add amount to the variable + if (nums[i] % 2 != 0) odds += nums[i]; + } + + // Return the sum of the odds in the array + return odds; } /** From 656a9c24dcb864d0116a9cc99e39d2e154885b8c Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Tue, 23 Sep 2025 12:31:44 -0700 Subject: [PATCH 02/10] Completed shortestWord, and passed all tests. --- src/Practice.java | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 60d0b31..1d3c8a1 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -40,8 +40,43 @@ public static int oddSum(int[] nums) * @throws IllegalArgumentException if words is empty * @throws NullPointerException if words is null */ - public static String shortestWord(Set words) { - return null; + public static String shortestWord(Set words) + { + // Error handling + if (words.isEmpty()) throw new IllegalArgumentException(); + if (words == null) throw new NullPointerException(); + + // Set up shortest word variable + String shortWord = ""; + + // Go through each word in the set + for (String word : words) + { + // If empty, initialize first word into variable + if (shortWord.equals("")) shortWord = word; + // If shorter, change variable to current word + else if (word.length() < shortWord.length()) shortWord = word; + // If same length, compare individual letters lexicographically + else if (word.length() == shortWord.length()) + { + // Cycle through each letter in the word + for (int i = 0; i < word.length(); i++) + { + // If larger, keep old word and stop checking + if (word.charAt(i) > shortWord.charAt(i)) break; + // If smaller, replace with new word and stop checking + if (word.charAt(i) < shortWord.charAt(i)) + { + shortWord = word; + break; + } + // Otherwise, keep checking + } + } + } + + // Return shortest word + return shortWord; } /** From a1ac7478bdd5f5ba9919ec0a976205f8dafbd3c1 Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Tue, 23 Sep 2025 16:24:13 -0700 Subject: [PATCH 03/10] Completed adults, and passed all tests. --- src/Practice.java | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 1d3c8a1..7118065 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,7 +1,9 @@ +import java.util.HashSet; import java.util.Map; import java.util.Set; -public class Practice { +public class Practice +{ /** * Returns the sum of the odd numbers in the array. * @@ -27,6 +29,7 @@ public static int oddSum(int[] nums) // Return the sum of the odds in the array return odds; + } /** @@ -77,6 +80,7 @@ else if (word.length() == shortWord.length()) // Return shortest word return shortWord; + } /** @@ -88,8 +92,23 @@ else if (word.length() == shortWord.length()) * @return the set of all names of people >= 18 years old * @throws NullPointerException if ages is null */ - public static Set adults(Map ages) { - return null; + public static Set adults(Map ages) + { + // Set up a variable to hold a set of names + Set adultList = new HashSet(); + + // For each name in the map + for (String name : ages.keySet()) + { + // If the age is null throw an exception + if (ages.get(name) == null) throw new NullPointerException(); + // Otherwise, if 18 or over, add name to the adultList + else if (ages.get(name) >= 18) adultList.add(name); + } + + // Return the set + return adultList; + } /** From 9c7419854a16fe4530e12a48e81298f8fc85a62a Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Tue, 23 Sep 2025 16:41:29 -0700 Subject: [PATCH 04/10] Completed biggestNumber, and passed all tests. --- src/Practice.java | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 7118065..dae1baa 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -118,8 +118,28 @@ public static Set adults(Map ages) * @return the biggest number in the list * @throws IllegalArgumentException if head is null */ - public static int biggestNumber(ListNode head) { - return 0; + public static int biggestNumber(ListNode head) + { + // Throw exception, if head is null + if (head == null) throw new IllegalArgumentException(); + + // Set up a new node that can cycle through the list + ListNode current = head; + // Initialize the biggest number variable with the current number + int bigNum = current.data; + + // Cycle through + while (current.next != null) + { + // Set new node spot + current = current.next; + // If the current number is bigger than the value in bigNum, use the current number + if (current.data > bigNum) bigNum = current.data; + } + + // Return the biggest number + return bigNum; + } /** From f92699be51fdf6dac6abd4a49feffacd99a34e82 Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Tue, 23 Sep 2025 17:04:42 -0700 Subject: [PATCH 05/10] Completed frequencies, and passed all tests. --- src/Practice.java | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index dae1baa..4d1fe06 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; @@ -155,8 +156,35 @@ public static int biggestNumber(ListNode head) * @param head the head of the list * @return a frequency map of values in the list */ - public static Map frequencies(ListNode head) { - return null; + public static Map frequencies(ListNode head) + { + // Create Map variable to hold the frequencies + Map freqMap = new HashMap(); + + // Null error handling + if (head == null) return freqMap; + + // Set up temp node to cycle through the list + ListNode current = head; + + // Add the first node data to the map + freqMap.put(current.data, 1); + + // Cycle through the node list + while (current.next != null) + { + // Go to next node + current = current.next; + + //If the key doesn't exist, add key with a count of 1 + if (!freqMap.containsKey(current.data)) freqMap.put(current.data, 1); + // If key already exists, increment the value of said key + else freqMap.put(current.data, freqMap.get(current.data) + 1); + } + + // Return the map of frequencies + return freqMap; + } From a32c8a4ea4db5c14e24ade2d78962029d923c4af Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Tue, 23 Sep 2025 17:33:56 -0700 Subject: [PATCH 06/10] Completed levelCount and passed all tests. --- src/Practice.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 4d1fe06..4578048 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -175,7 +175,7 @@ public static Map frequencies(ListNode head) { // Go to next node current = current.next; - + //If the key doesn't exist, add key with a count of 1 if (!freqMap.containsKey(current.data)) freqMap.put(current.data, 1); // If key already exists, increment the value of said key @@ -196,8 +196,20 @@ public static Map frequencies(ListNode head) * @param root the root of the tree * @return the number of levels in the tree */ - public static int levelCount(BinaryTreeNode root) { - return 0; + public static int levelCount(BinaryTreeNode root) + { + // If null return 0 + if (root == null) return 0; + + // Check left, incrementing each level + int levelLeft = levelCount(root.left) + 1; + // Check right, incrementing each level + int levelRight = levelCount(root.right) + 1; + + // Compare left and right directions, and return the largest level + if (levelLeft > levelRight) return levelLeft; + else return levelRight; + } From af2d49d8deefafdff53aa5620e779a28792521eb Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Tue, 23 Sep 2025 18:03:32 -0700 Subject: [PATCH 07/10] Completed sumAtLevel and passed all tests. --- src/Practice.java | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 4578048..c735dd8 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -209,7 +209,7 @@ public static int levelCount(BinaryTreeNode root) // Compare left and right directions, and return the largest level if (levelLeft > levelRight) return levelLeft; else return levelRight; - + } @@ -236,8 +236,33 @@ public static int levelCount(BinaryTreeNode root) * @param level the level to sum * @return the sum of the nodes at the given level */ - public static int sumAtLevel(BinaryTreeNode root, int level) { - return 0; + public static int sumAtLevel(BinaryTreeNode root, int level) + { + // Call helper method and return result + return sumAtLevel(root, level, 0); + } + + public static int sumAtLevel(BinaryTreeNode root, int level, int sum) + { + // If null increment the level back up and return the sum + if (root == null) + { + level++; + return sum; + } + + // Decrement the level + level--; + // When the level is zero, add the data to the sum + if (level == 0) sum += root.data; + + // Traverse left, then right + int sumLeft = sumAtLevel(root.left, level, sum); + int sumRight = sumAtLevel(root.right, level, sumLeft); + + // Return the overall sum + return sumRight; + } From 0206b4d50e0d8698d0cdf61b210dab88a4389e92 Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Tue, 23 Sep 2025 18:54:48 -0700 Subject: [PATCH 08/10] Complete sumMatch, and passed all tests. --- src/Practice.java | 53 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index c735dd8..669a4cc 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -276,10 +276,59 @@ public static int sumAtLevel(BinaryTreeNode root, int level, int sum) * @param head The head of the linked list * @return true if the sums are equal, false otherwise */ - public static boolean sumMatch(BinaryTreeNode root, ListNode head) { - return false; + public static boolean sumMatch(BinaryTreeNode root, ListNode head) + { + // Call a method to calculate and return the sum of the ListNode + int listTotal = listSum(head); + // Call a method to calculate and return the sum of the tree + int treeTotal = treeSum(root, 0); + // Compare the totals, if matching return true, else return false + if (listTotal == treeTotal) return true; + else return false; + } + + public static int listSum(ListNode head) + { + // Set up the variable to hold the sum within the list + int listTotal = 0; + // If null return current total + if (head == null) return listTotal; + // Set up a node to cycle through the list + ListNode current = head; + // Add the current data to the total + listTotal += current.data; + + // Cycle through the Listnode + while (current.next != null) + { + // Go to next node + current = current.next; + // Add the current data to the total + listTotal += current.data; + } + + // Return the overall total + return listTotal; + + } + + public static int treeSum(BinaryTreeNode root, int total) + { + // If null, return total + if (root == null) return total; + // Add the current data to the total + total += root.data; + + // Cycle through the tree left, then right + int leftTotal = treeSum(root.left, total); + int rightTotal = treeSum(root.right, leftTotal); + + // Return the overall total + return rightTotal; + } + /** * Returns the sum of all the vertices in a graph that are reachable from a given * starting vertex. From f178b71d1c92631d126e0f632cd64e80f01cfb1f Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Tue, 23 Sep 2025 19:16:09 -0700 Subject: [PATCH 09/10] Completed graphSum and passed the test. --- src/Practice.java | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 669a4cc..e742f74 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -338,9 +338,39 @@ public static int treeSum(BinaryTreeNode root, int total) * @param start the starting vertex * @return the sum of all the vertices */ - public static int graphSum(Vertex start) { - return 0; + public static int graphSum(Vertex start) + { + // Set up a Set to keep track of all locations visited + Set> visited = new HashSet<>(); + // Call a helper method and return the sum + return graphSum(start, visited); + } + + public static int graphSum(Vertex start, Set> visited) + { + // If null or already visited, return a 0 + if (start == null || visited.contains(start)) return 0; + // Add location to the visited list + visited.add(start); + // Set up a variable to hold the sum + int sum = 0; + // Add the initial data to the summ + sum += start.data; + + // Cycle through all the neighboring points + for (Vertex neighbor : start.neighbors) + { + // Recursively check each neighbor and return the sum + int neighborSum = graphSum(neighbor, visited); + // Add the neighbors sum to the total sum + sum += neighborSum; + } + + // Return the total sum + return sum; + } + /** * Returns the count of vertices in a graph that have an outdegree of 0. From 6ee486643d1239549981025269358becf36a0af3 Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Tue, 23 Sep 2025 23:10:12 -0700 Subject: [PATCH 10/10] Completed sinkCount, and passed all tests. --- src/Practice.java | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index e742f74..01a520f 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -370,7 +370,7 @@ public static int graphSum(Vertex start, Set> visited) return sum; } - + /** * Returns the count of vertices in a graph that have an outdegree of 0. @@ -380,7 +380,35 @@ public static int graphSum(Vertex start, Set> visited) * @param start the entrypoint to the graph * @return the count of vertices with outdegree 0 */ - public static int sinkCount(Vertex start) { - return 0; + public static int sinkCount(Vertex start) + { + // Initialize set to keep track of what's been visited + Set> visited = new HashSet<>(); + // Call a helper method to count dead ends, and return the count + return sinkCount(start, visited, 0); + } + + public static int sinkCount(Vertex start, Set> visited, int count) + { + // If null or already visited, return count + if (start == null || visited.contains(start)) return count; + // Add the current vertex to visited + visited.add(start); + + // If there are no neighbors, increment the count + if (start.neighbors.isEmpty()) count++; + + // Cycle through each neighbor + for (Vertex neighbor : start.neighbors) + { + // Recursively check for additional neighbor dead ends + count = sinkCount(neighbor, visited, count); + } + + // Return the total count + return count; + + } + } \ No newline at end of file