diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..f8274fe 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,17 @@ 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 count = 0; + + + for(int i = 0; i < nums.length; i++){ + if(nums[i] % 2 != 0){ + count += nums[i]; + } + } + return count; + } /** @@ -26,7 +38,23 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + if(words.size() == 0) throw new IllegalArgumentException(); + if(words == null) throw new NullPointerException(); + + + String shortest = null; + + + for (String word : words) { + if(shortest == null) shortest = word; + else if(word.length() < shortest.length()) shortest = word; + else if(word.length() == shortest.length()){ + if(word.compareTo(shortest) < 0) shortest = word; + } + } + + + return shortest; } /** @@ -39,7 +67,13 @@ 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 set = new HashSet<>(); + + for (String name : ages.keySet()) { + if(ages.get(name) >= 18) set.add(name); + } + return set; } /** @@ -50,7 +84,16 @@ 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 temp = head; + int max = Integer.MIN_VALUE; + + while(temp!= null){ + if(temp.data > max) max = temp.data; + temp = temp.next; + } + return max; } /** @@ -67,7 +110,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 map = new HashMap<>(); + if(head == null) return map; + + ListNode temp = head; + + while(temp != null){ + if(!map.containsKey(temp.data)){ + map.put(temp.data, map.getOrDefault(temp.data, 0)+1); + }else{ + map.put(temp.data, map.getOrDefault(temp.data, 0)+1); + } + + temp = temp.next; + } + + return map; } @@ -80,7 +138,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 Math.max(left, right) + 1; } @@ -108,7 +171,15 @@ 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; + return sumAtLevel(root, level,1); + } + + private static int sumAtLevel(BinaryTreeNode root, int level, int curLvl){ + if(root == null) return 0; + + if(curLvl == level) return root.data; + + return sumAtLevel(root.left, level, curLvl+1) + sumAtLevel(root.right, level, curLvl+1); } @@ -123,7 +194,23 @@ 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 sumTree = sumOfTree(root); + int sumList = 0; + + ListNode temp = head; + while(temp != null){ + sumList += temp.data; + temp = temp.next; + } + + return sumList == sumTree; + } + + private static int sumOfTree(BinaryTreeNode root){ + if(root == null) return 0; + int sum = sumOfTree(root.left) + sumOfTree(root.right) + root.data; + + return sum; } /** @@ -136,7 +223,21 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + Set> visited = new HashSet<>(); + return graphSum(start, visited); + } + + private static int graphSum(Vertex start, Set> visited){ + if(start == null || visited.contains(start)) return 0; + + visited.add(start); + int total = start.data; + + for(Vertex neighbor : start.neighbors){ + total += graphSum(neighbor, visited); + } + + return total; } /** @@ -148,6 +249,22 @@ public static int graphSum(Vertex start) { * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + Set> visited = new HashSet<>(); + return sinkCount(start, visited); + } + + private static int sinkCount(Vertex start, Set> visited){ + if(start == null || visited.contains(start)) return 0; + + visited.add(start); + int sink = 0; + + if(start.neighbors.isEmpty()) sink++; + + for (Vertex neighbor : start.neighbors) { + sink += sinkCount(neighbor, visited); + } + + return sink; } } \ No newline at end of file