From 283c8a12fb7b29e8d20b7208c534387b53e31c66 Mon Sep 17 00:00:00 2001 From: xavierb117 <194048107+xavierb117@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:26:05 -0700 Subject: [PATCH 01/10] Finshed oddSum --- src/Practice.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..746a4da 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,7 +11,19 @@ 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 == 1 || num % 2 == -1) { + sum += num; + } + } + + return sum; } /** From 850a4ef8c08947db14fddb30dadce5a9fa801a8d Mon Sep 17 00:00:00 2001 From: xavierb117 <194048107+xavierb117@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:43:59 -0700 Subject: [PATCH 02/10] Finished shortestWord --- src/Practice.java | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 746a4da..e2cc217 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -38,7 +38,39 @@ 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.isEmpty()) { + throw new IllegalArgumentException(); + } + + int shortestCount = Integer.MAX_VALUE; + String shortestWord = ""; + + for (String word : words) { + if (word.length() < shortestCount) { + shortestCount = word.length(); + shortestWord = word; + } + else if (word.length() == shortestCount) { + for (int i = 0; i < shortestCount; i++) { + char first = word.charAt(i); + char second = shortestWord.charAt(i); + + if (first < second) { + shortestWord = word; + break; + } + else if (first > second) { + break; + } + } + } + } + + return shortestWord; } /** From 54ce73f527c2956c57b67c9d7745b1f9fe389f82 Mon Sep 17 00:00:00 2001 From: xavierb117 <194048107+xavierb117@users.noreply.github.com> Date: Wed, 24 Sep 2025 20:02:26 -0700 Subject: [PATCH 03/10] Finished adults --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index e2cc217..d1fa3e6 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; @@ -83,7 +84,19 @@ else if (first > second) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { - return null; + if (ages == null) { + throw new NullPointerException(); + } + + Set adults = new HashSet<>(); + + for (String name : ages.keySet()) { + if (ages.get(name) >= 18) { + adults.add(name); + } + } + + return adults; } /** From 4e50ce357da4b5ae5edd88d9163b00fd5667c546 Mon Sep 17 00:00:00 2001 From: xavierb117 <194048107+xavierb117@users.noreply.github.com> Date: Wed, 24 Sep 2025 20:30:19 -0700 Subject: [PATCH 04/10] Finished biggestNumber --- src/Practice.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index d1fa3e6..8643c35 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -87,7 +87,7 @@ public static Set adults(Map ages) { if (ages == null) { throw new NullPointerException(); } - + Set adults = new HashSet<>(); for (String name : ages.keySet()) { @@ -107,7 +107,22 @@ 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 biggestNum = Integer.MIN_VALUE; + + ListNode curr = head; + + while (curr != null) { + if (curr.data > biggestNum) { + biggestNum = curr.data; + } + curr = curr.next; + } + + return biggestNum; } /** From 717896eace85a296372541b5c897e0558dc8dc4e Mon Sep 17 00:00:00 2001 From: xavierb117 <194048107+xavierb117@users.noreply.github.com> Date: Wed, 24 Sep 2025 20:40:46 -0700 Subject: [PATCH 05/10] Finished frequencies --- src/Practice.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 8643c35..4c613ea 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; @@ -139,7 +140,21 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map frequency = new HashMap<>(); + + ListNode curr = head; + + while (curr != null) { + if (frequency.containsKey(curr.data)) { + frequency.put(curr.data, frequency.get(curr.data) + 1); + } + else{ + frequency.put(curr.data, 1); + } + curr = curr.next; + } + + return frequency; } From 938bd30f0b9f9c7b91785e129b82da7ab7ae38bc Mon Sep 17 00:00:00 2001 From: xavierb117 <194048107+xavierb117@users.noreply.github.com> Date: Wed, 24 Sep 2025 21:23:49 -0700 Subject: [PATCH 06/10] Finished levelCount --- src/Practice.java | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 4c613ea..69e2f99 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,6 +1,9 @@ +import java.util.ArrayList; 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 { @@ -167,7 +170,38 @@ 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 level = 0; + + ArrayList> arr = new ArrayList<>(); + Queue>> queue = new LinkedList<>(); + + arr.add(root); + queue.offer(arr); + + while(!queue.isEmpty()) { + ArrayList> curr = queue.poll(); + ArrayList> next = new ArrayList<>(); + + for (BinaryTreeNode node : curr) { + if (node.left != null) { + next.add(node.left); + } + if (node.right != null) { + next.add(node.right); + } + } + + if (!next.isEmpty()) { + queue.offer(next); + } + level += 1; + } + + return level; } From 278dddd9e893f77858c41026d69260ba9fb353d8 Mon Sep 17 00:00:00 2001 From: xavierb117 <194048107+xavierb117@users.noreply.github.com> Date: Wed, 24 Sep 2025 22:18:23 -0700 Subject: [PATCH 07/10] Finished sumAtLevel --- src/Practice.java | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 69e2f99..82829a2 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -229,7 +229,52 @@ 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 <= 0) { + return 0; + } + + int currLevel = 1; + + ArrayList> arr = new ArrayList<>(); + Queue>> queue = new LinkedList<>(); + + arr.add(root); + queue.offer(arr); + + while (!queue.isEmpty()) { + if (currLevel == level) { + break; + } + + ArrayList> curr = queue.poll(); + ArrayList> next = new ArrayList<>(); + + for (BinaryTreeNode node : curr) { + if (node.left != null) { + next.add(node.left); + } + if (node.right != null) { + next.add(node.right); + } + } + + if (!next.isEmpty()) { + queue.offer(next); + } + currLevel += 1; + } + + if (currLevel < level) { + return 0; + } + + int sum = 0; + ArrayList> curr = queue.poll(); + for (BinaryTreeNode node : curr) { + sum += node.data; + } + + return sum; } From 1b3ba2a56941a05821d65ac2180c9062d95f7e5c Mon Sep 17 00:00:00 2001 From: xavierb117 <194048107+xavierb117@users.noreply.github.com> Date: Wed, 24 Sep 2025 22:41:06 -0700 Subject: [PATCH 08/10] Finished sumMatch --- src/Practice.java | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 82829a2..cb267eb 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -289,7 +289,35 @@ 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 tree = treehelper(root); + int linked = listnodeHelper(head); + + if (tree == linked) { + return true; + } + else { + return false; + } + } + + public static int treehelper(BinaryTreeNode root) { + if (root == null) { + return 0; + } + + return root.data + treehelper(root.left) + treehelper(root.right); + } + + public static int listnodeHelper(ListNode head) { + int sum = 0; + + ListNode curr = head; + + while (curr != null) { + sum += curr.data; + curr = curr.next; + } + return sum; } /** From c923106859185c3069d02ea3b57f5353fd7b2665 Mon Sep 17 00:00:00 2001 From: xavierb117 <194048107+xavierb117@users.noreply.github.com> Date: Wed, 24 Sep 2025 23:21:46 -0700 Subject: [PATCH 09/10] Finished graphSum --- src/Practice.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index cb267eb..a42fd25 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -330,7 +330,27 @@ public static int listnodeHelper(ListNode head) { * @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 graphHelper(start, visited); + } + + public static int graphHelper(Vertex start, Set> visited) { + if (start == null || visited.contains(start)) { + return 0; + } + + visited.add(start); + int sum = 0; + sum += start.data; + + for (Vertex neighbor : start.neighbors) { + sum += graphHelper(neighbor, visited); + } + + return sum; } /** From 041540818b239c9a12fbbd7158344688714236cc Mon Sep 17 00:00:00 2001 From: xavierb117 <194048107+xavierb117@users.noreply.github.com> Date: Wed, 24 Sep 2025 23:28:24 -0700 Subject: [PATCH 10/10] Finished sinkCount --- src/Practice.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index a42fd25..c2ba406 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -362,6 +362,26 @@ public static int graphHelper(Vertex start, Set> visite * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + Set> visited = new HashSet<>(); + return sinkCountHelper(start, visited); + } + + public static int sinkCountHelper(Vertex start, Set> visited) { + if (start == null || visited.contains(start)) { + return 0; + } + + visited.add(start); + int outdegree = 0; + + if (start.neighbors.size() == 0) { + outdegree += 1; + } + + for (Vertex neighbor : start.neighbors) { + outdegree += sinkCountHelper(neighbor, visited); + } + + return outdegree; } } \ No newline at end of file