diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..a5f7157 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,18 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + int sum = 0; + if (nums == null) { + return 0; + } + + for (int i = 0; i < nums.length; i++) { + if (nums[i] % 2 != 0) { + sum+=nums[i]; + } + } + + return sum; } /** @@ -26,7 +39,21 @@ 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 was not created"); + if (words.isEmpty()) throw new IllegalArgumentException("Words is empty"); + String shortest = null; + for (String word : words) { + if (shortest == null) shortest = word; + if (shortest.length() > word.length()) { + shortest = word; + } else if (shortest.length() == word.length()) { + if (word.compareTo(shortest) < 0) { + shortest = word; + } + } + } + + return shortest; } /** @@ -39,7 +66,17 @@ 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 adults = new HashSet(); + + for (String person : ages.keySet()) { + if (ages.get(person) >= 18) { + adults.add(person); + } + } + + return adults; } /** @@ -50,7 +87,17 @@ 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(); + int max = head.data; + + ListNode current = head.next; + + while (current != null) { + if (current.data > max) max = current.data; + current = current.next; + } + + return max; } /** @@ -67,7 +114,21 @@ 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<>(); + if (head == null) return frequency; + + ListNode current = head; + while (current != null) { + T data = current.data; + if (!frequency.containsKey(data)) { + frequency.put(data, 1); + } else { + frequency.put(data, frequency.get(data)+1); + } + current = current.next; + } + + return frequency; } @@ -80,7 +141,9 @@ 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 1 + Integer.max(levelCount(root.left), levelCount(root.right)); } @@ -108,9 +171,18 @@ 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; + + return traverse(root, level, 1); } + public static int traverse(BinaryTreeNode root, int targetLevel, int currentLevel) { + if (root == null) return 0; + + if (currentLevel == targetLevel) return root.data; + + return traverse(root.left, targetLevel, currentLevel+1) + traverse(root.right, targetLevel, currentLevel+1); + } + /** * Returns true if the sum of the values in a given tree is equal to the sum @@ -123,9 +195,33 @@ 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) { + int treeTotal = traverseTreeSum(root); + int listTotal = traverseListSum(head); + + if (treeTotal == listTotal) return true; + return false; } + public static int traverseTreeSum(BinaryTreeNode root) { + if (root == null) return 0; + + return root.data + traverseTreeSum(root.left) + traverseTreeSum(root.right); + } + + public static int traverseListSum(ListNode head) { + if (head == null) return 0; + + ListNode current = head; + int total = 0; + while (current != null) { + total+=current.data; + current = current.next; + } + + return total; + } + /** * Returns the sum of all the vertices in a graph that are reachable from a given * starting vertex. @@ -136,7 +232,26 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + if (start == null) return 0; + + Set> visited = new HashSet<>(); + + return graphSum(start, visited); + } + + public static int graphSum(Vertex current, Set> visited) { + if (current == null || visited.contains(current)) return 0; + + visited.add(current); + int sum = current.data; + + if (current.neighbors == null) return sum; + + for (Vertex vertex : current.neighbors) { + if (vertex != null) sum+=graphSum(vertex, visited); + } + + return sum; } /** @@ -148,6 +263,24 @@ public static int graphSum(Vertex start) { * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + if (start == null) return 0; + + Set> visited = new HashSet<>(); + + return sinkCount(start, visited); + } + + public static int sinkCount(Vertex current, Set> visited) { + if (current == null || visited.contains(current)) return 0; + + visited.add(current); + if (current.neighbors == null || current.neighbors.isEmpty()) return 1; + + int exitCount = 0; + for (Vertex vertex : current.neighbors) { + exitCount+=sinkCount(vertex, visited); + } + + return exitCount; } } \ No newline at end of file