diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..01a520f 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,7 +1,10 @@ +import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; -public class Practice { +public class Practice +{ /** * Returns the sum of the odd numbers in the array. * @@ -10,8 +13,24 @@ public class Practice { * @param nums an array of numbers * @return the sum of the odd numbers in the array */ - public static int oddSum(int[] nums) { - return 0; + public static int oddSum(int[] nums) + { + // Set up sum variable + int odds = 0; + + // Null handling + if (nums == null) return odds; + + // Loop through each item in the array + for (int i = 0; i < nums.length; i++) + { + // If odd, add amount to the variable + if (nums[i] % 2 != 0) odds += nums[i]; + } + + // Return the sum of the odds in the array + return odds; + } /** @@ -25,8 +44,44 @@ public static int oddSum(int[] nums) { * @throws IllegalArgumentException if words is empty * @throws NullPointerException if words is null */ - public static String shortestWord(Set words) { - return null; + public static String shortestWord(Set words) + { + // Error handling + if (words.isEmpty()) throw new IllegalArgumentException(); + if (words == null) throw new NullPointerException(); + + // Set up shortest word variable + String shortWord = ""; + + // Go through each word in the set + for (String word : words) + { + // If empty, initialize first word into variable + if (shortWord.equals("")) shortWord = word; + // If shorter, change variable to current word + else if (word.length() < shortWord.length()) shortWord = word; + // If same length, compare individual letters lexicographically + else if (word.length() == shortWord.length()) + { + // Cycle through each letter in the word + for (int i = 0; i < word.length(); i++) + { + // If larger, keep old word and stop checking + if (word.charAt(i) > shortWord.charAt(i)) break; + // If smaller, replace with new word and stop checking + if (word.charAt(i) < shortWord.charAt(i)) + { + shortWord = word; + break; + } + // Otherwise, keep checking + } + } + } + + // Return shortest word + return shortWord; + } /** @@ -38,8 +93,23 @@ public static String shortestWord(Set words) { * @return the set of all names of people >= 18 years old * @throws NullPointerException if ages is null */ - public static Set adults(Map ages) { - return null; + public static Set adults(Map ages) + { + // Set up a variable to hold a set of names + Set adultList = new HashSet(); + + // For each name in the map + for (String name : ages.keySet()) + { + // If the age is null throw an exception + if (ages.get(name) == null) throw new NullPointerException(); + // Otherwise, if 18 or over, add name to the adultList + else if (ages.get(name) >= 18) adultList.add(name); + } + + // Return the set + return adultList; + } /** @@ -49,8 +119,28 @@ public static Set adults(Map ages) { * @return the biggest number in the list * @throws IllegalArgumentException if head is null */ - public static int biggestNumber(ListNode head) { - return 0; + public static int biggestNumber(ListNode head) + { + // Throw exception, if head is null + if (head == null) throw new IllegalArgumentException(); + + // Set up a new node that can cycle through the list + ListNode current = head; + // Initialize the biggest number variable with the current number + int bigNum = current.data; + + // Cycle through + while (current.next != null) + { + // Set new node spot + current = current.next; + // If the current number is bigger than the value in bigNum, use the current number + if (current.data > bigNum) bigNum = current.data; + } + + // Return the biggest number + return bigNum; + } /** @@ -66,8 +156,35 @@ public static int biggestNumber(ListNode head) { * @param head the head of the list * @return a frequency map of values in the list */ - public static Map frequencies(ListNode head) { - return null; + public static Map frequencies(ListNode head) + { + // Create Map variable to hold the frequencies + Map freqMap = new HashMap(); + + // Null error handling + if (head == null) return freqMap; + + // Set up temp node to cycle through the list + ListNode current = head; + + // Add the first node data to the map + freqMap.put(current.data, 1); + + // Cycle through the node list + while (current.next != null) + { + // Go to next node + current = current.next; + + //If the key doesn't exist, add key with a count of 1 + if (!freqMap.containsKey(current.data)) freqMap.put(current.data, 1); + // If key already exists, increment the value of said key + else freqMap.put(current.data, freqMap.get(current.data) + 1); + } + + // Return the map of frequencies + return freqMap; + } @@ -79,8 +196,20 @@ public static Map frequencies(ListNode head) { * @param root the root of the tree * @return the number of levels in the tree */ - public static int levelCount(BinaryTreeNode root) { - return 0; + public static int levelCount(BinaryTreeNode root) + { + // If null return 0 + if (root == null) return 0; + + // Check left, incrementing each level + int levelLeft = levelCount(root.left) + 1; + // Check right, incrementing each level + int levelRight = levelCount(root.right) + 1; + + // Compare left and right directions, and return the largest level + if (levelLeft > levelRight) return levelLeft; + else return levelRight; + } @@ -107,8 +236,33 @@ public static int levelCount(BinaryTreeNode root) { * @param level the level to sum * @return the sum of the nodes at the given level */ - public static int sumAtLevel(BinaryTreeNode root, int level) { - return 0; + public static int sumAtLevel(BinaryTreeNode root, int level) + { + // Call helper method and return result + return sumAtLevel(root, level, 0); + } + + public static int sumAtLevel(BinaryTreeNode root, int level, int sum) + { + // If null increment the level back up and return the sum + if (root == null) + { + level++; + return sum; + } + + // Decrement the level + level--; + // When the level is zero, add the data to the sum + if (level == 0) sum += root.data; + + // Traverse left, then right + int sumLeft = sumAtLevel(root.left, level, sum); + int sumRight = sumAtLevel(root.right, level, sumLeft); + + // Return the overall sum + return sumRight; + } @@ -122,10 +276,59 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { * @param head The head of the linked list * @return true if the sums are equal, false otherwise */ - public static boolean sumMatch(BinaryTreeNode root, ListNode head) { - return false; + public static boolean sumMatch(BinaryTreeNode root, ListNode head) + { + // Call a method to calculate and return the sum of the ListNode + int listTotal = listSum(head); + // Call a method to calculate and return the sum of the tree + int treeTotal = treeSum(root, 0); + // Compare the totals, if matching return true, else return false + if (listTotal == treeTotal) return true; + else return false; + } + + public static int listSum(ListNode head) + { + // Set up the variable to hold the sum within the list + int listTotal = 0; + // If null return current total + if (head == null) return listTotal; + // Set up a node to cycle through the list + ListNode current = head; + // Add the current data to the total + listTotal += current.data; + + // Cycle through the Listnode + while (current.next != null) + { + // Go to next node + current = current.next; + // Add the current data to the total + listTotal += current.data; + } + + // Return the overall total + return listTotal; + } + public static int treeSum(BinaryTreeNode root, int total) + { + // If null, return total + if (root == null) return total; + // Add the current data to the total + total += root.data; + + // Cycle through the tree left, then right + int leftTotal = treeSum(root.left, total); + int rightTotal = treeSum(root.right, leftTotal); + + // Return the overall total + return rightTotal; + + } + + /** * Returns the sum of all the vertices in a graph that are reachable from a given * starting vertex. @@ -135,10 +338,40 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @param start the starting vertex * @return the sum of all the vertices */ - public static int graphSum(Vertex start) { - return 0; + public static int graphSum(Vertex start) + { + // Set up a Set to keep track of all locations visited + Set> visited = new HashSet<>(); + // Call a helper method and return the sum + return graphSum(start, visited); } + public static int graphSum(Vertex start, Set> visited) + { + // If null or already visited, return a 0 + if (start == null || visited.contains(start)) return 0; + // Add location to the visited list + visited.add(start); + // Set up a variable to hold the sum + int sum = 0; + // Add the initial data to the summ + sum += start.data; + + // Cycle through all the neighboring points + for (Vertex neighbor : start.neighbors) + { + // Recursively check each neighbor and return the sum + int neighborSum = graphSum(neighbor, visited); + // Add the neighbors sum to the total sum + sum += neighborSum; + } + + // Return the total sum + return sum; + + } + + /** * Returns the count of vertices in a graph that have an outdegree of 0. * @@ -147,7 +380,35 @@ public static int graphSum(Vertex start) { * @param start the entrypoint to the graph * @return the count of vertices with outdegree 0 */ - public static int sinkCount(Vertex start) { - return 0; + public static int sinkCount(Vertex start) + { + // Initialize set to keep track of what's been visited + Set> visited = new HashSet<>(); + // Call a helper method to count dead ends, and return the count + return sinkCount(start, visited, 0); + } + + public static int sinkCount(Vertex start, Set> visited, int count) + { + // If null or already visited, return count + if (start == null || visited.contains(start)) return count; + // Add the current vertex to visited + visited.add(start); + + // If there are no neighbors, increment the count + if (start.neighbors.isEmpty()) count++; + + // Cycle through each neighbor + for (Vertex neighbor : start.neighbors) + { + // Recursively check for additional neighbor dead ends + count = sinkCount(neighbor, visited, count); + } + + // Return the total count + return count; + + } + } \ No newline at end of file