diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..d76a11e 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,5 @@ +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -12,7 +14,17 @@ 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 oddSum = 0; + for(int num : nums){ + if(num % 2 != 0){ + oddSum += num; + } + } + return oddSum; } /** @@ -27,7 +39,32 @@ 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("Set cannot be null"); + } + + if(words.isEmpty()){ + throw new IllegalArgumentException("Set cannot be empty"); + } + + String shortestWord = ""; + for(String word : words){ + if(shortestWord.equals("")){ + shortestWord = word; + } + + if(word.length() < shortestWord.length()){ + shortestWord = word; + }else if (word.length() == shortestWord.length()){ + if(word.compareTo(shortestWord) < 0){ + shortestWord = word; + } + } + + } + + return shortestWord; + } /** @@ -40,7 +77,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("Age cannot be null"); + } + + Set namesOver18 = new HashSet<>(); + for(String name : ages.keySet()){ + if(ages.get(name) >= 18){ + namesOver18.add(name); + } + } + + return namesOver18; + } /** @@ -51,7 +100,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 biggestNumber = Integer.MIN_VALUE; + + ListNode current = head; + + while(current != null){ + biggestNumber = Math.max(biggestNumber, current.data); + current = current.next; + } + return biggestNumber; } /** @@ -68,7 +129,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 frequencyMap = new HashMap<>(); + if(head == null){ + return frequencyMap; + } + + ListNode current = head; + + while(current != null){ + frequencyMap.put(current.data, frequencyMap.getOrDefault(current.data, 0) + 1); + current = current.next; + } + + return frequencyMap; } @@ -81,7 +154,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; + } + + return Math.max(levelCount(root.left), levelCount(root.right)) + 1; } @@ -109,7 +186,23 @@ 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; + } + + return sumAtLevel(root, level, 1); + } + + public static int sumAtLevel(BinaryTreeNode root, int level, int currentLevel) { + if(root == null){ + return 0; + } + + if(currentLevel == level){ + return root.data; + } + + return sumAtLevel(root.left, level, currentLevel + 1) + sumAtLevel(root.right, level, currentLevel + 1); } @@ -124,7 +217,26 @@ 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 listSum = 0; + + ListNode current = head; + + while(current != null){ + listSum += current.data; + current = current.next; + } + + int treeSum = sumTree(root); + + return treeSum == listSum; + } + + private static int sumTree(BinaryTreeNode root){ + if(root == null){ + return 0; + } + + return sumTree(root.left) + sumTree(root.right) + root.data; } /** @@ -135,10 +247,20 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @param root the root of the tree * @return the sum of all the tree's values */ - public static int nbSum(TreeNode root) { + public static int nbSum(TreeNode root) { + if (root == null) { return 0; } + int total = root.data; + + for (TreeNode child : root.children) { + total += nbSum(child); + } + + return total; + } + /** * Returns the count of nodes in a non-binary tree that are only children, EXCLUDING the root. * @@ -168,7 +290,21 @@ 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){ + count++; + } + + return count; } /** @@ -206,6 +342,26 @@ 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){ + return 0; + } + + int maxDepth = 1; + + List children = tree.get(root); + + if(children != null){ + for(T child : children){ + maxDepth = Math.max(maxDepth, maxDepth(tree, child)); + } + }else{ + return 0; + } + + + + return 1 + maxDepth; + + } } \ No newline at end of file