diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..18b5aca 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,10 @@ 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 num : nums) if (Math.abs(num) % 2 == 1) sum += num; + return sum; } /** @@ -27,7 +32,14 @@ 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(); + if (words.isEmpty()) throw new IllegalArgumentException(); + String shortest = null; + for (String str : words) { + if (shortest == null || str.length() < shortest.length() || + (str.length() == shortest.length() && str.compareTo(shortest) < 0)) shortest = str; + } + return shortest; } /** @@ -40,7 +52,10 @@ 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 names = new HashSet<>(); + for (Map.Entry entry : ages.entrySet()) if (entry.getValue() >= 18) names.add(entry.getKey()); + return names; } /** @@ -51,7 +66,14 @@ 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 biggest = current.data; + while (current.next != null) { + if (current.next.data > biggest) biggest = current.next.data; + current = current.next; + } + return biggest; } /** @@ -68,7 +90,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 map = new HashMap<>(); + if (head == null) return map; + ListNode current = head; + while (current != null) { + if (!map.containsKey(current.data)) map.put(current.data, 1); + else map.put(current.data, map.get(current.data) + 1); + current = current.next; + } + return map; } @@ -81,7 +111,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 leftLen = levelCount(root.left); + int rightLen = levelCount(root.right); + if (leftLen > rightLen) return leftLen + 1; + else return rightLen + 1; } @@ -109,7 +143,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) return 0; + if (level == 1) return root.data; + return sumAtLevel(root.left, level - 1) + sumAtLevel(root.right, level - 1); } @@ -124,7 +160,18 @@ 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; + ListNode current = head; + int listSum = 0; + while (current != null) { + listSum += current.data; + current = current.next; + } + return listSum == sumMatch(root); + } + + private static int sumMatch(BinaryTreeNode root) { + if (root == null) return 0; + return root.data + sumMatch(root.left) + sumMatch(root.right); } /** @@ -136,7 +183,10 @@ 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 = 0; + for (TreeNode child : root.children) sum += nbSum(child); + return sum + root.data; } /** @@ -168,7 +218,11 @@ 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++; + for (TreeNode child : root.children) count += onlyChildCount(child); + return count; } /** @@ -206,6 +260,15 @@ 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 || !tree.containsKey(root)) return 0; + return maxDepthHelper(tree, root); + } + + private static int maxDepthHelper(Map> tree, T root) { + List children = tree.get(root); + if (children == null || children.isEmpty()) return 1; + int max = 1; + for (T child : children) max = Math.max(max, maxDepthHelper(tree, child) + 1); + return max; } } \ No newline at end of file