From d51d61af5a1f255175054df39debf8f42ab869e9 Mon Sep 17 00:00:00 2001 From: RJ Date: Sun, 8 Jun 2025 02:27:43 -0700 Subject: [PATCH 1/6] final one done and one vertex done --- src/Practice.java | 54 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 6f1383b..1bfaa5a 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,5 @@ +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; @@ -24,8 +26,21 @@ public class Practice { * @param starting the starting vertex (may be null) * @return the number of vertices with odd values reachable from the starting vertex */ - public static int oddVertices(Vertex starting) { - return 0; + public static int oddVertices(Vertex starting, List> seen) { + if (seen.contains(starting)||starting==null) return 0; + int count = 0; + seen.add(starting); + for (Vertex neighbor : starting.neighbors) { + count += oddVertices(neighbor,seen); + if (starting.data%2==1) { + return 1; + } + } + return count; + } + public static int oddVertices(Vertex starting) { + List> seen = new ArrayList<>(); + return oddVertices(starting,seen); } /** @@ -46,8 +61,20 @@ public static int oddVertices(Vertex starting) { * @param starting the starting vertex (may be null) * @return a sorted list of all reachable vertex values by */ - public static List sortedReachable(Vertex starting) { - return null; + public static List sortedReachable(Vertex current, List> seen, List reachable) { + if (current==null||seen.contains(current)) return List.of(); + seen.add(current); + reachable.add(current.data); + for (Vertex neighbor : current.neighbors) { + sortedReachable(neighbor,seen,reachable); + } + Collections.sort(reachable); + return reachable; + } + public static List sortedReachable(Vertex starting) { + List> seen = new ArrayList<>(); + List reachable = new ArrayList<>(); + return sortedReachable(starting,seen,reachable); } /** @@ -78,9 +105,14 @@ public static List sortedReachable(Map> graph, in * @param v2 the target vertex * @return true if there is a two-way connection between v1 and v2, false otherwise */ - public static boolean twoWay(Vertex v1, Vertex v2) { + public static boolean twoWay(Vertex v1, Vertex v2, List> seen) { + if (v1==v2) return true; return false; } + public static boolean twoWay(Vertex v1, Vertex v2) { + List> seen = new ArrayList<>(); + return twoWay(v1, v2, seen); + } /** * Returns whether there exists a path from the starting to ending vertex that includes only positive values. @@ -179,6 +211,16 @@ public static boolean hasExtendedConnectionAtCompany(Professional person, String * @return an unsorted list of next moves */ public static List nextMoves(char[][] board, int[] current, int[][] directions) { - return null; + int currROW = current[0]; + int currCOL = current[1]; + List possibleMoves = new ArrayList<>(); + for (int[] dir : directions) { + int newROW = currROW + dir[0]; + int newCOL = currCOL + dir[1]; + if (newROW>=0&&newROW=0&&newCOL Date: Sun, 8 Jun 2025 02:39:15 -0700 Subject: [PATCH 2/6] completed hasextendedconnectiono --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 1bfaa5a..3ec1ff3 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -139,9 +139,19 @@ public static boolean positivePathExists(Map> graph, int s * @param companyName the name of the company to check for employment * @return true if a person in the extended network works at the specified company, false otherwise */ - public static boolean hasExtendedConnectionAtCompany(Professional person, String companyName) { + public static boolean hasExtendedConnectionAtCompany(Professional person, String companyName, List seen) { + if (person==null||seen.contains(person)) return false; + seen.add(person); + for (Professional connection : person.getConnections()) { + if (connection.getCompany()==companyName) return true; + return hasExtendedConnectionAtCompany(connection, companyName, seen); + } return false; } + public static boolean hasExtendedConnectionAtCompany(Professional person, String companyName) { + List seen = new ArrayList<>(); + return hasExtendedConnectionAtCompany(person, companyName, seen); + } /** * Returns a list of possible next moves starting from a given position. From d53cab6b663eeb602cb740c85996b132e66d1390 Mon Sep 17 00:00:00 2001 From: RJ Date: Sun, 8 Jun 2025 12:49:11 -0700 Subject: [PATCH 3/6] sorted reachable map ver done --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 3ec1ff3..a166226 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -87,8 +87,20 @@ public static List sortedReachable(Vertex starting) { * @param starting the starting vertex value * @return a sorted list of all reachable vertex values */ + public static List sortedReachable(Map> graph, int starting, List sorted) { + if (!graph.containsKey(starting)) return List.of(); + if (sorted.contains(starting)) return sorted; + sorted.add(starting); + for (Integer neighbor : graph.get(starting)) { + sortedReachable(graph, neighbor, sorted); + } + return sorted; + } public static List sortedReachable(Map> graph, int starting) { - return null; + List sorted = new ArrayList<>(); + sortedReachable(graph,starting,sorted); + Collections.sort(sorted); + return sorted; } /** @@ -141,6 +153,7 @@ public static boolean positivePathExists(Map> graph, int s */ public static boolean hasExtendedConnectionAtCompany(Professional person, String companyName, List seen) { if (person==null||seen.contains(person)) return false; + if (person.getCompany()==companyName) return true; seen.add(person); for (Professional connection : person.getConnections()) { if (connection.getCompany()==companyName) return true; From 7457af68566fbc429b0f37b602e19ff24e2e0de5 Mon Sep 17 00:00:00 2001 From: RJ Date: Sun, 8 Jun 2025 14:19:59 -0700 Subject: [PATCH 4/6] completed odds --- src/Practice.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index a166226..bab3216 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -26,21 +26,19 @@ public class Practice { * @param starting the starting vertex (may be null) * @return the number of vertices with odd values reachable from the starting vertex */ - public static int oddVertices(Vertex starting, List> seen) { + public static int oddVertices(Vertex starting, List> seen, List odds) { if (seen.contains(starting)||starting==null) return 0; - int count = 0; seen.add(starting); + if (starting.data%2==1) odds.add(starting.data); for (Vertex neighbor : starting.neighbors) { - count += oddVertices(neighbor,seen); - if (starting.data%2==1) { - return 1; - } + oddVertices(neighbor,seen,odds); } - return count; + return odds.size(); } public static int oddVertices(Vertex starting) { List> seen = new ArrayList<>(); - return oddVertices(starting,seen); + List odds = new ArrayList<>(); + return oddVertices(starting,seen,odds); } /** @@ -118,7 +116,12 @@ public static List sortedReachable(Map> graph, in * @return true if there is a two-way connection between v1 and v2, false otherwise */ public static boolean twoWay(Vertex v1, Vertex v2, List> seen) { + if (v1==null||v2==null) return false; if (v1==v2) return true; + seen.add(v1); + for (Vertex neighbor : v1.neighbors) { + return twoWay(neighbor, v2, seen); + } return false; } public static boolean twoWay(Vertex v1, Vertex v2) { From e4f7b2df18545f69de12a4c5b917545a1b244abd Mon Sep 17 00:00:00 2001 From: RJ Date: Sun, 8 Jun 2025 14:25:46 -0700 Subject: [PATCH 5/6] pospath done --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index bab3216..c944fff 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -141,9 +141,19 @@ public static boolean twoWay(Vertex v1, Vertex v2) { * @param ending the ending vertex value * @return whether there exists a valid positive path from starting to ending */ - public static boolean positivePathExists(Map> graph, int starting, int ending) { + public static boolean positivePathExists(Map> graph, int starting, int ending, List seen) { + if (starting<0||ending<0||!graph.containsKey(starting)) return false; + if (starting==ending) return true; + seen.add(starting); + for (Integer neighbor : graph.get(starting)) { + if (!seen.contains(neighbor)&&neighbor>0) return positivePathExists(graph, neighbor, ending, seen); + } return false; } + public static boolean positivePathExists(Map> graph, int starting, int ending) { + List seen = new ArrayList<>(); + return positivePathExists(graph, starting, ending, seen); + } /** * Returns true if a professional has anyone in their extended network (reachable through any number of links) From a639374fc4d894a5c22f3d786afc5d939b86a929 Mon Sep 17 00:00:00 2001 From: RJ Date: Sun, 8 Jun 2025 14:27:01 -0700 Subject: [PATCH 6/6] i am the best computer scientist of all time omg --- src/Practice.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index c944fff..581e237 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -126,7 +126,7 @@ public static boolean twoWay(Vertex v1, Vertex v2, List> see } public static boolean twoWay(Vertex v1, Vertex v2) { List> seen = new ArrayList<>(); - return twoWay(v1, v2, seen); + return (twoWay(v1, v2, seen)==twoWay(v2, v1, seen)); } /**