diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..f6b9d95 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,153 +1,254 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; public class Practice { - /** - * Returns the sum of the odd numbers in the array. - * - * Returns 0 if the array is null or has no odd numbers. - * - * @param nums an array of numbers - * @return the sum of the odd numbers in the array - */ - public static int oddSum(int[] nums) { - return 0; + /** + * Returns the sum of the odd numbers in the array. + * + * Returns 0 if the array is null or has no odd numbers. + * + * @param nums an array of numbers + * @return the sum of the odd numbers in the array + */ + public static int oddSum(int[] nums) { + if(nums == null) return 0; + int sum = 0; + for(int num : nums) { + if(num % 2 != 0) { + sum += num; + } } + return sum; + } - /** - * Returns the shortest word in the Set. - * - * If multiple words are tied for shortest, returns the one that is smallest - * lexicographically. - * - * @param words a set of words - * @return the shortest word in the set with a lexicographic tiebreaker - * @throws IllegalArgumentException if words is empty - * @throws NullPointerException if words is null - */ - public static String shortestWord(Set words) { - return null; + /** + * Returns the shortest word in the Set. + * + * If multiple words are tied for shortest, returns the one that is smallest + * lexicographically. + * + * @param words a set of words + * @return the shortest word in the set with a lexicographic tiebreaker + * @throws IllegalArgumentException if words is empty + * @throws NullPointerException if words is null + */ + public static String shortestWord(Set words) { + if(words == null) throw new NullPointerException(); + if (words.size() == 0) throw new IllegalArgumentException(); + List wordList = new ArrayList<>(); + wordList.addAll(words); + Collections.sort(wordList); + String shortest = wordList.get(0); + for( String word: wordList) { + if(word.length() < shortest.length()) { + shortest = word; + } } + return shortest; + } - /** - * Returns a set of all the names of people that are 18 years of age or older. - * - * The input maps name to age in years. - * - * @param ages mapping of name to age - * @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; + /** + * Returns a set of all the names of people that are 18 years of age or older. + * + * The input maps name to age in years. + * + * @param ages mapping of name to age + * @return the set of all names of people >= 18 years old + * @throws NullPointerException if ages is null + */ + public static Set adults(Map ages) { + if(ages == null) throw new NullPointerException(); + Set nameSet = new HashSet<>(); + for (String key : ages.keySet()) { + if(ages.get(key) >= 18) { + nameSet.add(key); + } } + return nameSet; + } - /** - * Returns the biggest number in a linked list. - * - * @param head the head of the linked list - * @return the biggest number in the list - * @throws IllegalArgumentException if head is null - */ - public static int biggestNumber(ListNode head) { - return 0; + /** + * Returns the biggest number in a linked list. + * + * @param head the head of the linked list + * @return the biggest number in the list + * @throws IllegalArgumentException if head is null + */ + public static int biggestNumber(ListNode head) { + if(head == null) throw new IllegalArgumentException(); + ListNode current = head; + int biggest = current.data; + while(current != null) { + if(current.data > biggest) { + biggest = current.data; + } + current = current.next; } + return biggest; + } - /** - * Returns a frequency map counting how frequently items appear in a linked list. - * - * Example: - * Input: a -> x -> a -> a -> x -> y - * Output: {a:3, x:2, y: 1} - * - * Returns an empty map if head is null - * - * @param the type of data held by the list - * @param head the head of the list - * @return a frequency map of values in the list - */ - public static Map frequencies(ListNode head) { - return null; + /** + * Returns a frequency map counting how frequently items appear in a linked list. + * + * Example: + * Input: a -> x -> a -> a -> x -> y + * Output: {a:3, x:2, y: 1} + * + * Returns an empty map if head is null + * + * @param the type of data held by the list + * @param head the head of the list + * @return a frequency map of values in the list + */ + public static Map frequencies(ListNode head) { + Map map = new HashMap<>(); + ListNode current = head; + while(current != null) { + T key = current.data; + if(!map.containsKey(key)) { + map.put(key, 1); + } + else { + map.put(key, map.get(key) +1); + } + current = current.next; } + return map; + } + /** + * Returns the number of levels in the tree. + * + * An empty tree has 0 levels, a tree with only a root has 1 level. + * + * @param root the root of the tree + * @return the number of levels in the tree + */ + public static int levelCount(BinaryTreeNode root) { + if(root == null) return 0; + int left = levelCount(root.left); + int right = levelCount(root.right); + int levels = Math.max(left, right); + return levels + 1; + } - /** - * Returns the number of levels in the tree. - * - * An empty tree has 0 levels, a tree with only a root has 1 level. - * - * @param root the root of the tree - * @return the number of levels in the tree - */ - public static int levelCount(BinaryTreeNode root) { - return 0; - } + /** + * Returns the sum at a specified level in a binary tree. + * + * For example, if the given level was 3: + * 5 + * / \ + * 8 4 + * / \ / + * 7 9 2 + * / + * 1 + * + * Nodes at level 3: 7, 9, and 2 + * Sum of nodes at level 3: 18 + * + * The root is considered to be at level 1. + * + * Returns 0 if the tree is empty or if the level is not present in the tree. + * + * @param root the root of the binary tree + * @param level the level to sum + * @return the sum of the nodes at the given level + */ + public static int sumAtLevel(BinaryTreeNode root, int level) { + if(root == null) 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 + * of the values in the given list. + * + * An empty tree or list is considered to have a sum of 0. + * + * @param root The root of the binary tree + * @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) { + if (sumBinaryTree(root) == sumLinkedList(head)) return true; + return false; + } + public static int sumBinaryTree(BinaryTreeNode root) { + if(root == null) return 0; + return sumBinaryTree(root.left) + sumBinaryTree(root.right) + root.data; + } - /** - * Returns the sum at a specified level in a binary tree. - * - * For example, if the given level was 3: - * 5 - * / \ - * 8 4 - * / \ / - * 7 9 2 - * / - * 1 - * - * Nodes at level 3: 7, 9, and 2 - * Sum of nodes at level 3: 18 - * - * The root is considered to be at level 1. - * - * Returns 0 if the tree is empty or if the level is not present in the tree. - * - * @param root the root of the binary tree - * @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 sumLinkedList(ListNode head) { + if(head == null) return 0; + ListNode current = head; + int sum = 0; + while(current != null) { + sum += current.data; + current = current.next; } + return sum; + } + /** + * Returns the sum of all the vertices in a graph that are reachable from a given + * starting vertex. + * + * Returns 0 if the starting vertex is null. + * + * @param start the starting vertex + * @return the sum of all the vertices + */ + public static int graphSum(Vertex start) { + Set> visited = new HashSet<>(); + return graphSum(start, visited); + } - /** - * Returns true if the sum of the values in a given tree is equal to the sum - * of the values in the given list. - * - * An empty tree or list is considered to have a sum of 0. - * - * @param root The root of the binary tree - * @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 int graphSum(Vertex start, Set> visited) { + if(start == null || visited.contains(start)) return 0; + visited.add(start); + int sum = 0; + sum += start.data; + for(Vertexneighbor : start.neighbors) { + int neighborSum = graphSum(neighbor, visited); + sum += neighborSum; } + return sum; + } - /** - * Returns the sum of all the vertices in a graph that are reachable from a given - * starting vertex. - * - * Returns 0 if the starting vertex is null. - * - * @param start the starting vertex - * @return the sum of all the vertices - */ - public static int graphSum(Vertex start) { - return 0; - } + /** + * Returns the count of vertices in a graph that have an outdegree of 0. + * + * Returns 0 if the starting vertex is null. + * + * @param start the entrypoint to the graph + * @return the count of vertices with outdegree 0 + */ + public static int sinkCount(Vertex start) { + Set> visited = new HashSet<>(); + return sinkCount(start, visited); + } - /** - * Returns the count of vertices in a graph that have an outdegree of 0. - * - * Returns 0 if the starting vertex is null. - * - * @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, Set> visited) { + if(start == null || visited.contains(start)) return 0; + visited.add(start); + if (start.neighbors == null || start.neighbors.size() == 0) return 1; + int count = 0; + for (Vertex neighbor : start.neighbors) { + if (neighbor != null) { + int neighborCount = sinkCount(neighbor, visited); + count += neighborCount; + } } + return count; + } } \ No newline at end of file