diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..3c0335a 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,16 @@ 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 num : nums) { + if (num % 2 != 0){ + sum += num; + } + } + return sum; } /** @@ -20,13 +31,27 @@ public static int oddSum(int[] nums) { * If multiple words are tied for shortest, returns the one that is smallest * lexicographically. * - * @param words a set of words + * @param words a set of word * @return the shortest word in the set with a lexicographic tiebreaker * @throws IllegalArgumentException if words is empty * @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 || word.length() < shortest.length() + || (word.length() == shortest.length() && word.compareTo(shortest) < 0)) { + shortest = word; + } + } + return shortest; } /** @@ -39,7 +64,19 @@ 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()) { + Integer age = ages.get(name); + + if (age >= 18) { + result.add(name); + } + } + return result; } /** @@ -50,7 +87,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 IllegalArgumentException(); + } + + 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 +117,22 @@ 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, frequency.get(data) + 1); + } else { + frequency.put(data, 1); + } + current = current.next; + } + return frequency; } @@ -80,7 +145,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 left = levelCount(root.left); + int right = levelCount(root.right); + + return Math.max(left, right) + 1; } @@ -108,7 +180,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; + if (root == null) { + return 0; + } + + if (level == 1) { + return root.data; + } + + int left = sumAtLevel(root.left, level - 1); + int right = sumAtLevel(root.right, level - 1); + + return left + right; } @@ -123,7 +206,32 @@ 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 = 0; + + if (root != null) { + treeSum = root.data; + if (root.left != null) { + treeSum += sumMatchHelper(root.left); + } + if (root.right != null) { + treeSum += sumMatchHelper(root.right); + } + } + + int listSum = 0; + ListNode current = head; + while (current != null) { + listSum += current.data; + current = current.next; + } + return treeSum == listSum; + } + + public static int sumMatchHelper(BinaryTreeNode node) { + if (node == null) { + return 0; + } + return node.data + sumMatchHelper(node.left) + sumMatchHelper(node.right); } /** @@ -136,7 +244,24 @@ 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 graphSumHelper(start, visited); + } + + public static int graphSumHelper(Vertex current, Set> visited) { + if (visited.contains(current)) { + return 0; + } + visited.add(current); + + int sum = current.data; + for (Vertex neighbor : current.neighbors) { + sum += graphSumHelper(neighbor, visited); + } + return sum; } /** @@ -148,6 +273,29 @@ 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 sinkCountHelper(start, visited); + } + + public static int sinkCountHelper(Vertex current, Set> visited) { + // visited vertex + if (visited.contains(current)) { + return 0; + } + visited.add(current); + + int count = 0; + if (current.neighbors.isEmpty()) { + count = 1; + } + + for (Vertex neighbor : current.neighbors) { + count += sinkCountHelper(neighbor, visited); + } + return count; } } \ No newline at end of file