From babe24736bbfd363b2bbd15784d6ac70354ea925 Mon Sep 17 00:00:00 2001 From: Abdi <89213120+abdirashidexe@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:18:47 -0700 Subject: [PATCH 01/10] oddSum complete --- src/Practice.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..ce27010 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,7 +11,18 @@ 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 != 0) + { + sum += num; + } + } + + return sum; } /** From 632f6106fecb37fbe52a9b745a8cfc564ea22718 Mon Sep 17 00:00:00 2001 From: Abdi <89213120+abdirashidexe@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:33:19 -0700 Subject: [PATCH 02/10] shortestWord solved! --- src/Practice.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ce27010..7aeb665 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,5 @@ +import java.util.ArrayList; +import java.util.Collections; import java.util.Map; import java.util.Set; @@ -37,7 +39,25 @@ 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(); + + ArrayList list = new ArrayList<>(); + list.addAll(words); + Collections.sort(list); + + String shortestWord = list.get(0); + + for (String word : words) + { + if (word.length() < shortestWord.length()) + { + shortestWord = word; + } + } + + return shortestWord; } /** From 3fb18e9f4d2a6e354eeb2f5ffe68f1dbe7849011 Mon Sep 17 00:00:00 2001 From: Abdi <89213120+abdirashidexe@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:39:16 -0700 Subject: [PATCH 03/10] adults solved --- src/Practice.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 7aeb665..cb01a34 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,6 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -70,7 +71,20 @@ 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 mySet = new HashSet<>(); + + for (String name : ages.keySet()) + { + if (ages.get(name) >= 18) + { + mySet.add(name); + } + } + + return mySet; } /** From cf451a784b96e2434f8da4afe9801f86fff2a7f2 Mon Sep 17 00:00:00 2001 From: Abdi <89213120+abdirashidexe@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:46:39 -0700 Subject: [PATCH 04/10] biggestNumber solved --- src/Practice.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index cb01a34..cc7e6cb 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -95,7 +95,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(); + + ListNode current = head; + int biggest = current.data; + + while (current.next != null) + { + if (current.data > biggest) + { + biggest = current.data; + } + current = current.next; + } + return biggest; } /** From 81fa9618ba19a4114e539bedaed94b0051dcde5e Mon Sep 17 00:00:00 2001 From: Abdi <89213120+abdirashidexe@users.noreply.github.com> Date: Wed, 24 Sep 2025 11:48:14 -0700 Subject: [PATCH 05/10] frequencies complete --- src/Practice.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index cc7e6cb..3a16344 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,6 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -126,7 +127,25 @@ 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 new HashMap<>(); + + Map valuesMap = new HashMap<>(); + ListNode current = head; + + while (current != null) + { + if (!valuesMap.containsKey(current.data)) + { + valuesMap.put(current.data, 1); + } else { + int temp = valuesMap.get(current.data) + 1; + valuesMap.put(current.data, temp); + } + current = current.next; + } + + return valuesMap; } From f742e875230c2adbe76f31d6174846b4b9311439 Mon Sep 17 00:00:00 2001 From: Abdi <89213120+abdirashidexe@users.noreply.github.com> Date: Wed, 24 Sep 2025 12:08:16 -0700 Subject: [PATCH 06/10] levelCount complete --- src/Practice.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 3a16344..bb31527 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -158,7 +158,22 @@ 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; + } + + int leftLevels = levelCount(root.left); + int rightLevels = levelCount(root.right); + + levels = Math.max(leftLevels, rightLevels); + + return levels; } From ab13b513ce682a12454141c746acaffcb0f51639 Mon Sep 17 00:00:00 2001 From: Abdi <89213120+abdirashidexe@users.noreply.github.com> Date: Wed, 24 Sep 2025 13:00:58 -0700 Subject: [PATCH 07/10] sumAtLevel complete --- src/Practice.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index bb31527..28c99ce 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -173,7 +173,7 @@ public static int levelCount(BinaryTreeNode root) { levels = Math.max(leftLevels, rightLevels); - return levels; + return 1 + levels; } @@ -201,9 +201,20 @@ 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; + + if (level == 1) return root.data; + + int sum = 0; + + int leftSum = sumAtLevel(root.left, level-1); + int rightSum = sumAtLevel(root.right, level-1); + + sum = leftSum + rightSum; + + return sum; + } /** * Returns true if the sum of the values in a given tree is equal to the sum From ed22a04ec430aeb0f702c79c9c82b887954d4215 Mon Sep 17 00:00:00 2001 From: Abdi <89213120+abdirashidexe@users.noreply.github.com> Date: Wed, 24 Sep 2025 14:33:50 -0700 Subject: [PATCH 08/10] sumMatch solved --- src/Practice.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index 28c99ce..ebfd757 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -227,9 +227,38 @@ 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) { + + if (sumLinkedList(head) == sumTree(root)) return true; + return false; } + public static int sumLinkedList(ListNode head) + { + int total = 0; + ListNode current = head; + + while (current != null) + { + total += current.data; + current = current.next; + } + + return total; + } + + public static int sumTree(BinaryTreeNode root) + { + if (root == null) return 0; + + int sumLeft = sumTree(root.left); + int sumRight = sumTree(root.right); + + int total = root.data + sumLeft + sumRight; + + return total; + } + /** * Returns the sum of all the vertices in a graph that are reachable from a given * starting vertex. From 5b6aff5fd89c91dd9c7773674b849496489dac3b Mon Sep 17 00:00:00 2001 From: Abdi <89213120+abdirashidexe@users.noreply.github.com> Date: Wed, 24 Sep 2025 14:51:59 -0700 Subject: [PATCH 09/10] graphSum solved --- src/Practice.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ebfd757..91c18e1 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -269,7 +269,24 @@ public static int sumTree(BinaryTreeNode root) * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + HashSet> myVisited = new HashSet<>(); + + return graphSumHelper(start, myVisited); + } + + public static int graphSumHelper(Vertex vertex, Set> visited) + { + if (vertex == null || visited.contains(vertex)) return 0; + + visited.add(vertex); + int total = vertex.data; + + for (Vertex neighbor : vertex.neighbors) + { + total += graphSumHelper(neighbor, visited); + } + + return total; } /** From ed7496e906fda9c0f94906a2114f6d39cb992267 Mon Sep 17 00:00:00 2001 From: Abdi <89213120+abdirashidexe@users.noreply.github.com> Date: Wed, 24 Sep 2025 15:13:26 -0700 Subject: [PATCH 10/10] sinkCount finished --- src/Practice.java | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 91c18e1..720d79b 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -298,6 +298,28 @@ public static int graphSumHelper(Vertex vertex, Set> vi * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + HashSet> myVisited = new HashSet<>(); + + return sinkCountHelper(start, myVisited); + } + + public static int sinkCountHelper(Vertex vertex, Set> visited) { + + if (vertex == null || visited.contains(vertex)) return 0; + + int sinkCount = 0; + visited.add(vertex); + + if (vertex.neighbors.size() == 0) + { + sinkCount += 1; + } + + for (Vertex neighbor : vertex.neighbors) + { + sinkCount += sinkCountHelper(neighbor, visited); + } + + return sinkCount; } } \ No newline at end of file