diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c995aa5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.debug.settings.onBuildFailureProceed": true +} \ No newline at end of file diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..e38d563 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,12 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + if (nums==null||nums.length==0) return 0; + int sum = 0; + for (int i: nums) { + if (i%2==1||i%2==-1) sum+= i; + } + return sum; } /** @@ -26,7 +35,20 @@ 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.size()==0) throw new IllegalArgumentException(); + int shortestlength = Integer.MAX_VALUE; + String shortest = ""; + for (String word : words) { + if (word.length() words) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { - return null; + if (ages==null) throw new NullPointerException(); + Set majors = new HashSet<>(); + for (String name : ages.keySet()) { + if (ages.get(name)>=18) majors.add(name); + } + return majors; } /** @@ -50,7 +77,13 @@ 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 biggest = Integer.MIN_VALUE; + while (head!=null) { + if (head.data>biggest) biggest = head.data; + head = head.next; + } + return biggest; } /** @@ -67,7 +100,18 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + if (head==null) return Map.of(); + Map frequency = new HashMap<>(); + while(head!=null) { + if (frequency.containsKey(head.data)) { + frequency.put(head.data, frequency.get(head.data)+1); + } + else { + frequency.put(head.data, 1); + } + head=head.next; + } + return frequency; } @@ -80,7 +124,10 @@ 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); + return Math.max(left,right)+1; } @@ -108,6 +155,33 @@ 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) return 0; + Queue> queue = new LinkedList<>(); + queue.offer(root); + int currlevel = 1; + int levelsum = 0; + while (!queue.isEmpty()) { + int levelsize = queue.size(); + if (currlevel == level) { + for (int i = 0; i < levelsize; i++) { + BinaryTreeNode node = queue.poll(); + levelsum += node.data; + } + return levelsum; + } + else { + for (int j = 0; j < levelsize; j++) { + BinaryTreeNode node = queue.poll(); + if (node.left!=null) { + queue.offer(node.left); + } + if (node.right!=null) { + queue.offer(node.right); + } + } + currlevel++; + } + } return 0; } @@ -123,7 +197,23 @@ 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; + if (root==null||head==null) return false; + Queue> queue = new LinkedList<>(); + int treesum = 0; + queue.offer(root); + while (!queue.isEmpty()) { + BinaryTreeNode node = queue.poll(); + if (node.left!=null) queue.offer(node.left); + if (node.right!=null) queue.offer(node.right); + treesum+=node.data; + } + int listsum = 0; + while(head!=null) { + listsum+= head.data; + head=head.next; + } + return treesum==listsum; } /** @@ -136,7 +226,17 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + HashSet> visited = new HashSet>(); + return graphSumHelper(start, visited); + } + public static int graphSumHelper(Vertex start, Set> visited) { + if (start==null||visited.contains(start)) return 0; + visited.add(start); + int sum = start.data; + for (Vertex neighbour : start.neighbors) { + sum += graphSumHelper(neighbour, visited); + } + return sum; } /** @@ -148,6 +248,17 @@ public static int graphSum(Vertex start) { * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + HashSet> visited = new HashSet<>(); + return sinkCountHelper(start, visited); + } + public static int sinkCountHelper(Vertex start, Set> visited) { + if (start==null||visited.contains(start)) return 0; + int sinkcount = 0; + visited.add(start); + if (start.neighbors.size()==0) sinkcount++; + for (Vertex neighbour : start.neighbors) { + sinkcount += sinkCountHelper(neighbour, visited); + } + return sinkcount; } } \ No newline at end of file