diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..8b3d67e 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; + if(nums == null || nums.length < 1){ + return 0; + } + int sum = 0; + for(int i =0; i<= nums.length-1; i++){ + if(nums[i]%2 != 0){ + sum += nums[i]; + } + } + return sum; } /** @@ -26,7 +37,21 @@ 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();} + if(words == null){throw new NullPointerException();} + String shortest = null; + for(String word: words){ + if(shortest == null) shortest = word; + if(word.length() < shortest.length()){ + shortest = word; + } + if(word.length() == shortest.length()){ + if(word.compareTo(shortest) <0){ + shortest = word; + } + } + } + return shortest; } /** @@ -39,7 +64,14 @@ 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 adult = new HashSet<>(); + for(String name: ages.keySet()){ + if(ages.get(name)>=18){ + adult.add(name); + } + } + return adult; } /** @@ -50,7 +82,15 @@ 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 biggest = Integer.MIN_VALUE; + + while(temp != null){ + if(temp.data > biggest) biggest = temp.data; + temp = temp.next; + } + return biggest; } /** @@ -67,7 +107,21 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map freqMap= new HashMap<>(); + if(head == null) return freqMap; + ListNode temp = head; + int count = 0; + while(temp != null){ + if(freqMap.containsKey(temp.data)){ + count = freqMap.get(temp.data) +1; + freqMap.put(temp.data, count); + }else{ + freqMap.put(temp.data, 1); + } + + temp = temp.next; + } + return freqMap; } @@ -80,7 +134,17 @@ 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; + return levelCount(root, 0); + + } + public static int levelCount(BinaryTreeNode temp, int lvlCount){ + if(temp == null) return lvlCount; + lvlCount++; + int leftCount = levelCount(temp.left, lvlCount); + int rightCount = levelCount(temp.right, lvlCount); + int max = Math.max(leftCount, rightCount); + return max; } @@ -108,7 +172,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; + return sumAtLevelRecur(root, level, 1, 0); + } + public static int sumAtLevelRecur(BinaryTreeNode currentNode, int level, int currentLvl, int sum){ + if(currentLvl < level){ + if(currentNode.left != null) sum = sumAtLevelRecur(currentNode.left, level, currentLvl+1, sum); + if(currentNode.right != null) sum = sumAtLevelRecur(currentNode.right, level, currentLvl+1, sum); + } + if(currentLvl == level){ + sum += currentNode.data; + } + return sum; } @@ -123,7 +198,28 @@ 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) { + int rootSum = sumRoot(root, 0); + int headSum = sumHead(head, 0); + System.out.println(rootSum + " " + headSum); + if(rootSum == headSum) return true; return false; + + + } + public static int sumRoot(BinaryTreeNode temp, int sum){ + if(temp == null) return sum; + sum += temp.data; + sum = sumRoot(temp.left, sum); + sum = sumRoot(temp.right, sum); + + return sum; + } + public static int sumHead(ListNode head, int sum){ + if(head == null) return sum; + sum += head.data; + sum = sumHead(head.next, sum); + + return sum; } /** @@ -136,9 +232,22 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + HashSet> visited = new HashSet<>(); + return graphSum(start, visited, 0); + } + public static int graphSum(Vertex current, HashSet> visited, int sum){ + if(current == null || visited.contains(current)) return sum; + if (!visited.contains(current)) { + visited.add(current); + sum += current.data; + for(Vertex neighbor: current.neighbors){ + sum = graphSum(neighbor, visited, sum); + } + } + return sum; + } /** * Returns the count of vertices in a graph that have an outdegree of 0. * @@ -148,6 +257,24 @@ public static int graphSum(Vertex start) { * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + return sinkCount(start, 0, new HashSet<>()); + } + + public static int sinkCount(Vertex current, int sinks, HashSet> visited){ + + if(current != null){ + if(!visited.contains(current)){ + visited.add(current); + if(current.neighbors.size() == 0){ + sinks++; + } + for(Vertex neighbor: current.neighbors){ + sinks = sinkCount(neighbor, sinks, visited); + } + } + + + } + return sinks; } } \ No newline at end of file