From d3764940e04ea6df672bf91de24bc31e8af90cce Mon Sep 17 00:00:00 2001 From: rjfredr Date: Wed, 24 Sep 2025 09:32:39 -0700 Subject: [PATCH 1/4] completed odd sum, adults, linked list, level count --- .vscode/settings.json | 3 +++ src/Practice.java | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 5 deletions(-) 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 01da8d0..5b4bc53 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; @@ -11,7 +12,12 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + if (nums==null||nums.length==0) return 0; + int sum = 0; + for (int i: nums) { + if (i%2==1||i%2==-1) sum+= i; + } + return sum; } /** @@ -26,7 +32,13 @@ 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.size()==0) throw new IllegalArgumentException(); + String shortest = ""; + for (String word : words) { + if (word.length() words) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { - return null; + if (ages==null) throw new NullPointerException(); + Set majors = new HashSet<>(); + for (String name : ages.keySet()) { + if (ages.get(name)>=18) majors.add(name); + } + return majors; } /** @@ -50,7 +67,13 @@ 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 biggest = Integer.MIN_VALUE; + while (head!=null) { + if (head.data>biggest) biggest = head.data; + head = head.next; + } + return biggest; } /** @@ -67,6 +90,8 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { + if (head==null) throw new NullPointerException(); + Map frequency = new HashMap(); return null; } @@ -80,7 +105,10 @@ 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); + return Math.max(left,right)+1; } From 988c0e206882ef9678aaa44d36835ea7886821a8 Mon Sep 17 00:00:00 2001 From: rjfredr Date: Wed, 24 Sep 2025 23:09:16 -0700 Subject: [PATCH 2/4] completed level sum --- src/Practice.java | 60 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 5b4bc53..33f8466 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,8 @@ +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 { @@ -34,9 +37,16 @@ public static int oddSum(int[] nums) { public static String shortestWord(Set words) { if (words==null) throw new NullPointerException(); if (words.size()==0) throw new IllegalArgumentException(); + int shortestlength = Integer.MAX_VALUE; String shortest = ""; for (String word : words) { - if (word.length() head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - if (head==null) throw new NullPointerException(); - Map frequency = new HashMap(); - return null; + if (head==null) return Map.of(); + Map frequency = new HashMap<>(); + while(head!=null) { + if (frequency.containsKey(head.data)) { + frequency.put(head.data, frequency.get(head.data)+1); + } + else { + frequency.put(head.data, 1); + } + head=head.next; + } + return frequency; } @@ -136,6 +155,33 @@ public static int levelCount(BinaryTreeNode root) { * @return the sum of the nodes at the given level */ public static int sumAtLevel(BinaryTreeNode root, int level) { + if (root==null) return 0; + Queue> queue = new LinkedList<>(); + queue.offer(root); + int currlevel = 1; + int levelsum = 0; + while (!queue.isEmpty()) { + int levelsize = queue.size(); + if (currlevel == level) { + for (int i = 0; i < levelsize; i++) { + BinaryTreeNode node = queue.poll(); + levelsum += node.data; + } + return levelsum; + } + else { + for (int j = 0; j < levelsize; j++) { + BinaryTreeNode node = queue.poll(); + if (node.left!=null) { + queue.offer(node.left); + } + if (node.right!=null) { + queue.offer(node.right); + } + } + currlevel++; + } + } return 0; } @@ -151,6 +197,12 @@ 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&&head==null) return true; + int listsum = 0; + while(head!=null) { + listsum+= head.data; + head=head.next; + } return false; } From 93118137fc2eb40fb7c311c30a482b8083b008ef Mon Sep 17 00:00:00 2001 From: rjfredr Date: Wed, 24 Sep 2025 23:24:56 -0700 Subject: [PATCH 3/4] finished summatch --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 33f8466..9fbca87 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -198,12 +198,22 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { */ public static boolean sumMatch(BinaryTreeNode root, ListNode head) { if (root==null&&head==null) return true; + if (root==null||head==null) return false; + Queue> queue = new LinkedList<>(); + int treesum = 0; + queue.offer(root); + while (!queue.isEmpty()) { + BinaryTreeNode node = queue.poll(); + if (node.left!=null) queue.offer(node.left); + if (node.right!=null) queue.offer(node.right); + treesum+=node.data; + } int listsum = 0; while(head!=null) { listsum+= head.data; head=head.next; } - return false; + return treesum==listsum; } /** From 02f84188de06d33f27988ad4700afe66cda2e406 Mon Sep 17 00:00:00 2001 From: rjfredr Date: Wed, 24 Sep 2025 23:38:50 -0700 Subject: [PATCH 4/4] sink and graph sum done man that took ALOT of revieiwing --- src/Practice.java | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 9fbca87..e38d563 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -226,7 +226,17 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + HashSet> visited = new HashSet>(); + return graphSumHelper(start, visited); + } + public static int graphSumHelper(Vertex start, Set> visited) { + if (start==null||visited.contains(start)) return 0; + visited.add(start); + int sum = start.data; + for (Vertex neighbour : start.neighbors) { + sum += graphSumHelper(neighbour, visited); + } + return sum; } /** @@ -238,6 +248,17 @@ public static int graphSum(Vertex start) { * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + HashSet> visited = new HashSet<>(); + return sinkCountHelper(start, visited); + } + public static int sinkCountHelper(Vertex start, Set> visited) { + if (start==null||visited.contains(start)) return 0; + int sinkcount = 0; + visited.add(start); + if (start.neighbors.size()==0) sinkcount++; + for (Vertex neighbour : start.neighbors) { + sinkcount += sinkCountHelper(neighbour, visited); + } + return sinkcount; } } \ No newline at end of file