diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..2db49ed 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,5 @@ +import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -11,7 +13,19 @@ 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 : nums) { + if (i % 2 != 0){ + sum = sum + i; + } + i++; + } + return sum; } /** @@ -26,7 +40,21 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + if (words == null || words.isEmpty()){ + throw new IllegalArgumentException("Set is empty"); + } + + String shortest = null; + for (String word : words){ + if (shortest == null){ + shortest = word; + } else if (word.length() < shortest.length()){ + shortest = word; + } else if (word.length() == shortest.length() && word.compareTo(shortest) < 0){ + shortest = word; + } + } + return shortest; } /** @@ -39,7 +67,18 @@ 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 (String name : ages.keySet()){ + if(ages.get(name) >= 18){ + names.add(name); + } + } + return names; } /** @@ -50,7 +89,20 @@ 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 NullPointerException(); + } + + int biggest = head.data; + ListNode current = head.next; + + while(current != null){ + if(current.data > biggest){ + biggest = current.data; + } + current = current.next; + } + return biggest; } /** @@ -67,7 +119,17 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map frequency = new HashMap<>(); + + ListNode current = head; + + while (current != null) { + T value = current.data; + frequency.put(value, frequency.getOrDefault(value, 0) + 1); + current = current.next; + } + + return frequency; } @@ -80,7 +142,14 @@ 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); } @@ -108,7 +177,15 @@ 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); } @@ -123,7 +200,23 @@ 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 treeSum = sumTreeHelper(root); + + int listSum = 0; + ListNode current = head; + while (current != null){ + listSum += current.data; + current = current.next; + } + + return treeSum == listSum; + } + + private static int sumTreeHelper(BinaryTreeNode node){ + if (node == null){ + return 0; + } + return node.data + sumTreeHelper(node.left) + sumTreeHelper(node.right); } /** @@ -136,7 +229,22 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + Set> visited = new HashSet<>(); + return dfsSumHelper(start, visited); + } + + private static int dfsSumHelper(Vertex vertex, Set> visited){ + if (vertex == null || visited.contains(vertex)){ + return 0; + } + + visited.add(vertex); + int sum = vertex.data; + + for (Vertex neighbor : vertex.neighbors){ + sum += dfsSumHelper(neighbor, visited); + } + return sum; } /** @@ -148,6 +256,25 @@ public static int graphSum(Vertex start) { * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + Set> visited = new HashSet<>(); + return dfsSinkCountHelper(start, visited); + } + + private static int dfsSinkCountHelper(Vertex vertex, Set> visited) { + if (vertex == null || visited.contains(vertex)){ + return 0; + } + + visited.add(vertex); + + int count = 0; + if (vertex.neighbors.isEmpty()){ + count = 1; + } + + for (Vertex neighbor : vertex.neighbors) { + count += dfsSinkCountHelper(neighbor, visited); + } + return count; } } \ No newline at end of file