diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..4a0e7c5 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,4 +1,8 @@ +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; import java.util.Map; +import java.util.Queue; import java.util.Set; public class Practice { @@ -11,7 +15,20 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + if(nums == null) + { + return 0; + } + int odds = 0; + + for(int i = 0; i<=nums.length; i++) + { + if(nums[i] % 2 != 0) + { + odds = odds + nums[i]; + } + } + return odds; } /** @@ -26,7 +43,21 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + String shortWord = ""; + + if(words == null || words.size() == 0) + { + throw new IllegalArgumentException("Not allowed"); + } + + for(var word : words) + { + if(word.length() < shortWord.length()) + { + shortWord = word; + } + } + return shortWord; } /** @@ -39,7 +70,18 @@ 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("Head cannot be null"); + } + Set grownUps = new HashSet<>(); + for (String names : ages.keySet()) { + if(ages.get(names) >= 18) + { + grownUps.add(names); + } + } + return null; } /** @@ -50,7 +92,21 @@ 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 cannot be null"); + } + int bigNum = head.data; + while(head != null) + { + if(head.data > bigNum) + { + bigNum = head.data; + } + head = head.next; + } + + return bigNum; } /** @@ -67,7 +123,20 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map counts = new HashMap<>(); + while(head != null) + { + if(counts.containsKey(head.data)) + { + counts.put(head.data, counts.get(head.data) + 1); + } + else + { + counts.put(head.data, 1); + } + head = head.next; + } + return counts; } @@ -80,7 +149,18 @@ 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; + } } @@ -108,8 +188,36 @@ public static int levelCount(BinaryTreeNode root) { * @return the sum of the nodes at the given level */ public static int sumAtLevel(BinaryTreeNode root, int level) { + + if(root == null || level < 1) + { + return 0; + } + Queue> queue = new LinkedList<>(); + queue.add(root); + int currentLevel = 1; + int sum = 0; + while(!queue.isEmpty()) { + int size = queue.size(); + for(int i = 0; i < size; i++) { + BinaryTreeNode node = queue.poll(); + if(currentLevel == level) { + sum += node.data; + } + if(node.left != null) { + queue.add(node.left); + } + if(node.right != null) { + queue.add(node.right); + } + } + if(currentLevel == level) { + return sum; + } + currentLevel++; + } return 0; - } + } /** @@ -123,7 +231,27 @@ 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(treeSum(root) == listSum(head)) + { + return true; + } + else return false; + } + public static int treeSum(BinaryTreeNode node) { + if(node == null) + { + return 0; + } + return node.data + treeSum(node.left) + treeSum(node.right); + } + public static int listSum(ListNode head) { + int sum = 0; + while(head != null) + { + sum += head.data; + head = head.next; + } + return sum; } /** @@ -136,9 +264,30 @@ 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 graphSum(start, visited); } + public static int graphSum(Vertex node, Set> visited) { + if(node == null || visited.contains(node)) + { + return 0; + } + visited.add(node); + + int sum = node.data; + for(Vertex neighbor : node.neighbors) + { + sum+=graphSum(neighbor, visited); + } + return sum; + } + + /** * Returns the count of vertices in a graph that have an outdegree of 0. *