diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..720d79b 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,7 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -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; + if (nums == null) return 0; + int sum = 0; + + for (int num : nums) + { + if (num % 2 != 0) + { + sum += num; + } + } + + return sum; } /** @@ -26,7 +41,25 @@ 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.size() == 0) throw new IllegalArgumentException(); + + ArrayList list = new ArrayList<>(); + list.addAll(words); + Collections.sort(list); + + String shortestWord = list.get(0); + + for (String word : words) + { + if (word.length() < shortestWord.length()) + { + shortestWord = word; + } + } + + return shortestWord; } /** @@ -39,7 +72,20 @@ 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 mySet = new HashSet<>(); + + for (String name : ages.keySet()) + { + if (ages.get(name) >= 18) + { + mySet.add(name); + } + } + + return mySet; } /** @@ -50,7 +96,21 @@ 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.data > biggest) + { + biggest = current.data; + } + current = current.next; + } + return biggest; } /** @@ -67,7 +127,25 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + + if (head == null) return new HashMap<>(); + + Map valuesMap = new HashMap<>(); + ListNode current = head; + + while (current != null) + { + if (!valuesMap.containsKey(current.data)) + { + valuesMap.put(current.data, 1); + } else { + int temp = valuesMap.get(current.data) + 1; + valuesMap.put(current.data, temp); + } + current = current.next; + } + + return valuesMap; } @@ -80,7 +158,22 @@ public static Map frequencies(ListNode head) { * @return the number of levels in the tree */ public static int levelCount(BinaryTreeNode root) { - return 0; + + int levels = 0; + + if (root == null) return 0; + + if (root.left == null && root.right == null) + { + return 1; + } + + int leftLevels = levelCount(root.left); + int rightLevels = levelCount(root.right); + + levels = Math.max(leftLevels, rightLevels); + + return 1 + levels; } @@ -108,9 +201,20 @@ 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; + + int sum = 0; + + int leftSum = sumAtLevel(root.left, level-1); + int rightSum = sumAtLevel(root.right, level-1); + + sum = leftSum + rightSum; + + return sum; + } /** * Returns true if the sum of the values in a given tree is equal to the sum @@ -123,9 +227,38 @@ 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) { + + if (sumLinkedList(head) == sumTree(root)) return true; + return false; } + public static int sumLinkedList(ListNode head) + { + int total = 0; + ListNode current = head; + + while (current != null) + { + total += current.data; + current = current.next; + } + + return total; + } + + public static int sumTree(BinaryTreeNode root) + { + if (root == null) return 0; + + int sumLeft = sumTree(root.left); + int sumRight = sumTree(root.right); + + int total = root.data + sumLeft + sumRight; + + return total; + } + /** * Returns the sum of all the vertices in a graph that are reachable from a given * starting vertex. @@ -136,7 +269,24 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + HashSet> myVisited = new HashSet<>(); + + return graphSumHelper(start, myVisited); + } + + public static int graphSumHelper(Vertex vertex, Set> visited) + { + if (vertex == null || visited.contains(vertex)) return 0; + + visited.add(vertex); + int total = vertex.data; + + for (Vertex neighbor : vertex.neighbors) + { + total += graphSumHelper(neighbor, visited); + } + + return total; } /** @@ -148,6 +298,28 @@ public static int graphSum(Vertex start) { * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + HashSet> myVisited = new HashSet<>(); + + return sinkCountHelper(start, myVisited); + } + + public static int sinkCountHelper(Vertex vertex, Set> visited) { + + if (vertex == null || visited.contains(vertex)) return 0; + + int sinkCount = 0; + visited.add(vertex); + + if (vertex.neighbors.size() == 0) + { + sinkCount += 1; + } + + for (Vertex neighbor : vertex.neighbors) + { + sinkCount += sinkCountHelper(neighbor, visited); + } + + return sinkCount; } } \ No newline at end of file