From 149936dca48b6c9608dfe6d50ebd1ebbfe49b3dc Mon Sep 17 00:00:00 2001 From: dangrabo <193651636+dangrabo@users.noreply.github.com> Date: Tue, 23 Sep 2025 20:23:04 -0700 Subject: [PATCH 1/4] completed some problems in class --- src/Practice.java | 73 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..f0fc263 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,4 @@ -import java.util.Map; -import java.util.Set; +import java.util.*; public class Practice { /** @@ -11,7 +10,12 @@ 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 num : nums) { + if (num % 2 != 0) sum += num; + } + return sum; } /** @@ -26,7 +30,15 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + String shortestWord = ""; + for (String word : words) { + if (shortestWord.length() == 0) shortestWord = word; + if (word.length() < shortestWord.length()) shortestWord = word; + if (word.length() == shortestWord.length()) { + if (word.compareTo(shortestWord) < 0) shortestWord = word; + } + } + return shortestWord; } /** @@ -39,7 +51,11 @@ public static String shortestWord(Set words) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { - return null; + Set results = new HashSet(); + for (String name : ages.keySet()){ + if (ages.get(name) >= 18) results.add(name); + } + return results; } /** @@ -50,7 +66,14 @@ 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 biggestNumber = head.data; + ListNode current = head; + while (current != null) { + if (current.data > biggestNumber) biggestNumber = current.data; + current = current.next; + } + return biggestNumber; } /** @@ -67,7 +90,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 results = new HashMap<>(); + if (head == null) return results; + ListNode current = head; + while (current != null) { + if (results.containsKey(current.data)) { + results.put(current.data, results.get(current.data) + 1); + } else { + results.put(current.data, 1); + } + current = current.next; + } + + return results; } @@ -80,7 +115,8 @@ 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; + return 1 + Math.max(levelCount(root.left), levelCount(root.right)); } @@ -108,7 +144,26 @@ 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 levelTrack = level; + Queue> q = new LinkedList<>(); + q.add(root); + while (levelTrack > 1) { + int i = q.size(); + while (i > 0) { + System.out.println("i" + i); + BinaryTreeNode current = q.poll(); + if (current.right != null) q.add(current.right); + if (current.left != null) q.add(current.left); + i--; + } + levelTrack--; + System.out.println("new lt " + levelTrack); + } + int sum = 0; + for (BinaryTreeNode current : q) sum += current.data; + System.out.println(sum); + return sum; } From ffa1f30d263dc16dfd354b3c6bc770d3e5da2b18 Mon Sep 17 00:00:00 2001 From: dangrabo <193651636+dangrabo@users.noreply.github.com> Date: Tue, 23 Sep 2025 20:38:34 -0700 Subject: [PATCH 2/4] completed another problem --- src/Practice.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index f0fc263..8e58108 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -178,7 +178,22 @@ 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; + return sumMatchTreeHelper(root) == sumMatchListHelper(head); + } + + public static int sumMatchTreeHelper(BinaryTreeNode root) { + if (root == null) return 0; + return root.data + sumMatchTreeHelper(root.left) + sumMatchTreeHelper(root.right); + } + + public static int sumMatchListHelper(ListNode head) { + if (head == null) return 0; + int sum = 0; + while (head != null) { + sum += head.data; + head = head.next; + } + return sum; } /** From 8b187780b332c974146e9e4a3ab45d4b35782f17 Mon Sep 17 00:00:00 2001 From: dangrabo <193651636+dangrabo@users.noreply.github.com> Date: Tue, 23 Sep 2025 20:53:07 -0700 Subject: [PATCH 3/4] comleted a problem --- src/Practice.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 8e58108..fb6c8af 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -206,7 +206,18 @@ public static int sumMatchListHelper(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<>(); + graphSum(start, visited); + int sum = 0; + for (Vertex current : visited) sum += current.data; + return sum; + } + + public static void graphSum(Vertex current, Set> visited) { + if (current == null || visited.contains(current)) return; + visited.add(current); + for (Vertex neighbor : current.neighbors) graphSum(neighbor, visited); } /** From 3ecdb08be557b6bf109beac85f41a3ea76dd463b Mon Sep 17 00:00:00 2001 From: dangrabo <193651636+dangrabo@users.noreply.github.com> Date: Tue, 23 Sep 2025 20:58:57 -0700 Subject: [PATCH 4/4] completed problemset --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index fb6c8af..eac7b49 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -229,6 +229,19 @@ public static void graphSum(Vertex current, Set> visite * @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<>(); + sinkCount(start, visited); + int count = 0; + for (Vertex current : visited) { + if (current.neighbors.size() == 0 || current.neighbors == null) count++; + } + return count; + } + + public static void sinkCount(Vertex current, Set> visited) { + if (current == null || visited.contains(current)) return; + visited.add(current); + for (Vertex neighbor : current.neighbors) sinkCount(neighbor, visited); } } \ No newline at end of file