From 026e18260950cf8b1c973b9545bf96ef5c06139f Mon Sep 17 00:00:00 2001 From: f00d Date: Wed, 8 Apr 2026 12:28:37 -0700 Subject: [PATCH 1/5] did shortestword & adults --- src/Practice.java | 48 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..5943c26 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -12,7 +13,23 @@ 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; + } + if(nums.length > 0){ + int sum = 0; + for(int i = 0; i < nums.length; i++){ + switch (nums[i]%2) { + case 0 -> { + } + case 1 -> sum+=nums[i]; + case -1 -> sum+=nums[i]; + } + } + return sum; + }else{ + return 0; + } } /** @@ -27,7 +44,23 @@ 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.size() < 1){ + throw new IllegalArgumentException(); + } + String shortWord = "dsajfoiewahjrog8jeraiobjfdaighjdaihgerwao8gherahgusahfoiueahgfdo98"; + for (String string : words) { + if(string.length() < shortWord.length()){ + shortWord = string; + }else if(string.length() == shortWord.length()){ + int compare = shortWord.compareTo(string); + if(compare > 0){ + shortWord = string; + } + } + } + return shortWord; } /** @@ -40,7 +73,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 peopleReturn = new HashSet<>(); + for(String person: ages.keySet()){ + if(ages.get(person) >= 18){ + peopleReturn.add(person); + } + } + return peopleReturn; } /** From 756344fc742aa9c58bfbfc52003328a44c1a79c5 Mon Sep 17 00:00:00 2001 From: f00d Date: Wed, 8 Apr 2026 12:42:37 -0700 Subject: [PATCH 2/5] added biggestnumber and frequencies --- src/Practice.java | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 5943c26..1ea382f 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -93,7 +94,18 @@ 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(); + } + ListNode current = head; + int largest = current.data; + while(current!=null){ + if(current.data > largest){ + largest = current.data; + } + current = current.next; + } + return largest; } /** @@ -110,7 +122,16 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map returnMap = new HashMap<>(); + ListNode current = head; + if(head == null){ + return returnMap; + } + while(current != null){ + returnMap.put(current.data, returnMap.getOrDefault(current.data, 0)+1); + current = current.next; + } + return returnMap; } From 81082a555a09cd9a1f175c552e8936e721c3f451 Mon Sep 17 00:00:00 2001 From: f00d Date: Wed, 8 Apr 2026 20:42:16 -0700 Subject: [PATCH 3/5] levels --- src/Practice.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 1ea382f..db1f963 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -144,7 +144,13 @@ 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; + if(levelCount(root.left) > levelCount(root.right)){ + return levelCount(root.left) + 1; + }else{ + return levelCount(root.right) + 1; + } } From 4055f7345675f91f8823d25ce9abaf3385932ade Mon Sep 17 00:00:00 2001 From: f00d Date: Wed, 8 Apr 2026 21:05:14 -0700 Subject: [PATCH 4/5] summatch and nbsum --- src/Practice.java | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index db1f963..0623924 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -178,7 +178,12 @@ 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(levelCount(root.left) > levelCount(root.right)){ + return levelCount(root.left) + 1; + }else{ + return levelCount(root.right) + 1; + } } @@ -193,7 +198,25 @@ 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; + if(root == null && head == null){ + return true; + } + ListNode current = head; + int listSum = 0; + while(current != null){ + listSum += current.data; + current = current.next; + } + if(listSum == sumTree(root)){ + return true; + }else{ + return false; + } + } + public static int sumTree(BinaryTreeNode root){ + if(root == null) return 0; + + return sumTree(root.left) + sumTree(root.right) + root.data; } /** @@ -205,7 +228,12 @@ 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; } /** From 836e4acb2505f8e8b0eb41a172e785e7e8152e89 Mon Sep 17 00:00:00 2001 From: f00d Date: Wed, 8 Apr 2026 21:41:54 -0700 Subject: [PATCH 5/5] onlychildcount --- src/Practice.java | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 0623924..0443513 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -265,7 +265,17 @@ 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; + + for(TreeNode child: root.children){ + count += onlyChildCount(child); + } + if(root.children.size() == 1){ + return count + 1; + } + return count; + } /** @@ -303,6 +313,13 @@ 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) return 0; + if(tree.get(root)==null)return 0; + if(tree.get(root).isEmpty())return 1; + int value = 1; + for(T child: tree.get(root)){ + value = Math.max(value, maxDepth(tree, child)); + } + return value + 1; + } } \ No newline at end of file