From 8d1714faf582ff850ab2bfb8bbd571b43a3d633e Mon Sep 17 00:00:00 2001 From: timmyd4 Date: Tue, 23 Sep 2025 11:24:23 -0700 Subject: [PATCH 01/10] Finished oddSum --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..9cd6d15 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,7 +11,20 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + + int oddCount = 0; + + if(nums == null) return 0; + + for(int items: nums) + { + if(items % 2 != 0) + { + oddCount += items; + } + } + + return oddCount; } /** From db207d5ea4b7f97f06ea720fbc34854de31d8dec Mon Sep 17 00:00:00 2001 From: timmyd4 Date: Tue, 23 Sep 2025 13:00:41 -0700 Subject: [PATCH 02/10] Finished shortestWord --- src/Practice.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 9cd6d15..7c97ec3 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -39,7 +39,24 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + + String smallestWord = null; + + if(words == null) throw new NullPointerException(); + if(words.isEmpty()) throw new IllegalArgumentException(); + + for(String items: words) + { + if(smallestWord == null || items.length() < smallestWord.length() || + (items.length() == smallestWord.length()) && items.compareTo(smallestWord) < 0) + { + smallestWord = items; + } + + } + + + return smallestWord; } /** @@ -52,6 +69,9 @@ public static String shortestWord(Set words) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { + if(ages == null) throw new NullPointerException(); + + return null; } From 4d9b974359eefb5672a8eb6caf43f00b43437b2a Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 23 Sep 2025 16:57:14 -0700 Subject: [PATCH 03/10] Finished frequencies --- src/Practice.java | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 7c97ec3..9edb31f 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,5 @@ +import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -70,9 +72,18 @@ public static String shortestWord(Set words) { */ public static Set adults(Map ages) { if(ages == null) throw new NullPointerException(); + Set people = new HashSet<>(); + + for(Map.Entry items : ages.entrySet()) + { + if(items.getValue() >= 18) + { + people.add(items.getKey()); + } + } - return null; + return people; } /** @@ -83,7 +94,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(); + + int biggest = Integer.MIN_VALUE; + ListNode current = head; + while(current != null) + { + if(biggest < current.data) + { + biggest = current.data; + } + current = current.next; + } + + return biggest; } /** @@ -100,7 +125,19 @@ 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 current = head; + while(current != null) + { + map.put(current.data, map.getOrDefault(current.data, 0) + 1); + current = current.next; + } + + + return map; } From e564bd9947d05e4a179200a26968834a503ffb29 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 23 Sep 2025 17:06:40 -0700 Subject: [PATCH 04/10] Almost forgot to commit, finished levelCount --- src/Practice.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 9edb31f..cee66d6 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -150,7 +150,13 @@ 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 leftSearch = levelCount(root.left); + int rightSearch = levelCount(root.right); + + + return Math.max(leftSearch, rightSearch) + 1; } From 8cecd0e46aedd9d98ffaba198bec45ce257fe4fe Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 23 Sep 2025 17:30:25 -0700 Subject: [PATCH 05/10] Struggling but figured id commit --- src/Practice.java | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index cee66d6..6a7a249 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,6 +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 { @@ -184,7 +186,29 @@ 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) return 0; + + Queue> q = new LinkedList<>(); + q.add(root); + int target = 0; + + while(!q.isEmpty()) + { + int size = q.size(); + int sumed = 0; + + for(int i = 0; i < size; i++) + { + BinaryTreeNode current = q.poll(); + + if(current.left != null) q.add(current.left); + if(current.right != null) q.add(current.right); + } + + target++; + + } + return sumed; } From cb72f6861790ad338980d73231f76ffaf2b5a9e2 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 23 Sep 2025 17:38:52 -0700 Subject: [PATCH 06/10] Finished sumAtLevel --- src/Practice.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 6a7a249..7f76929 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -190,7 +190,7 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { Queue> q = new LinkedList<>(); q.add(root); - int target = 0; + int target = 1; while(!q.isEmpty()) { @@ -200,15 +200,16 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { for(int i = 0; i < size; i++) { BinaryTreeNode current = q.poll(); - + if(target == level) sumed += current.data; if(current.left != null) q.add(current.left); if(current.right != null) q.add(current.right); } + if(target == level) return sumed; target++; } - return sumed; + return 0; } From a5bed3eeeb3608c8aac24f42ee6329591a42f69f Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 23 Sep 2025 17:53:00 -0700 Subject: [PATCH 07/10] Finished sumMatch --- src/Practice.java | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 7f76929..95003d2 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -224,7 +224,34 @@ 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(head == null && root == null) return true; + if(root == null) return false; + if(head == null) return false; + + Queue> q = new LinkedList<>(); + q.add(root); + int BSTSum = 0; + + while(!q.isEmpty()) + { + BinaryTreeNode current = q.poll(); + BSTSum += current.data; + if(current.left != null) q.add(current.left); + if(current.right != null) q.add(current.right); + } + + ListNode current = head; + int LNSum = 0; + + while(current != null) + { + LNSum += current.data; + current = current.next; + } + + + return BSTSum == LNSum; } /** From 2f860a11f4b8d1dabfb245f6026b898726be77fc Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 23 Sep 2025 18:12:39 -0700 Subject: [PATCH 08/10] Finished graphsum, felt tough but starting to come back --- src/Practice.java | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 95003d2..8be7472 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -264,7 +264,25 @@ 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 dfs(start, visited); + } + + + public static int dfs(Vertex start, Set> visited) + { + if(visited.contains(start)) return 0; + visited.add(start); + + int sum = start.data; + + for(Vertex neighbor: start.neighbors) + { + sum += dfs(neighbor, visited); + } + return sum; } /** From fce294d985a719e65fe1f5bbce445f028bfab027 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 23 Sep 2025 18:22:41 -0700 Subject: [PATCH 09/10] taking a break, my brain is mush --- src/Practice.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index 8be7472..f193978 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -294,6 +294,18 @@ public static int dfs(Vertex start, Set> visited) * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { + if(start == null) return 0; + Set> visited = new HashSet<>(); + return sinkCountDFS(start, visited); + } + + public static int sinkCountDFS(Vertex start, Set> visited) + { + if(visited.contains(start)) return 0; + visited.add(start); + + + return 0; } } \ No newline at end of file From 50902db7c1013f5d013d5bb68418ce29d190db92 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 24 Sep 2025 14:55:24 -0700 Subject: [PATCH 10/10] Finished sinkCount --- src/Practice.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index f193978..6b61ecd 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -304,8 +304,18 @@ public static int sinkCountDFS(Vertex start, Set> visit if(visited.contains(start)) return 0; visited.add(start); - + int count = 0; - return 0; + if(start.neighbors.isEmpty()) + { + count++; + } + + for(Vertex neighbor: start.neighbors) + { + count += sinkCountDFS(neighbor, visited); + } + + return count; } } \ No newline at end of file