diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..5b28e24 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,4 +1,8 @@ +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; import java.util.Map; +import java.util.Queue; import java.util.Set; public class Practice { @@ -11,7 +15,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 +41,19 @@ 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 word : words) { + if (shortest == null) shortest = word; + if (word.length() < shortest.length()) shortest = word; + if (word.length() == shortest.length() && word.compareTo(shortest) < 0) shortest = word; + } + return shortest; + + } /** @@ -39,7 +66,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 result = new HashSet<>(); + for (String name : ages.keySet()) { + if (ages.get(name) >= 18) { + result.add(name); + } + } + return result; + } /** @@ -50,7 +86,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(); + + 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,12 @@ 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 left = levelCount(root.left); + int right = levelCount(root.right); + + return 1 + Math.max(left, right); } @@ -108,7 +174,13 @@ 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; + // at this level, return value + if(level == 1) return root.data; + + int sum = sumAtLevel(root.left, level -1) + sumAtLevel(root.right, level -1); + + return sum; } @@ -123,8 +195,27 @@ 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 treeTotal = treeSum(root); + int listTotal = listSum(head); + return treeTotal == listTotal; +} + +private static int treeSum(BinaryTreeNode root) { + if (root == null) return 0; // empty tree = 0 + return root.data + treeSum(root.left) + treeSum(root.right); +} + +private static int listSum(ListNode head) { + int sum = 0; + ListNode current = head; + while (current != null) { + sum += current.data; + current = current.next; } + return sum; +} + + /** * Returns the sum of all the vertices in a graph that are reachable from a given @@ -136,8 +227,22 @@ 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 dfsSum(start, visited); } + + private static int dfsSum(Vertex v, Set> visited) { + // already visited + if (!visited.add(v)) return 0; + int sum = v.data; + for (Vertex next : v.neighbors) { + sum += dfsSum(next, visited); + } + return sum; + + } /** * Returns the count of vertices in a graph that have an outdegree of 0. @@ -148,6 +253,21 @@ 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; + + int count = 0; + Set> seen = new HashSet<>(); + Queue> skin1 = new LinkedList<>(); + skin1.add(start); + + while (!skin1.isEmpty()) { + Vertex node = skin1.poll(); + if (!seen.add(node)) continue; + + if (node.neighbors == null || node.neighbors.isEmpty()) count++; + else skin1.addAll(node.neighbors); + } + return count; +} + } \ No newline at end of file