From fd469bcfd800ca3782e58b3cc0ee25086b0698e2 Mon Sep 17 00:00:00 2001 From: Jacob Woodbury Date: Tue, 23 Sep 2025 11:29:44 -0700 Subject: [PATCH 01/10] working on shrotestWord --- src/Practice.java | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..3b237fb 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,7 +11,16 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + if(nums.equals(null) || nums.length < 1){ + return 0; + } + int sum = 0; + for(int i =0; i<= nums.length; i++){ + if(nums[i]%2 != 0){ + sum += nums[i]; + } + } + return sum; } /** @@ -26,6 +35,22 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { + if(words.isEmpty()){throw new IllegalArgumentException();} + if(words == null){throw new NullPointerException();} + 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){ + return shortest; + } + } + + } + return null; } From 80acf48b39a0537c87094dc786e68504409f1be8 Mon Sep 17 00:00:00 2001 From: Jacob Woodbury Date: Tue, 23 Sep 2025 11:37:26 -0700 Subject: [PATCH 02/10] shortest word working --- src/Practice.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 3b237fb..f9bafec 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,11 +11,11 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - if(nums.equals(null) || nums.length < 1){ + if(nums == null || nums.length < 1){ return 0; } int sum = 0; - for(int i =0; i<= nums.length; i++){ + for(int i =0; i<= nums.length-1; i++){ if(nums[i]%2 != 0){ sum += nums[i]; } @@ -44,14 +44,12 @@ public static String shortestWord(Set words) { shortest = word; } if(word.length() == shortest.length()){ - if(word.compareTo(shortest)>0){ - return shortest; + if(word.compareTo(shortest) <0){ + shortest = word; } } - } - - return null; + return shortest; } /** From e97572485b3f9900903411c8610a5f8050c44c6b Mon Sep 17 00:00:00 2001 From: Jacob Woodbury Date: Tue, 23 Sep 2025 11:50:12 -0700 Subject: [PATCH 03/10] biggest num --- src/Practice.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index f9bafec..9c0dd78 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; @@ -62,7 +63,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 adult = new HashSet<>(); + for(String name: ages.keySet()){ + if(ages.get(name)>=18){ + adult.add(name); + } + } + return adult; } /** @@ -73,7 +81,15 @@ 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 temp = head; + int biggest = Integer.MIN_VALUE; + + while(temp != null){ + if(temp.data > biggest) biggest = temp.data; + temp = temp.next; + } + return biggest; } /** From 4b9cfe2621ef8b040fbd10796412e0391986d9a9 Mon Sep 17 00:00:00 2001 From: Jacob Woodbury Date: Tue, 23 Sep 2025 12:43:25 -0700 Subject: [PATCH 04/10] freq of vals --- src/Practice.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 9c0dd78..2bc6509 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; @@ -106,7 +107,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 freqMap= new HashMap<>(); + if(head == null) return freqMap; + ListNode temp = head; + int count = 0; + while(temp != null){ + if(freqMap.containsKey(temp.data)){ + count = freqMap.get(temp.data) +1; + freqMap.put(temp.data, count); + }else{ + freqMap.put(temp.data, 1); + } + + temp = temp.next; + } + return freqMap; } From 5099e9da9ebb4c317b3c142aaa36ac851fa97a24 Mon Sep 17 00:00:00 2001 From: Jacob Woodbury Date: Tue, 23 Sep 2025 12:57:59 -0700 Subject: [PATCH 05/10] lvl Count --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 2bc6509..bf04742 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -134,7 +134,17 @@ 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 levelCount(root, 0); + + } + public static int levelCount(BinaryTreeNode temp, int lvlCount){ + if(temp == null) return lvlCount; + lvlCount++; + int leftCount = levelCount(temp.left, lvlCount); + int rightCount = levelCount(temp.right, lvlCount); + int max = Math.max(leftCount, rightCount); + return max; } From 9a0a230f1c693f4dc5a2ea0299a7d5dee582244d Mon Sep 17 00:00:00 2001 From: Jacob Woodbury Date: Tue, 23 Sep 2025 18:29:32 -0700 Subject: [PATCH 06/10] finishing sumAtlvl --- src/Practice.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index bf04742..c71ccf3 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -172,7 +172,18 @@ public static int levelCount(BinaryTreeNode temp, int lvlCount){ * @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; + return sumAtLevelRecur(root, level, 1, 0); + } + public static int sumAtLevelRecur(BinaryTreeNode currentNode, int level, int currentLvl, int sum){ + if(currentLvl < level){ + if(currentNode.left != null) sum = sumAtLevelRecur(currentNode.left, level, currentLvl+1, sum); + if(currentNode.right != null) sum = sumAtLevelRecur(currentNode.right, level, currentLvl+1, sum); + } + if(currentLvl == level){ + sum += currentNode.data; + } + return sum; } From a21db2841be1a4e3c551f139a53d0eeeda5034f8 Mon Sep 17 00:00:00 2001 From: Jacob Woodbury Date: Tue, 23 Sep 2025 18:51:28 -0700 Subject: [PATCH 07/10] finished sum root vs head --- src/Practice.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index c71ccf3..c19a14d 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -198,7 +198,28 @@ public static int sumAtLevelRecur(BinaryTreeNode currentNode, int level * @return true if the sums are equal, false otherwise */ public static boolean sumMatch(BinaryTreeNode root, ListNode head) { + int rootSum = sumRoot(root, 0); + int headSum = sumHead(head, 0); + System.out.println(rootSum + " " + headSum); + if(rootSum == headSum) return true; return false; + + + } + public static int sumRoot(BinaryTreeNode temp, int sum){ + if(temp == null) return sum; + sum += temp.data; + sum = sumRoot(temp.left, sum); + sum = sumRoot(temp.right, sum); + + return sum; + } + public static int sumHead(ListNode head, int sum){ + if(head == null) return sum; + sum += head.data; + sum = sumHead(head.next, sum); + + return sum; } /** From eec2948faa5cad6a27346134129c4dbfa4c97643 Mon Sep 17 00:00:00 2001 From: Jacob Woodbury Date: Tue, 23 Sep 2025 19:04:31 -0700 Subject: [PATCH 08/10] finished vertex problem --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index c19a14d..773e1ab 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -232,9 +232,22 @@ public static int sumHead(ListNode head, int sum){ * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + HashSet> visited = new HashSet<>(); + return graphSum(start, visited, 0); + } + public static int graphSum(Vertex current, HashSet> visited, int sum){ + if(current == null || visited.contains(current)) return sum; + if (!visited.contains(current)) { + visited.add(current); + sum += current.data; + for(Vertex neighbor: current.neighbors){ + sum = graphSum(neighbor, visited, sum); + } + } + return sum; + } /** * Returns the count of vertices in a graph that have an outdegree of 0. * From 42a480958083be88a3cfc553430969c2e68abd95 Mon Sep 17 00:00:00 2001 From: Jacob Woodbury Date: Tue, 23 Sep 2025 19:24:10 -0700 Subject: [PATCH 09/10] finished --- src/Practice.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 773e1ab..b69484a 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -257,6 +257,23 @@ public static int graphSum(Vertex current, HashSet> vis * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + + return sinkCount(start, 0); + } + + public static int sinkCount(Vertex current, int sinks){ + if(current == null){ + + }else{ + if(current.neighbors.size() == 0){ + sinks++; + } + + for(Vertex neighbor: current.neighbors){ + sinks = sinkCount(neighbor, sinks); + } + } + + return sinks; } } \ No newline at end of file From 11912e7c540a66694eb1a1249e9dcd8adf0ba7eb Mon Sep 17 00:00:00 2001 From: Jacob Woodbury Date: Tue, 23 Sep 2025 19:31:26 -0700 Subject: [PATCH 10/10] fixing up last problem for cycles --- src/Practice.java | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index b69484a..8b3d67e 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -257,23 +257,24 @@ public static int graphSum(Vertex current, HashSet> vis * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - - return sinkCount(start, 0); + return sinkCount(start, 0, new HashSet<>()); } - public static int sinkCount(Vertex current, int sinks){ - if(current == null){ - - }else{ - if(current.neighbors.size() == 0){ - sinks++; - } - - for(Vertex neighbor: current.neighbors){ - sinks = sinkCount(neighbor, sinks); + public static int sinkCount(Vertex current, int sinks, HashSet> visited){ + + if(current != null){ + if(!visited.contains(current)){ + visited.add(current); + if(current.neighbors.size() == 0){ + sinks++; + } + for(Vertex neighbor: current.neighbors){ + sinks = sinkCount(neighbor, sinks, visited); + } } + + } - return sinks; } } \ No newline at end of file