From 71eca5852165ed36225a74a3ea91ebd20d101da4 Mon Sep 17 00:00:00 2001 From: VibeSyntax <138536097+beza102@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:26:02 -0700 Subject: [PATCH 1/5] completed the oddSum and run the tests --- src/Practice.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..49bbf3e 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,7 +11,21 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + int sum =0; + if (nums ==null){ + return 0; + } + for(int i=0; i Date: Wed, 24 Sep 2025 02:51:55 -0700 Subject: [PATCH 2/5] completed the shortestWord method and run all the tests --- src/Practice.java | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 49bbf3e..fd6db7d 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,5 @@ +import java.util.Collections; +import java.util.IllegalFormatCodePointException; import java.util.Map; import java.util.Set; @@ -40,7 +42,29 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + String shortest =null; + int minLength = Integer.MAX_VALUE; + if (words ==null){ + throw new NullPointerException(); + } + if(words.isEmpty()){ + throw new IllegalArgumentException(); + } + for(String word:words){ + if(word.length() Date: Wed, 24 Sep 2025 04:12:45 -0700 Subject: [PATCH 3/5] completed and run tests for the adult method --- src/Practice.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index fd6db7d..14939db 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,4 +1,5 @@ import java.util.Collections; +import java.util.HashSet; import java.util.IllegalFormatCodePointException; import java.util.Map; import java.util.Set; @@ -77,7 +78,21 @@ public static String shortestWord(Set words) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { - return null; + int age=0; + if(ages ==null){ + throw new NullPointerException(); + } + Set adultNames= new HashSet<>(); + for(String name:ages.keySet()){ + age=ages.get(name); + if(age>=18){ + adultNames.add(name); + } + + } + + + return adultNames; } /** From 79f09355dec6ec84f8d2d55dd23d65fca98cec96 Mon Sep 17 00:00:00 2001 From: VibeSyntax <138536097+beza102@users.noreply.github.com> Date: Wed, 24 Sep 2025 14:27:40 -0700 Subject: [PATCH 4/5] completed the biggestNumber method and run the test case --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 14939db..c11086a 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -103,7 +103,20 @@ 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(); + } + ListNode current=head; + int max = Integer.MIN_VALUE; + while(current!=null){ + if(current.data>max){ + max=current.data; + current=current.next; + } + } + + + return max; } /** From 3340f08d52a24baaaca7052389cb301b6c93c14b Mon Sep 17 00:00:00 2001 From: VibeSyntax <138536097+beza102@users.noreply.github.com> Date: Wed, 24 Sep 2025 23:55:56 -0700 Subject: [PATCH 5/5] completed the methods and run all the tests --- src/Practice.java | 107 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 101 insertions(+), 6 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index c11086a..50aa88b 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,4 +1,5 @@ import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.IllegalFormatCodePointException; import java.util.Map; @@ -133,7 +134,24 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + MapfreqMap= new HashMap<>(); + if(head==null){ + return freqMap; + + } + ListNode current=head; + while(current!=null){ + if(freqMap.containsKey(current.data)){ + //add 1 if we have seen it before + freqMap.put(current.data, freqMap.get(current.data)+1); + }else{ + //first time seeing it, add with count of 1 + freqMap.put(current.data, 1); + } + current=current.next; + } + + return freqMap; } @@ -146,7 +164,20 @@ public static Map frequencies(ListNode head) { * @return the number of levels in the tree */ public static int levelCount(BinaryTreeNode root) { - return 0; + int levels =0; + if(root ==null){ + return 0; + } + if(root.left ==null && root.right==null){ + return 1; + } + if(root.left !=null){ + levels=Math.max(levelCount(root.left), levels); + } + if(root.right !=null){ + levels=Math.max(levelCount(root.right), levels); + } + return levels+1; } @@ -174,7 +205,16 @@ 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<1){ + return 0; + } + if(level==1){ + return root.data; + } + + return sumAtLevel(root.left,level-1) + sumAtLevel(root.right, level-1); + + } @@ -189,7 +229,26 @@ 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 treeSum=treeSum(root); + int listSum=listSum(head); + return treeSum==listSum; + } + //helper methods add all value in the tree + public static int treeSum(BinaryTreeNode root){ + if(root==null){ + return 0; + } + return root.data + treeSum(root.left) + treeSum(root.right); + } + //helper method to add all values in the list + public static int listSum(ListNode head){ + int sum=0; + ListNode current=head; + while(current!=null){ + sum+=current.data; + current=current.next; + } + return sum; } /** @@ -202,7 +261,24 @@ 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 graphSumHelper(start, visited); + } + //helper method to do depth first search + public static int graphSumHelper(Vertex vertex, Set> visited){ + if(visited.contains(vertex)){ + return 0; + } + visited.add(vertex); + //add current vertex data to sum of neighbors + int sum = vertex.data; + for(Vertex neighbor: vertex.neighbors){ + sum += graphSumHelper(neighbor, visited); + } + return sum; } /** @@ -214,6 +290,25 @@ public static int graphSum(Vertex start) { * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + if(start==null){ + return 0; + } + Set> visited = new HashSet<>(); + return sinkCountHelper(start, visited); + } + //helper method to do depth first search + public static int sinkCountHelper(Vertex vertex, Set> visited){ + if(visited.contains(vertex)){ + return 0; + } + visited.add(vertex); + int count =0; + if(vertex.neighbors.isEmpty()){ + count=1; + } + for(Vertex neighbor: vertex.neighbors){ + count += sinkCountHelper(neighbor, visited); + } + return count; } } \ No newline at end of file