Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
s50600822 committed Oct 31, 2023
1 parent 8d08f33 commit b74d0cf
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ public static <T> Node<T> bfs(
while (!frontier.isEmpty()) {
Node<T> currentNode = frontier.poll();
T currentState = currentNode.state;
System.out.println("currentState: "+currentState);
//System.out.println("currentState: "+currentState);
if (goalTest.test(currentState)) {
return currentNode;
}
// check where we can go next and haven't explored
for (T child : successors.apply(currentState)) {
System.out.println("child " +child+", visited:" + explored);
//System.out.println("child " +child+", visited:" + explored);
if (explored.contains(child)) {
continue; // skip children we already explored
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public static void countApplesAndOranges(
int b,
List<Integer> apples,
List<Integer> oranges) {
System.out.println(countF(s, t, a, apples));
System.out.println(countF(s, t, b, oranges));
//System.out.println(countF(s, t, a, apples));
//System.out.println(countF(s, t, b, oranges));
}

public static long countF(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package hoa.can.code.gg;

public interface Jumping {
boolean canJump(int[] nums);
int minJumps(int[] arr);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package hoa.can.code.gg;

public class JumpingDP {
public static boolean canJump(int[] nums) {
public class JumpingDP implements Jumping {
public boolean canJump(int[] nums) {
return minJumps(nums) != Integer.MAX_VALUE;
}

public static int minJumps(int[] arr) {
public int minJumps(int[] arr) {
int n = arr.length;
int minCostToIdx[] = new int[n];
// result
int[] minCostToIdx = new int[n];
int i, j;

// if first element is 0,
if (n == 0 || arr[0] == 0)
return Integer.MAX_VALUE;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package hoa.can.code.gg;

public class JumpingGreedily {
public class JumpingGreedily implements Jumping {
final static int NO_WAY = 10000;

public static boolean canJump(int[] nums) {
public boolean canJump(int[] nums) {
return minJumps(nums) != NO_WAY;
}

public static int minJumps(int[] nums) {
public int minJumps(int[] nums) {
int n = nums.length;
if (n <= 1) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
/**
* <a href="https://leetcode.com/problems/jump-game/">Desc</a>
*/
public class JumpingRecursive {
public class JumpingRecursive implements Jumping {
final static int NO_WAY = 10000;

public static boolean canJump(int[] nums) {
public boolean canJump(int[] nums) {
int step = minJumps(nums);
return (step != NO_WAY);
}

public static int minJumps(int[] nums) {
public int minJumps(int[] nums) {
int[] memo = new int[nums.length];
Arrays.fill(memo, -1);
int lastIdx = nums.length - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void test8Queens() {
Map<Integer, Integer> solution = csp.backtrackingSearch();

assertEquals(8, solution.size(), "solution found!");
System.out.println(solution);//preview
//System.out.println(solution);//preview
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package hoa.can.code;

import hoa.can.code.gg.JumpingDP;

public class JumpingDPTest extends JumpingTest {
public JumpingDPTest() {
super(new JumpingDP());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package hoa.can.code;

import hoa.can.code.gg.JumpingGreedily;

public class JumpingGreedyTest extends JumpingTest {
public JumpingGreedyTest() {
super(new JumpingGreedily());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package hoa.can.code;

import hoa.can.code.gg.JumpingRecursive;

public class JumpingRecTest extends JumpingTest {
public JumpingRecTest() {
super(new JumpingRecursive());
}

@Override
public void slow() {
// do nothing since it will freeze
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package hoa.can.code;

import hoa.can.code.gg.JumpingDP;
import hoa.can.code.gg.JumpingGreedily;
import hoa.can.code.gg.JumpingRecursive;
import org.junit.jupiter.api.Disabled;
import hoa.can.code.gg.Jumping;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand All @@ -14,19 +11,25 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class JumpingTest {
public abstract class JumpingTest {
public JumpingTest(Jumping algo) {
this.algo = algo;
}

Jumping algo;

@Test
@DisplayName("ok")
public void yes() {
@DisplayName("SHOULD BE ABLE to jump")
public void shouldBeAbleTo() {
//canJump(new int[]{0});// depends
canJump(new int[]{2, 3, 1, 1, 4});
canJump(new int[]{1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9});
canJump(new int[]{3, 2, 1, 1, 1, 2, 3, 4, 5, 1, 3, 0, 2, 3, 0, 3, 1, 2, 3, 9, 1, 0, 0, 4, 1, 1, 1, 0, 2, 3, 4, 0, 9, 0, 0, 1, 2, 3, 9, 0, 0, 1, 0, 0, 1, 0, 1, 2, 3, 4});
}

@Test
@DisplayName("nope")
public void no() {
@DisplayName("SHOULD NOT be able to jump")
public void shouldNOTBeAbleTo() {
canNotJump(new int[]{0, 1});
canNotJump(new int[]{3, 2, 1, 0, 4});
canNotJump(new int[]{2, 0, 0, 0, 3});
Expand All @@ -35,59 +38,42 @@ public void no() {
}

@Test
@DisplayName("yes-WithDetail")
public void yesWithDetail() {
@DisplayName("Verify steps COUNT")
public void verifyMinStepCount() {
yesWithDetail(new int[]{1, 2}, 1, "1 -> end");
yesWithDetail(new int[]{2,0,1},1, "2 -> end");
yesWithDetail(new int[]{2, 0, 1}, 1, "2 -> end");
yesWithDetail(new int[]{2, 3, 1, 1, 4}, 2, "2 -> 3 -> end");
yesWithDetail(new int[]{10, 8, 12, 17, 1, 21, 5, 17, 20, 11, 5, 27, 23, 8, 12, 18, 16, 12, 9, 8, 17, 12, 23, 26, 0, 5, 9, 24, 10}, 2, "10 -> 20 -> end");
yesWithDetail(new int[]{5, 9, 3, 2, 1, 0, 2, 3, 3, 1, 0, 0}, 3, "5->9->3->end");
yesWithDetail(new int[]{1, 1, 1, 1, 1, 1}, 5, "1 -> 1 ->... end");
}

@Test
@DisplayName("nope")
@Disabled("TOO SLOW TO CHECK ALL COMBINATIONS")
public void nope() {
assertEquals(
13,
JumpingRecursive.minJumps(new int[]{8, 2, 4, 4, 4, 9, 5, 2, 5, 8, 8, 0, 8, 6, 9, 1, 1, 6, 3, 5, 1, 2, 6, 6, 0, 4, 8, 6, 0, 3, 2, 8, 7, 6, 5, 1, 7, 0, 3, 4, 8, 3, 5, 9, 0, 4, 0, 1, 0, 5, 9, 2, 0, 7, 0, 2, 1, 0, 8, 2, 5, 1, 2, 3, 9, 7, 4, 7, 0, 0, 1, 8, 5, 6, 7, 5, 1, 9, 9, 3, 5, 0, 7, 5}),
"quick"
);
}

@Test
@DisplayName("yep")
public void yep() {
@DisplayName("Verify work on considerable length")
public void slow() {
assertEquals(
13,
JumpingGreedily.minJumps(new int[]{8,2,4,4,4,9,5,2,5,8,8,0,8,6,9,1,1,6,3,5,1,2,6,6,0,4,8,6,0,3,2,8,7,6,5,1,7,0,3,4,8,3,5,9,0,4,0,1,0,5,9,2,0,7,0,2,1,0,8,2,5,1,2,3,9,7,4,7,0,0,1,8,5,6,7,5,1,9,9,3,5,0,7,5}),
algo.minJumps(new int[]{8, 2, 4, 4, 4, 9, 5, 2, 5, 8, 8, 0, 8, 6, 9, 1, 1, 6, 3, 5, 1, 2, 6, 6, 0, 4, 8, 6, 0, 3, 2, 8, 7, 6, 5, 1, 7, 0, 3, 4, 8, 3, 5, 9, 0, 4, 0, 1, 0, 5, 9, 2, 0, 7, 0, 2, 1, 0, 8, 2, 5, 1, 2, 3, 9, 7, 4, 7, 0, 0, 1, 8, 5, 6, 7, 5, 1, 9, 9, 3, 5, 0, 7, 5}),
"quick"
);
assertEquals(
13,
JumpingDP.minJumps(new int[]{8, 2, 4, 4, 4, 9, 5, 2, 5, 8, 8, 0, 8, 6, 9, 1, 1, 6, 3, 5, 1, 2, 6, 6, 0, 4, 8, 6, 0, 3, 2, 8, 7, 6, 5, 1, 7, 0, 3, 4, 8, 3, 5, 9, 0, 4, 0, 1, 0, 5, 9, 2, 0, 7, 0, 2, 1, 0, 8, 2, 5, 1, 2, 3, 9, 7, 4, 7, 0, 0, 1, 8, 5, 6, 7, 5, 1, 9, 9, 3, 5, 0, 7, 5}),
algo.minJumps(new int[]{8, 2, 4, 4, 4, 9, 5, 2, 5, 8, 8, 0, 8, 6, 9, 1, 1, 6, 3, 5, 1, 2, 6, 6, 0, 4, 8, 6, 0, 3, 2, 8, 7, 6, 5, 1, 7, 0, 3, 4, 8, 3, 5, 9, 0, 4, 0, 1, 0, 5, 9, 2, 0, 7, 0, 2, 1, 0, 8, 2, 5, 1, 2, 3, 9, 7, 4, 7, 0, 0, 1, 8, 5, 6, 7, 5, 1, 9, 9, 3, 5, 0, 7, 5}),
"quick"
);
}

void canJump(int[] steps) {
String log = format("%s Should be REACHABLE", Arrays.toString(steps));
assertTrue(JumpingRecursive.canJump(steps), log);
assertTrue(JumpingGreedily.canJump(steps), log);
assertTrue(JumpingDP.canJump(steps), log);
assertTrue(algo.canJump(steps), log);
}

void canNotJump(int[] steps) {
assertFalse(JumpingRecursive.canJump(steps));
assertFalse(JumpingGreedily.canJump(steps));
assertFalse(JumpingDP.canJump(steps));
assertFalse(algo.canJump(steps));
}

void yesWithDetail(int[] steps, int expected, String msg) {
String log = format("\n%s \n%s", Arrays.toString(steps), msg);
assertEquals(expected, JumpingRecursive.minJumps(steps), log);
assertEquals(expected, JumpingGreedily.minJumps(steps), log);
assertEquals(expected, JumpingDP.minJumps(steps), log);
assertEquals(expected, algo.minJumps(steps), log);
}
}

0 comments on commit b74d0cf

Please sign in to comment.