diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..c2ba406 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,4 +1,9 @@ +import java.util.ArrayList; +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 +16,19 @@ 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 sum = 0; + + for (int num:nums) { + if (num % 2 == 1 || num % 2 == -1) { + sum += num; + } + } + + return sum; } /** @@ -26,7 +43,39 @@ 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(); + } + + if (words.isEmpty()) { + throw new IllegalArgumentException(); + } + + int shortestCount = Integer.MAX_VALUE; + String shortestWord = ""; + + for (String word : words) { + if (word.length() < shortestCount) { + shortestCount = word.length(); + shortestWord = word; + } + else if (word.length() == shortestCount) { + for (int i = 0; i < shortestCount; i++) { + char first = word.charAt(i); + char second = shortestWord.charAt(i); + + if (first < second) { + shortestWord = word; + break; + } + else if (first > second) { + break; + } + } + } + } + + return shortestWord; } /** @@ -39,7 +88,19 @@ 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 adults = new HashSet<>(); + + for (String name : ages.keySet()) { + if (ages.get(name) >= 18) { + adults.add(name); + } + } + + return adults; } /** @@ -50,7 +111,22 @@ 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(); + } + + int biggestNum = Integer.MIN_VALUE; + + ListNode curr = head; + + while (curr != null) { + if (curr.data > biggestNum) { + biggestNum = curr.data; + } + curr = curr.next; + } + + return biggestNum; } /** @@ -67,7 +143,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 frequency = new HashMap<>(); + + ListNode curr = head; + + while (curr != null) { + if (frequency.containsKey(curr.data)) { + frequency.put(curr.data, frequency.get(curr.data) + 1); + } + else{ + frequency.put(curr.data, 1); + } + curr = curr.next; + } + + return frequency; } @@ -80,7 +170,38 @@ 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 level = 0; + + ArrayList> arr = new ArrayList<>(); + Queue>> queue = new LinkedList<>(); + + arr.add(root); + queue.offer(arr); + + while(!queue.isEmpty()) { + ArrayList> curr = queue.poll(); + ArrayList> next = new ArrayList<>(); + + for (BinaryTreeNode node : curr) { + if (node.left != null) { + next.add(node.left); + } + if (node.right != null) { + next.add(node.right); + } + } + + if (!next.isEmpty()) { + queue.offer(next); + } + level += 1; + } + + return level; } @@ -108,7 +229,52 @@ 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 <= 0) { + return 0; + } + + int currLevel = 1; + + ArrayList> arr = new ArrayList<>(); + Queue>> queue = new LinkedList<>(); + + arr.add(root); + queue.offer(arr); + + while (!queue.isEmpty()) { + if (currLevel == level) { + break; + } + + ArrayList> curr = queue.poll(); + ArrayList> next = new ArrayList<>(); + + for (BinaryTreeNode node : curr) { + if (node.left != null) { + next.add(node.left); + } + if (node.right != null) { + next.add(node.right); + } + } + + if (!next.isEmpty()) { + queue.offer(next); + } + currLevel += 1; + } + + if (currLevel < level) { + return 0; + } + + int sum = 0; + ArrayList> curr = queue.poll(); + for (BinaryTreeNode node : curr) { + sum += node.data; + } + + return sum; } @@ -123,7 +289,35 @@ 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; + int tree = treehelper(root); + int linked = listnodeHelper(head); + + if (tree == linked) { + return true; + } + else { + return false; + } + } + + public static int treehelper(BinaryTreeNode root) { + if (root == null) { + return 0; + } + + return root.data + treehelper(root.left) + treehelper(root.right); + } + + public static int listnodeHelper(ListNode head) { + int sum = 0; + + ListNode curr = head; + + while (curr != null) { + sum += curr.data; + curr = curr.next; + } + return sum; } /** @@ -136,7 +330,27 @@ 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 graphHelper(start, visited); + } + + public static int graphHelper(Vertex start, Set> visited) { + if (start == null || visited.contains(start)) { + return 0; + } + + visited.add(start); + int sum = 0; + sum += start.data; + + for (Vertex neighbor : start.neighbors) { + sum += graphHelper(neighbor, visited); + } + + return sum; } /** @@ -148,6 +362,26 @@ public static int graphSum(Vertex start) { * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + Set> visited = new HashSet<>(); + return sinkCountHelper(start, visited); + } + + public static int sinkCountHelper(Vertex start, Set> visited) { + if (start == null || visited.contains(start)) { + return 0; + } + + visited.add(start); + int outdegree = 0; + + if (start.neighbors.size() == 0) { + outdegree += 1; + } + + for (Vertex neighbor : start.neighbors) { + outdegree += sinkCountHelper(neighbor, visited); + } + + return outdegree; } } \ No newline at end of file