From aed60862087920e80e9a3d825a78b13f4c325564 Mon Sep 17 00:00:00 2001 From: ryanrmills Date: Sat, 11 Apr 2026 12:59:07 -0700 Subject: [PATCH 1/3] first commit --- src/Practice.java | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..f908abb 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,5 @@ +import java.security.KeyStore.Entry; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -12,7 +14,14 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + if (nums == null) return 0; + int sum = 0; + + for (int i = 0; i < nums.length; i++){ + sum += nums[i] % 2 != 0 ? nums[i] : 0; + } + + return sum; } /** @@ -27,7 +36,17 @@ 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 cannot be null"); + if (words.size() == 0) throw new IllegalArgumentException("words set is empty."); + + String shortest = words.iterator().next(); + + for (String word : words){ + if (word.length() < shortest.length()) shortest = word; + } + + + return shortest; } /** @@ -40,7 +59,13 @@ 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("ages is null"); + Set adultSet = new HashSet<>(); + for (Map.Entry pair : ages.entrySet()){ + if (pair.getValue() >= 18) adultSet.add(pair.getKey()); + } + + return adultSet; } /** From 4cb57d773dacf5a4d2fe7999910f7aa5a6f0cb85 Mon Sep 17 00:00:00 2001 From: ryanrmills Date: Sat, 11 Apr 2026 13:24:00 -0700 Subject: [PATCH 2/3] second commit --- src/Practice.java | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index f908abb..2b400ee 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,4 +1,4 @@ -import java.security.KeyStore.Entry; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -42,7 +42,7 @@ public static String shortestWord(Set words) { String shortest = words.iterator().next(); for (String word : words){ - if (word.length() < shortest.length()) shortest = word; + if (word.length() < shortest.length() || word.compareTo(shortest) < 0) shortest = word; } @@ -76,7 +76,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("head cannot be null"); + + int biggest = head.data; + ListNode current = head.next; + + while (current != null) { + if (current.data > biggest) { + biggest = current.data; + } + current = current.next; + } + + return biggest; } /** @@ -93,7 +105,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 counts = new HashMap<>(); + ListNode current = head; + + while (current != null) { + counts.put(current.data, counts.getOrDefault(current.data, 0) + 1); + current = current.next; + } + + return counts; } @@ -106,7 +126,11 @@ 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 leftLevels = levelCount(root.left); + int rightLevels = levelCount(root.right); + return 1 + Math.max(leftLevels, rightLevels); } @@ -233,4 +257,4 @@ public static int onlyChildCount(TreeNode root) { public static int maxDepth(Map> tree, T root) { return 0; } -} \ No newline at end of file +} From a5004bfc51161296ac514a2890001ede3359c73b Mon Sep 17 00:00:00 2001 From: ryanrmills Date: Sat, 11 Apr 2026 13:39:46 -0700 Subject: [PATCH 3/3] third commit --- src/Practice.java | 68 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 2b400ee..1f5f7f5 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -42,7 +42,10 @@ public static String shortestWord(Set words) { String shortest = words.iterator().next(); for (String word : words){ - if (word.length() < shortest.length() || word.compareTo(shortest) < 0) shortest = word; + if (word.length() < shortest.length() + || (word.length() == shortest.length() && word.compareTo(shortest) < 0)) { + shortest = word; + } } @@ -158,7 +161,9 @@ 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 <= 0) return 0; + if (level == 1) return root.data; + return sumAtLevel(root.left, level - 1) + sumAtLevel(root.right, level - 1); } @@ -173,7 +178,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 false; + return binaryTreeSum(root) == listSum(head); } /** @@ -185,7 +190,14 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @return the sum of all the tree's values */ public static int nbSum(TreeNode root) { - return 0; + if (root == null) return 0; + + int sum = root.data; + for (TreeNode child : root.children) { + sum += nbSum(child); + } + + return sum; } /** @@ -217,7 +229,18 @@ public static int nbSum(TreeNode root) { * @return the count of nodes that do not have siblings, EXCLUDING THE ROOT */ public static int onlyChildCount(TreeNode root) { - return 0; + if (root == null) return 0; + + int count = 0; + if (root.children.size() == 1) { + count += 1; + } + + for (TreeNode child : root.children) { + count += onlyChildCount(child); + } + + return count; } /** @@ -255,6 +278,39 @@ public static int onlyChildCount(TreeNode root) { * @return the depth of the tree, or 0 if the tree is null or the root is not present in the tree */ public static int maxDepth(Map> tree, T root) { - return 0; + if (tree == null || root == null || !tree.containsKey(root)) return 0; + return maxDepthFrom(tree, root); + } + + private static int binaryTreeSum(BinaryTreeNode root) { + if (root == null) return 0; + return root.data + binaryTreeSum(root.left) + binaryTreeSum(root.right); + } + + private static int listSum(ListNode head) { + int sum = 0; + ListNode current = head; + while (current != null) { + sum += current.data; + current = current.next; + } + return sum; + } + + private static int maxDepthFrom(Map> tree, T root) { + List children = tree.get(root); + if (children == null || children.isEmpty()) { + return 1; + } + + int maxChildDepth = 0; + for (T child : children) { + int childDepth = maxDepthFrom(tree, child); + if (childDepth > maxChildDepth) { + maxChildDepth = childDepth; + } + } + + return 1 + maxChildDepth; } }