diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..bf7484c 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,6 @@ +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -11,7 +14,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 i = 0; i < nums.length; i++) { + if(nums[i] % 2 != 0) { + sum += nums[i]; + } + } + return sum; } /** @@ -26,7 +38,31 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + if(words.isEmpty()){ + throw new IllegalArgumentException(); + } + // unsure how to do this without a long word at the start + String shortWord = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + for (String string : words) { + if(string == null){ + throw new NullPointerException(); + } + if(string.length() < shortWord.length()){ + shortWord = string; + } + else if(string.length() == shortWord.length()) { + for (int i = 0; i < string.length(); i++) { + if(string.charAt(i) != shortWord.charAt(i)){ + if((int) string.charAt(i) < (int) shortWord.charAt(i)){ + shortWord = string; + } + break; + } + continue; + } + } + } + return shortWord; } /** @@ -39,6 +75,10 @@ public static String shortestWord(Set words) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { + if(ages.isEmpty()) { + throw new NullPointerException(); + } + return null; } @@ -50,7 +90,18 @@ public static Set adults(Map ages) { * @throws IllegalArgumentException if head is null */ public static int biggestNumber(ListNode head) { - return 0; + if(head == null || head.data == null){ + throw new IllegalArgumentException(); + } + int largest = head.data; + int prevLarge = Integer.MIN_VALUE; + if(head.next != null){ + prevLarge = biggestNumber(head.next); + } + if(largest < prevLarge){ + largest = prevLarge; + } + return largest; } /** @@ -67,7 +118,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 frequency = new HashMap<>(); + frequenciesHelper(head, frequency); + return frequency; + } + public static void frequenciesHelper(ListNode head, Map frequency) { + if(head == null) return; + if(!frequency.containsKey(head.data)) { + frequency.put(head.data, 1); + } + else{ + frequency.put(head.data, frequency.get(head.data) + 1); + } + frequenciesHelper(head.next, frequency); } @@ -136,7 +199,25 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + Set> used = new HashSet<>(); + if(start == null) return 0; + int sum = start.data; + used.add(start); + // maybe error if .neighbors is empty + sum = graphSumHelper(start.neighbors, sum, used); + return sum; + } + public static int graphSumHelper(List> nums, int sum, Set> used) { + for (Vertex num : nums) { + if(!used.contains(num)){ + sum += num.data; + used.add(num); + // resets sum + // could condense but -_- + sum += graphSumHelper(num.neighbors, 0, used); + } + } + return sum; } /** @@ -148,6 +229,25 @@ 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> used = new HashSet<>(); + used.add(start); + int total = sinkCountHelper(start.neighbors, used); + return total; + } + public static int sinkCountHelper(List> nums, Set> used) { + int total = 0; + // Have to use is empty + // doesn't recognize == null (IDK why) + if(nums.isEmpty()){ + total = 1; + } + for (Vertex vertex : nums) { + if(!used.contains(vertex)){ + used.add(vertex); + total += sinkCountHelper(vertex.neighbors, used); + } + } + return total; } } \ No newline at end of file