From c2dd12cb64d679a529a8f6596e67b6d5b697661a Mon Sep 17 00:00:00 2001 From: JesseChum Date: Wed, 24 Sep 2025 09:24:03 -0700 Subject: [PATCH 1/5] finished first two problems --- src/Practice.java | 69 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..eaf393e 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; + int sum = 0; + if (nums == null) + return 0; + for (int num : nums) { + if (num % 2 != 0) + sum += num; + } + return sum; } /** @@ -23,10 +30,31 @@ public static int oddSum(int[] nums) { * @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 + * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + if (words == null) { + throw new NullPointerException("Word is null"); + } + if (words.isEmpty()) { + throw new IllegalArgumentException("Word is empty"); + } + + String shortest = null; + + for (String word : words) { + if (shortest == null) + shortest = word; + if (word.length() < shortest.length()) { + shortest = word; + } + if (word.length() == shortest.length()) { + if (word.compareTo(shortest) < 0) { + shortest = word; + } + } + } + return shortest; } /** @@ -54,15 +82,16 @@ public static int biggestNumber(ListNode head) { } /** - * Returns a frequency map counting how frequently items appear in a linked list. + * 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} + * 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 the type of data held by the list * @param head the head of the list * @return a frequency map of values in the list */ @@ -70,7 +99,6 @@ public static Map frequencies(ListNode head) { return null; } - /** * Returns the number of levels in the tree. * @@ -83,27 +111,26 @@ 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 + * 5 + * / \ + * 8 4 + * / \ / + * 7 9 2 + * / + * 1 * * Nodes at level 3: 7, 9, and 2 - * Sum of nodes at level 3: 18 + * 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 root the root of the binary tree * @param level the level to sum * @return the sum of the nodes at the given level */ @@ -111,10 +138,9 @@ 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. + * of the values in the given list. * * An empty tree or list is considered to have a sum of 0. * @@ -127,7 +153,8 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h } /** - * Returns the sum of all the vertices in a graph that are reachable from a given + * 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. From aa2317031f4c126c31f30dfddc9b2b8d950488fb Mon Sep 17 00:00:00 2001 From: JesseChum Date: Sat, 27 Sep 2025 16:56:10 -0700 Subject: [PATCH 2/5] Finished practice problems return biggest number and return requency map. Debugging issues on red squigs --- src/Practice.java | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index eaf393e..533e34a 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -67,9 +67,23 @@ 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 result = new HashSet<>(); + + for (String name : ages.keySet()){ + int age = ages.get(name); + if (age >= 18){ + result.add(name); + } + } + + return result; + } + /** * Returns the biggest number in a linked list. * @@ -78,7 +92,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("list is empty"); + } + + int biggest = head.getValue(); + + ListNode current = head.getNext(); + + while (current != null){ + int value = current.getValue(); + if (value > biggest) { + biggest = value; + } + current = current.getNext(); + } + return biggest; } /** @@ -96,7 +125,7 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + } /** From 86b9172fbc62a6c27ab54a8ce8a45be8c249978c Mon Sep 17 00:00:00 2001 From: JesseChum Date: Sat, 27 Sep 2025 17:29:03 -0700 Subject: [PATCH 3/5] Debugging file showed up in vscode. and finished. return frequency map and return number of levels. figuring out redd squiglys stills --- .vscode/settings.json | 3 +++ src/Practice.java | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c995aa5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.debug.settings.onBuildFailureProceed": true +} \ No newline at end of file diff --git a/src/Practice.java b/src/Practice.java index 533e34a..b8ac406 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -125,7 +125,24 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { + Map freqMap = new HashMap<>(); + if(head == null) { + return freqMap; + } + + ListNode current = head; + while(current != null){ + T value = current.getValue(); + + if (freqMap.containsKey(value)) { + freqMap.put(value, freqMap.get(value) + 1); + } else { + freqMap.put(value, 1); + } + current = current.getNext(); + } + return freqMap; } /** @@ -137,7 +154,14 @@ 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 leftLevels = levelCount(root.getLeft()); + int rightLevels = levelCount(root.getRight()); + + return 1 + Math.max(leftLevels, rightLevels); } /** From 9331305317d22fd6d0d0a9f01bcedb77444cfb02 Mon Sep 17 00:00:00 2001 From: JesseChum Date: Sat, 27 Sep 2025 21:15:27 -0700 Subject: [PATCH 4/5] Accidental change in ListNode java and redoing some practice java problems to get rid of weird squigglys --- src/ListNode.java | 2 +- src/Practice.java | 71 ++++++++++++++++++++++++++--------------------- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/src/ListNode.java b/src/ListNode.java index ff02b96..2a7d4ff 100644 --- a/src/ListNode.java +++ b/src/ListNode.java @@ -10,4 +10,4 @@ public ListNode(T data, ListNode next) { public ListNode(T data) { this(data, null); } -} +} \ No newline at end of file diff --git a/src/Practice.java b/src/Practice.java index b8ac406..0d5692f 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; @@ -92,24 +94,17 @@ public static Set adults(Map ages) { * @throws IllegalArgumentException if head is null */ public static int biggestNumber(ListNode head) { - if (head == null){ - throw new IllegalArgumentException("list is empty"); + 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; } - - int biggest = head.getValue(); - - ListNode current = head.getNext(); - - while (current != null){ - int value = current.getValue(); - if (value > biggest) { - biggest = value; - } - current = current.getNext(); - } - return biggest; + return biggestNumber; } + /** * Returns a frequency map counting how frequently items appear in a linked * list. @@ -125,24 +120,19 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - Map freqMap = new HashMap<>(); - - if(head == null) { - return freqMap; - } - + Map results = new HashMap<>(); + if (head == null) return results; ListNode current = head; - while(current != null){ - T value = current.getValue(); - - if (freqMap.containsKey(value)) { - freqMap.put(value, freqMap.get(value) + 1); + while (current != null) { + if (results.containsKey(current.data)) { + results.put(current.data, results.get(current.data) + 1); } else { - freqMap.put(value, 1); + results.put(current.data, 1); } - current = current.getNext(); + current = current.next; } - return freqMap; + + return results; } /** @@ -188,7 +178,16 @@ 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.getValue(); + } + + return sumAtLevel(root.getLeft(), level -1) + + sumAtLevel(root.getRight(), level -1); } /** @@ -202,9 +201,19 @@ 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(root = null){ + return 0; + } + + if(level 1 == 1) { + return root.getValue(); + } + return sumAtLevel(root.getLeft(), level -1) + + sumAtLevel(root.getRight(), level -1); } + /** * Returns the sum of all the vertices in a graph that are reachable from a * given From d57d2af41e7e5e146353fc71aabcb79f6bda4606 Mon Sep 17 00:00:00 2001 From: JesseChum Date: Sat, 27 Sep 2025 23:06:12 -0700 Subject: [PATCH 5/5] Finished the last 3 problems and solved the red underline issue. restarted vscode and tried a different way of testing --- src/Practice.java | 87 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 68 insertions(+), 19 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 0d5692f..292eabe 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -148,10 +148,8 @@ public static int levelCount(BinaryTreeNode root) { return 0; } - int leftLevels = levelCount(root.getLeft()); - int rightLevels = levelCount(root.getRight()); - - return 1 + Math.max(leftLevels, rightLevels); + return 1 + Math.max(levelCount(root.left), + levelCount(root.right)); } /** @@ -181,13 +179,13 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { if (root == null){ return 0; } - if (level == 1){ - return root.getValue(); + return root.data; } - return sumAtLevel(root.getLeft(), level -1) + - sumAtLevel(root.getRight(), level -1); + return sumAtLevel(root.left, level -1) + + sumAtLevel(root.right, level -1); + } /** @@ -201,18 +199,25 @@ 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(root = null){ - return 0; - } - - if(level 1 == 1) { - return root.getValue(); + int treeSum = sumTree(root); + int listSum = sumList(head); + return treeSum == listSum; } - return sumAtLevel(root.getLeft(), level -1) + - sumAtLevel(root.getRight(), level -1); + + private static int sumTree(BinaryTreeNode root) { + if (root == null) return 0; + return root.data + sumTree(root.left) + sumTree(root.right); } + private static int sumList(ListNode head){ + int sum = 0; + ListNode current = head; + while (current != null) { + sum += current.data; + current = current.next; + } + return sum; + } /** * Returns the sum of all the vertices in a graph that are reachable from a @@ -225,7 +230,28 @@ 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 Sum(start, visited); + + } + + private static int Sum(Vertex v, Set> visited){ + if (visited.contains(v)){ + return 0; + } + + visited.add(v); + + int sum = v.data; + + for (Vertex neighbor : v.neighbors){ + sum += Sum(neighbor, visited); + } + return sum; } /** @@ -237,6 +263,29 @@ public static int graphSum(Vertex start) { * @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<>(); + return sinkCount(start, visited); + } + + private static int sinkCount(Vertex v, Set> visited) { + if (visited.contains(v)){ + return 0; + } + + visited.add(v); + + int count = 0; + if(v.neighbors.isEmpty()){ + count = 1; + } + + for (Vertex neighbor : v.neighbors){ + count += sinkCount(neighbor, visited); + } + return count; } } \ No newline at end of file