From e190b6136e57f9a0b8f55788de4cd855e227fa1a Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 24 Sep 2025 12:49:44 -0700 Subject: [PATCH 01/10] completed oddSum --- src/Practice.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..5fd80d7 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,7 +11,14 @@ 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 ae37df21ae08f9fe182078a05c93bb10766b798d Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 24 Sep 2025 12:54:34 -0700 Subject: [PATCH 02/10] completed shortestWord --- src/Practice.java | 294 ++++++++++++++++++++++++---------------------- 1 file changed, 154 insertions(+), 140 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 5fd80d7..193d332 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,160 +1,174 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.Set; public class Practice { - /** - * Returns the sum of the odd numbers in the array. - * - * Returns 0 if the array is null or has no odd numbers. - * - * @param nums an array of numbers - * @return the sum of the odd numbers in the array - */ - public static int oddSum(int[] nums) { - if(nums == null) return 0; - int sum = 0; - for(int num : nums) { - if(num % 2 != 0) { - sum += num; - } + /** + * Returns the sum of the odd numbers in the array. + * + * Returns 0 if the array is null or has no odd numbers. + * + * @param nums an array of numbers + * @return the sum of the odd numbers in the array + */ + public static int oddSum(int[] nums) { + if(nums == null) return 0; + int sum = 0; + for(int num : nums) { + if(num % 2 != 0) { + sum += num; } - return sum; } + return sum; + } - /** - * Returns the shortest word in the Set. - * - * If multiple words are tied for shortest, returns the one that is smallest - * lexicographically. - * - * @param words a set of words - * @return the shortest word in the set with a lexicographic tiebreaker - * @throws IllegalArgumentException if words is empty - * @throws NullPointerException if words is null - */ - public static String shortestWord(Set words) { - return null; + /** + * Returns the shortest word in the Set. + * + * If multiple words are tied for shortest, returns the one that is smallest + * lexicographically. + * + * @param words a set of words + * @return the shortest word in the set with a lexicographic tiebreaker + * @throws IllegalArgumentException if words is empty + * @throws NullPointerException if words is null + */ + public static String shortestWord(Set words) { + if(words == null) throw new NullPointerException(); + if (words.size() == 0) throw new IllegalArgumentException(); + List wordList = new ArrayList<>(); + wordList.addAll(words); + Collections.sort(wordList); + String shortest = wordList.get(0); + for( String word: wordList) { + if(word.length() < shortest.length()) { + shortest = word; + } } + return shortest; + } - /** - * Returns a set of all the names of people that are 18 years of age or older. - * - * The input maps name to age in years. - * - * @param ages mapping of name to age - * @return the set of all names of people >= 18 years old - * @throws NullPointerException if ages is null - */ - public static Set adults(Map ages) { - return null; - } + /** + * Returns a set of all the names of people that are 18 years of age or older. + * + * The input maps name to age in years. + * + * @param ages mapping of name to age + * @return the set of all names of people >= 18 years old + * @throws NullPointerException if ages is null + */ + public static Set adults(Map ages) { + return null; + } - /** - * Returns the biggest number in a linked list. - * - * @param head the head of the linked list - * @return the biggest number in the list - * @throws IllegalArgumentException if head is null - */ - public static int biggestNumber(ListNode head) { - return 0; - } + /** + * Returns the biggest number in a linked list. + * + * @param head the head of the linked list + * @return the biggest number in the list + * @throws IllegalArgumentException if head is null + */ + public static int biggestNumber(ListNode head) { + return 0; + } - /** - * Returns a frequency map counting how frequently items appear in a linked list. - * - * Example: - * Input: a -> x -> a -> a -> x -> y - * Output: {a:3, x:2, y: 1} - * - * Returns an empty map if head is null - * - * @param the type of data held by the list - * @param head the head of the list - * @return a frequency map of values in the list - */ - public static Map frequencies(ListNode head) { - return null; - } + /** + * Returns a frequency map counting how frequently items appear in a linked list. + * + * Example: + * Input: a -> x -> a -> a -> x -> y + * Output: {a:3, x:2, y: 1} + * + * Returns an empty map if head is null + * + * @param the type of data held by the list + * @param head the head of the list + * @return a frequency map of values in the list + */ + public static Map frequencies(ListNode head) { + return null; + } - /** - * Returns the number of levels in the tree. - * - * An empty tree has 0 levels, a tree with only a root has 1 level. - * - * @param root the root of the tree - * @return the number of levels in the tree - */ - public static int levelCount(BinaryTreeNode root) { - return 0; - } + /** + * Returns the number of levels in the tree. + * + * An empty tree has 0 levels, a tree with only a root has 1 level. + * + * @param root the root of the tree + * @return the number of levels in the tree + */ + public static int levelCount(BinaryTreeNode root) { + return 0; + } - /** - * Returns the sum at a specified level in a binary tree. - * - * For example, if the given level was 3: - * 5 - * / \ - * 8 4 - * / \ / - * 7 9 2 - * / - * 1 - * - * Nodes at level 3: 7, 9, and 2 - * Sum of nodes at level 3: 18 - * - * The root is considered to be at level 1. - * - * Returns 0 if the tree is empty or if the level is not present in the tree. - * - * @param root the root of the binary tree - * @param level the level to sum - * @return the sum of the nodes at the given level - */ - public static int sumAtLevel(BinaryTreeNode root, int level) { - return 0; - } + /** + * Returns the sum at a specified level in a binary tree. + * + * For example, if the given level was 3: + * 5 + * / \ + * 8 4 + * / \ / + * 7 9 2 + * / + * 1 + * + * Nodes at level 3: 7, 9, and 2 + * Sum of nodes at level 3: 18 + * + * The root is considered to be at level 1. + * + * Returns 0 if the tree is empty or if the level is not present in the tree. + * + * @param root the root of the binary tree + * @param level the level to sum + * @return the sum of the nodes at the given level + */ + public static int sumAtLevel(BinaryTreeNode root, int level) { + return 0; + } - /** - * Returns true if the sum of the values in a given tree is equal to the sum - * of the values in the given list. - * - * An empty tree or list is considered to have a sum of 0. - * - * @param root The root of the binary tree - * @param head The head of the linked list - * @return true if the sums are equal, false otherwise - */ - public static boolean sumMatch(BinaryTreeNode root, ListNode head) { - return false; - } + /** + * Returns true if the sum of the values in a given tree is equal to the sum + * of the values in the given list. + * + * An empty tree or list is considered to have a sum of 0. + * + * @param root The root of the binary tree + * @param head The head of the linked list + * @return true if the sums are equal, false otherwise + */ + public static boolean sumMatch(BinaryTreeNode root, ListNode head) { + return false; + } - /** - * Returns the sum of all the vertices in a graph that are reachable from a given - * starting vertex. - * - * Returns 0 if the starting vertex is null. - * - * @param start the starting vertex - * @return the sum of all the vertices - */ - public static int graphSum(Vertex start) { - return 0; - } + /** + * Returns the sum of all the vertices in a graph that are reachable from a given + * starting vertex. + * + * Returns 0 if the starting vertex is null. + * + * @param start the starting vertex + * @return the sum of all the vertices + */ + public static int graphSum(Vertex start) { + return 0; + } - /** - * Returns the count of vertices in a graph that have an outdegree of 0. - * - * Returns 0 if the starting vertex is null. - * - * @param start the entrypoint to the graph - * @return the count of vertices with outdegree 0 - */ - public static int sinkCount(Vertex start) { - return 0; - } + /** + * Returns the count of vertices in a graph that have an outdegree of 0. + * + * Returns 0 if the starting vertex is null. + * + * @param start the entrypoint to the graph + * @return the count of vertices with outdegree 0 + */ + public static int sinkCount(Vertex start) { + return 0; + } } \ No newline at end of file From 2b23b8461fe31bc65a7afe0072a9c289a6adf59f Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 24 Sep 2025 12:57:29 -0700 Subject: [PATCH 03/10] completed adults --- src/Practice.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 193d332..6896c8e 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.List; import java.util.Map; import java.util.Set; @@ -60,7 +61,14 @@ 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 nameSet = new HashSet<>(); + for (String key : ages.keySet()) { + if(ages.get(key) >= 18) { + nameSet.add(key); + } + } + return nameSet; } /** From 023b8764d7df2967b0bd64d9e81700fa1791e1c8 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 24 Sep 2025 13:04:01 -0700 Subject: [PATCH 04/10] completed biggestNumber --- src/Practice.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 6896c8e..2914f09 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -79,7 +79,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 current = head; + int biggest = current.data; + while(current != null) { + if(current.data > biggest) { + biggest = current.data; + } + current = current.next; + } + return biggest; } /** From c0b7e373d63d21fa902aa065a22d3f61817e92ca Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 24 Sep 2025 13:10:01 -0700 Subject: [PATCH 05/10] completed frequencies --- src/Practice.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 2914f09..dad4d40 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.List; import java.util.Map; @@ -105,10 +106,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 map = new HashMap<>(); + ListNode current = head; + while(current != null) { + T key = current.data; + if(!map.containsKey(key)) { + map.put(key, 1); + } + else { + map.put(key, map.get(key) +1); + } + current = current.next; + } + return map; } - /** * Returns the number of levels in the tree. * From c1c071d7c702f3b78a585301144fd00e0bfca4aa Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 24 Sep 2025 13:32:15 -0700 Subject: [PATCH 06/10] completed levelCount --- src/Practice.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index dad4d40..dbc2a13 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -130,10 +130,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 left = levelCount(root.left); + int right = levelCount(root.right); + int levels = Math.max(left, right); + return levels + 1; } - /** * Returns the sum at a specified level in a binary tree. * From 0128102cfa8356f60c9b6a1dacde84dc8a7bac87 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 24 Sep 2025 14:09:46 -0700 Subject: [PATCH 07/10] completed sumAtLevel --- src/Practice.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index dbc2a13..5e142d2 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -2,10 +2,14 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Queue; import java.util.Set; +import javax.swing.tree.TreeNode; + public class Practice { /** * Returns the sum of the odd numbers in the array. @@ -161,10 +165,13 @@ 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 left = sumAtLevel(root.left, level -1); + int right = sumAtLevel(root.right, level -1); + return left + right; } - /** * Returns true if the sum of the values in a given tree is equal to the sum * of the values in the given list. From 4fb91a4b5b92496564b31b3e328527d3faf24d6c Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 24 Sep 2025 14:33:07 -0700 Subject: [PATCH 08/10] completed sumMatch --- src/Practice.java | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 5e142d2..a2e81fe 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -2,14 +2,10 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Queue; import java.util.Set; -import javax.swing.tree.TreeNode; - public class Practice { /** * Returns the sum of the odd numbers in the array. @@ -183,7 +179,24 @@ 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 (sumBinaryTree(root) == sumLinkedList(head)) return true; + return false; + } + + public static int sumBinaryTree(BinaryTreeNode root) { + if(root == null) return 0; + return sumBinaryTree(root.left) + sumBinaryTree(root.right) + root.data; + } + + public static int sumLinkedList(ListNode head) { + if(head == null) return 0; + ListNode current = head; + int sum = 0; + while(current != null) { + sum += current.data; + current = current.next; + } + return sum; } /** From 1c13fe3f2390b233a83c37f3cbb6014569159d5d Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 24 Sep 2025 15:02:56 -0700 Subject: [PATCH 09/10] completed graphSum --- src/Practice.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index a2e81fe..018b064 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -209,7 +209,21 @@ public static int sumLinkedList(ListNode head) { * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + Set> visited = new HashSet<>(); + return graphSum(start, visited); + } + + public static int graphSum(Vertex start, Set> visited) { + if(start == null || visited.contains(start)) return 0; + visited.add(start); + int sum = 0; + sum += start.data; + for(Vertexneighbor : start.neighbors) + { + int neighborSum = graphSum(neighbor, visited); + sum += neighborSum; + } + return sum; } /** From e59e0401e05b56cd84b18f85939fc3ba57979442 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 24 Sep 2025 16:34:47 -0700 Subject: [PATCH 10/10] completed sinkCount --- src/Practice.java | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 018b064..f6b9d95 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -218,8 +218,7 @@ public static int graphSum(Vertex start, Set> visited) visited.add(start); int sum = 0; sum += start.data; - for(Vertexneighbor : start.neighbors) - { + for(Vertexneighbor : start.neighbors) { int neighborSum = graphSum(neighbor, visited); sum += neighborSum; } @@ -235,6 +234,21 @@ public 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); + } + + public static int sinkCount(Vertex start, Set> visited) { + if(start == null || visited.contains(start)) return 0; + visited.add(start); + if (start.neighbors == null || start.neighbors.size() == 0) return 1; + int count = 0; + for (Vertex neighbor : start.neighbors) { + if (neighbor != null) { + int neighborCount = sinkCount(neighbor, visited); + count += neighborCount; + } + } + return count; } } \ No newline at end of file