diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..bfeeef7 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,12 @@ import java.util.Map; +import java.util.Queue; import java.util.Set; +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; public class Practice { /** @@ -11,7 +18,17 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + int oddTotal = 0; + if (nums == null || nums.length == 0) { + return 0; + } + + for (int i: nums) { + if (i % 2 != 0) { + oddTotal += i; + } + } + return oddTotal; } /** @@ -26,7 +43,23 @@ 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 is null"); + } + if (words.isEmpty()) { + throw new IllegalArgumentException("Words is empty"); + } + + String shortest = words.iterator().next(); + for (String i : words) { + if (i.length() < shortest.length()) { + shortest = i; + } + else if (i.length() == shortest.length() && i.compareTo(shortest) < 0) { + shortest = i; + } + } + return shortest; } /** @@ -39,7 +72,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("no age"); + } + + Set result = new HashSet<>(); + for (Map.Entry entry : ages.entrySet()) { + Integer age = entry.getValue(); + if (age != null && age >= 18) { + result.add(entry.getKey()); + } + } + return result; } /** @@ -50,6 +94,19 @@ public static Set adults(Map ages) { * @throws IllegalArgumentException if head is null */ public static int biggestNumber(ListNode head) { + if (head == null) { + throw new IllegalArgumentException("Head is null"); + } + + int max = head.data; + ListNode curr = head.next; + + while (curr != null) { + if (curr.data > max) { + max = curr.data; + } + curr = curr.next; + } return 0; } @@ -67,7 +124,16 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map freq = new HashMap<>(); + ListNode curr = head; + + while (curr != null) { + T val = curr.data; + freq.put(val, freq.getOrDefault(val, 0) + 1); + curr = curr.next; + } + + return freq; } @@ -80,7 +146,11 @@ public static Map frequencies(ListNode head) { * @return the number of levels in the tree */ public static int levelCount(BinaryTreeNode root) { + if (root == null) { return 0; + } + + return 1 + Math.max(levelCount(root.left), levelCount(root.right)); } @@ -108,7 +178,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 || level < 1) { + return 0; + } + if (level == 1) { + return root.data; + } + return sumAtLevel(root.left, level - 1) + sumAtLevel(root.right, level - 1); } @@ -123,9 +199,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; + return treeSum(root) == listSum(head); } + private static int treeSum(BinaryTreeNode node) { + if (node == null) return 0; + + int v = node.data; + + return v + treeSum(node.left) + treeSum(node.right); +} + + private static int listSum(ListNode node) { + int total = 0; + ListNode curr = node; + while (curr != null) { + total += curr.data; // use curr.data if that's your field + curr = curr.next; + } + return total; +} + /** * Returns the sum of all the vertices in a graph that are reachable from a given * starting vertex. @@ -136,7 +230,27 @@ 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; + } + + int sum = 0; + Set> seen = new HashSet<>(); + Queue> queue = new LinkedList<>(); + queue.add(start); + seen.add(start); + + while (!queue.isEmpty()) { + Vertex v = queue.poll(); + sum += v.data; + + for (Vertex n : v.neighbors) { + if (n != null && seen.add(n)) { + queue.add(n); + } + } + } + return sum; } /** @@ -148,6 +262,28 @@ 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 sinks = 0; + Set> seen = new HashSet<>(); + Queue> q = new LinkedList<>(); + q.add(start); + seen.add(start); + + while (!q.isEmpty()) { + Vertex v = q.poll(); + List> neighbor = v.neighbors; + + if (neighbor == null || neighbor.isEmpty()) { + sinks++; + } else { + for (Vertex n : neighbor) { + if (n != null && seen.add(n)) { + q.add(n); + } + } + } + } + return sinks; } } \ No newline at end of file