diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..d643c78 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,4 @@ -import java.util.Map; -import java.util.Set; +import java.util.*; public class Practice { /** @@ -11,7 +10,21 @@ 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 +39,33 @@ 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 smallest = ""; + for(String word : words) //to get a word for starting length is not 0 + { + smallest = word; + break; + } + + for(String word : words) + { + if(word.length() < smallest.length()) + { + smallest = word; + } + else if(smallest.length() == word.length()) + { + if(smallest.compareTo(word) > 0) + { + smallest = word; + } + } + } + return smallest; } /** @@ -39,7 +78,21 @@ 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("map is null"); + } + Set names = new HashSet<>(); + + for(String name : ages.keySet()) + { + if(ages.get(name) >= 18) + { + names.add(name); + } + } + + return names; } /** @@ -50,7 +103,24 @@ 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("head is null"); + } + + ListNode current = head; + int biggest = head.data; + + while(current.next != null) + { + if(current.data > biggest) + { + biggest = current.data; + } + current = current.next; + } + + return biggest; } /** @@ -67,9 +137,30 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; - } + //go through list + //add each new value from list as key in map with a value of 1 + //if a value from list appears again increase the value for said key in map + //return the map + + Map frequencyMap = new HashMap<>(); + ListNode current = head; + + while(current != null) + { + if(!frequencyMap.containsKey(current.data)) + { + frequencyMap.put(current.data, 1); + } + else + { + frequencyMap.put(current.data, frequencyMap.get(current.data)+1); + } + current = current.next; + } + + return frequencyMap; + } /** * Returns the number of levels in the tree. @@ -80,9 +171,23 @@ 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); + if(left > right) + { + return left + 1; + } + else + { + return right + 1; + } + } /** * Returns the sum at a specified level in a binary tree. @@ -108,9 +213,21 @@ 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; + } + + int left = sumAtLevel(root.left, level - 1); + int right = sumAtLevel(root.right, level - 1); + + return left + right; + } /** * Returns true if the sum of the values in a given tree is equal to the sum @@ -123,7 +240,37 @@ 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(root == null && head == null) + { + return true; + } + else if((root == null && head != null) || (root != null && head == null)) + { + return false; + } + + int treeSum = treeSum(root); + + int listSum = 0; + ListNode current = head; + + while(current != null) + { + listSum += current.data; + current = current.next; + } + + return listSum == treeSum; + } + public static int treeSum(BinaryTreeNode root) + { + if(root == null) + { + return 0; + } + + int sum = root.data + treeSum(root.left) + treeSum(root.right); + return sum; } /** @@ -136,7 +283,29 @@ 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 sumGraph(start, visited); + } + public static int sumGraph(Vertex vert, Set> visited) + { + if(visited.contains(vert)) + { + return 0; + } + + visited.add(vert); + int sum = vert.data; + + for(Vertex n : vert.neighbors) + { + sum += sumGraph(n, visited); + } + + return sum; } /** @@ -148,6 +317,35 @@ 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 countOutDegrees(start, visited); + } + public static int countOutDegrees(Vertex vert, Set> visited) + { + if(visited.contains(vert)) + { + return 0; + } + + visited.add(vert); + + if(vert.neighbors == null || vert.neighbors.isEmpty()) //base case no neighbors + { + return 1; + } + + int count = 0; + + for(Vertex n : vert.neighbors) + { + count += countOutDegrees(n, visited); + } + + return count; } } \ No newline at end of file