diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..6b61ecd 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,20 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + + int oddCount = 0; + + if(nums == null) return 0; + + for(int items: nums) + { + if(items % 2 != 0) + { + oddCount += items; + } + } + + return oddCount; } /** @@ -26,7 +43,24 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + + String smallestWord = null; + + if(words == null) throw new NullPointerException(); + if(words.isEmpty()) throw new IllegalArgumentException(); + + for(String items: words) + { + if(smallestWord == null || items.length() < smallestWord.length() || + (items.length() == smallestWord.length()) && items.compareTo(smallestWord) < 0) + { + smallestWord = items; + } + + } + + + return smallestWord; } /** @@ -39,7 +73,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 people = new HashSet<>(); + + for(Map.Entry items : ages.entrySet()) + { + if(items.getValue() >= 18) + { + people.add(items.getKey()); + } + } + + + return people; } /** @@ -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(); + + int biggest = Integer.MIN_VALUE; + ListNode current = head; + while(current != null) + { + if(biggest < current.data) + { + biggest = current.data; + } + current = current.next; + } + + return biggest; } /** @@ -67,7 +127,19 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map map = new HashMap<>(); + + + if(head == null) return map; + ListNode current = head; + while(current != null) + { + map.put(current.data, map.getOrDefault(current.data, 0) + 1); + current = current.next; + } + + + return map; } @@ -80,7 +152,13 @@ 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 leftSearch = levelCount(root.left); + int rightSearch = levelCount(root.right); + + + return Math.max(leftSearch, rightSearch) + 1; } @@ -108,6 +186,29 @@ public static int levelCount(BinaryTreeNode root) { * @return the sum of the nodes at the given level */ public static int sumAtLevel(BinaryTreeNode root, int level) { + if(root == null) return 0; + + Queue> q = new LinkedList<>(); + q.add(root); + int target = 1; + + while(!q.isEmpty()) + { + int size = q.size(); + int sumed = 0; + + for(int i = 0; i < size; i++) + { + BinaryTreeNode current = q.poll(); + if(target == level) sumed += current.data; + if(current.left != null) q.add(current.left); + if(current.right != null) q.add(current.right); + } + + if(target == level) return sumed; + target++; + + } return 0; } @@ -123,7 +224,34 @@ 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; + + if(head == null && root == null) return true; + if(root == null) return false; + if(head == null) return false; + + Queue> q = new LinkedList<>(); + q.add(root); + int BSTSum = 0; + + while(!q.isEmpty()) + { + BinaryTreeNode current = q.poll(); + BSTSum += current.data; + if(current.left != null) q.add(current.left); + if(current.right != null) q.add(current.right); + } + + ListNode current = head; + int LNSum = 0; + + while(current != null) + { + LNSum += current.data; + current = current.next; + } + + + return BSTSum == LNSum; } /** @@ -136,7 +264,25 @@ 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 dfs(start, visited); + } + + + public static int dfs(Vertex start, Set> visited) + { + if(visited.contains(start)) return 0; + visited.add(start); + + int sum = start.data; + + for(Vertex neighbor: start.neighbors) + { + sum += dfs(neighbor, visited); + } + return sum; } /** @@ -148,6 +294,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; + Set> visited = new HashSet<>(); + return sinkCountDFS(start, visited); + } + + public static int sinkCountDFS(Vertex start, Set> visited) + { + if(visited.contains(start)) return 0; + visited.add(start); + + int count = 0; + + if(start.neighbors.isEmpty()) + { + count++; + } + + for(Vertex neighbor: start.neighbors) + { + count += sinkCountDFS(neighbor, visited); + } + + return count; } } \ No newline at end of file