From f9eac6e28f78ed1bf01b7409392ffb7a8b35b5e9 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:34:04 -0700 Subject: [PATCH 01/11] oddSum working tests --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..4540e43 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,7 +11,17 @@ 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 count = 0; + + + for(int i = 0; i < nums.length; i++){ + if(nums[i] % 2 != 0){ + count += nums[i]; + } + } + return count; + } /** From c3578021ff81669fdb2ec331d1dd3512fe70d677 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:37:22 -0700 Subject: [PATCH 02/11] shortestWord tests working --- src/Practice.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 4540e43..67b462d 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -36,7 +36,24 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + if(words.size() == 0) throw new IllegalArgumentException(); + if(words == null) throw new NullPointerException(); + + + String shortest = null; + + + for (String word : words) { + if(shortest == null) shortest = word; + else if(word.length() < shortest.length()) shortest = word; + else if(word.length() == shortest.length()){ + if(word.compareTo(shortest) < 0) shortest = word; + + } + } + + + return shortest; } /** From 38be5461bc7f07c83b0af145815c7e70908a4522 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:37:39 -0700 Subject: [PATCH 03/11] shortestWord tests working --- src/Practice.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 67b462d..cfd2fdc 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -48,7 +48,6 @@ public static String shortestWord(Set words) { else if(word.length() < shortest.length()) shortest = word; else if(word.length() == shortest.length()){ if(word.compareTo(shortest) < 0) shortest = word; - } } From b099a91ffdd46995a7e5040f642d965de759531b Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:42:10 -0700 Subject: [PATCH 04/11] adults working tests --- src/Practice.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index cfd2fdc..592eb02 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -65,7 +66,13 @@ else if(word.length() == shortest.length()){ * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { - return null; + if(ages == null) throw new NullPointerException(); + Set set = new HashSet<>(); + + for (String name : ages.keySet()) { + if(ages.get(name) >= 18) set.add(name); + } + return set; } /** From 744f7faed6dc4cc76da838a09615d870fe5e3e45 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:50:08 -0700 Subject: [PATCH 05/11] biggestNumber working tests --- src/Practice.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 592eb02..a9f0ba6 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -83,7 +83,16 @@ 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 temp = head; + int max = Integer.MIN_VALUE; + + while(temp!= null){ + if(temp.data > max) max = temp.data; + temp = temp.next; + } + return max; } /** From 8890b8330e28f00ab03a95ed37aa422abd8fb805 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Wed, 24 Sep 2025 17:19:45 -0700 Subject: [PATCH 06/11] freq working tests --- src/Practice.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index a9f0ba6..731ee89 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -109,7 +110,22 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map map = new HashMap<>(); + if(head == null) return map; + + ListNode temp = head; + + while(temp != null){ + if(!map.containsKey(temp.data)){ + map.put(temp.data, map.getOrDefault(temp.data, 0)+1); + }else{ + map.put(temp.data, map.getOrDefault(temp.data, 0)+1); + } + + temp = temp.next; + } + + return map; } From 3577e00c118c601cd0a20dc06c63693307c23fd7 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Wed, 24 Sep 2025 17:39:58 -0700 Subject: [PATCH 07/11] levelCount working tests --- src/Practice.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 731ee89..05788de 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -138,7 +138,12 @@ 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; } From c2c03a8606e349de335e89383f0d6c201373d011 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Wed, 24 Sep 2025 18:24:26 -0700 Subject: [PATCH 08/11] sumMatch working tests --- src/Practice.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 05788de..e269c4f 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -171,6 +171,7 @@ 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; } @@ -186,7 +187,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 sumTree = sumOfTree(root); + int sumList = 0; + + ListNode temp = head; + while(temp != null){ + sumList += temp.data; + temp = temp.next; + } + + return sumList == sumTree; + } + + private static int sumOfTree(BinaryTreeNode root){ + if(root == null) return 0; + + int sum = sumOfTree(root.left) + sumOfTree(root.right) + root.data; + + + + return sum; } /** From c93608700d3d4af316cd12727320b8e356863fe1 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Wed, 24 Sep 2025 18:37:53 -0700 Subject: [PATCH 09/11] graphSum working test --- src/Practice.java | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index e269c4f..a56aba3 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -200,12 +200,9 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h } private static int sumOfTree(BinaryTreeNode root){ - if(root == null) return 0; - + if(root == null) return 0; int sum = sumOfTree(root.left) + sumOfTree(root.right) + root.data; - - return sum; } @@ -219,7 +216,21 @@ private static int sumOfTree(BinaryTreeNode root){ * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + Set> visited = new HashSet<>(); + return graphSum(start, visited); + } + + private static int graphSum(Vertex start, Set> visited){ + if(start == null || visited.contains(start)) return 0; + + visited.add(start); + int total = start.data; + + for(Vertex neighbor : start.neighbors){ + total += graphSum(neighbor, visited); + } + + return total; } /** From 0a6bd05d0eeac824c787d6d1f391b7ee8b6ec718 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Wed, 24 Sep 2025 19:01:50 -0700 Subject: [PATCH 10/11] sinkCount working tests --- src/Practice.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index a56aba3..7ea0afa 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -242,6 +242,22 @@ private static int graphSum(Vertex start, Set> visited) * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + Set> visited = new HashSet<>(); + return sinkCount(start, visited); + } + + private static int sinkCount(Vertex start, Set> visited){ + if(start == null || visited.contains(start)) return 0; + + visited.add(start); + int sink = 0; + + if(start.neighbors.isEmpty()) sink++; + + for (Vertex neighbor : start.neighbors) { + sink += sinkCount(neighbor, visited); + } + + return sink; } } \ No newline at end of file From 2b6dc7cb15d5641cce8fe62979e7877eeabecbb1 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Wed, 24 Sep 2025 19:33:05 -0700 Subject: [PATCH 11/11] sumAtLevel working tests --- src/Practice.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 7ea0afa..f8274fe 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -171,8 +171,15 @@ 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 sumAtLevel(root, level,1); + } + + private static int sumAtLevel(BinaryTreeNode root, int level, int curLvl){ + if(root == null) return 0; + + if(curLvl == level) return root.data; - return 0; + return sumAtLevel(root.left, level, curLvl+1) + sumAtLevel(root.right, level, curLvl+1); }