style: format code (#4212)

close #4204
This commit is contained in:
acbin
2023-06-09 18:52:05 +08:00
committed by GitHub
parent ad03086f54
commit 00282efd8b
521 changed files with 5233 additions and 7309 deletions

View File

@ -1,6 +1,7 @@
package com.thealgorithms.backtracking;
import static org.junit.jupiter.api.Assertions.*;
import java.util.*;
import org.junit.jupiter.api.Test;
@ -9,48 +10,56 @@ public class AllPathsFromSourceToTargetTest {
@Test
void testForFirstCase() {
int vertices = 4;
int[][] a = {{0,1},{0,2},{0,3},{2,0},{2,1},{1,3}};
int[][] a = {{0, 1}, {0, 2}, {0, 3}, {2, 0}, {2, 1}, {1, 3}};
int source = 2;
int destination = 3;
List<List<Integer>> list2 = List.of(List.of(2, 0, 1, 3),List.of(2, 0, 3),List.of(2, 1, 3));
List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices,a,source,destination);
list2=list1;
List<List<Integer>> list2
= List.of(List.of(2, 0, 1, 3), List.of(2, 0, 3), List.of(2, 1, 3));
List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(
vertices, a, source, destination);
list2 = list1;
assertIterableEquals(list1, list2);
}
@Test
void testForSecondCase() {
int vertices = 5;
int[][] a = {{0,1},{0,2},{0,3},{2,0},{2,1},{1,3},{1,4},{3,4},{2,4}};
int[][] a = {{0, 1}, {0, 2}, {0, 3}, {2, 0}, {2, 1}, {1, 3}, {1, 4}, {3, 4}, {2, 4}};
int source = 0;
int destination = 4;
List<List<Integer>> list2 = List.of(List.of(0, 1, 3, 4),List.of(0, 1, 4),List.of(0, 2, 1, 3, 4),List.of(0, 2, 1, 4),List.of(0, 2, 4),List.of(0, 3, 4));
List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices,a,source,destination);
list2=list1;
List<List<Integer>> list2 = List.of(List.of(0, 1, 3, 4), List.of(0, 1, 4),
List.of(0, 2, 1, 3, 4), List.of(0, 2, 1, 4), List.of(0, 2, 4), List.of(0, 3, 4));
List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(
vertices, a, source, destination);
list2 = list1;
assertIterableEquals(list1, list2);
}
@Test
void testForThirdCase() {
int vertices = 6;
int[][] a = {{1,0},{2,3},{0,4},{1,5},{4,3},{0,2},{0,3},{1,2},{0,5},{3,4},{2,5},{2,4}};
int[][] a = {{1, 0}, {2, 3}, {0, 4}, {1, 5}, {4, 3}, {0, 2}, {0, 3}, {1, 2}, {0, 5}, {3, 4},
{2, 5}, {2, 4}};
int source = 1;
int destination = 5;
List<List<Integer>> list2 = List.of(List.of(1, 0, 2, 5),List.of(1, 0, 5),List.of(1, 5),List.of(1, 2, 5));
List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices,a,source,destination);
list2=list1;
List<List<Integer>> list2
= List.of(List.of(1, 0, 2, 5), List.of(1, 0, 5), List.of(1, 5), List.of(1, 2, 5));
List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(
vertices, a, source, destination);
list2 = list1;
assertIterableEquals(list1, list2);
}
@Test
void testForFourthcase() {
int vertices = 3;
int[][] a = {{0,1},{0,2},{1,2}};
int[][] a = {{0, 1}, {0, 2}, {1, 2}};
int source = 0;
int destination = 2;
List<List<Integer>> list2 = List.of(List.of(0, 1, 2),List.of(0, 2));
List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices,a,source,destination);
list2=list1;
List<List<Integer>> list2 = List.of(List.of(0, 1, 2), List.of(0, 2));
List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(
vertices, a, source, destination);
list2 = list1;
assertIterableEquals(list1, list2);
}
}

View File

@ -10,29 +10,20 @@ public class CombinationTest {
@Test
void testNoElement() {
List<TreeSet<Integer>> result = Combination.combination(
new Integer[] { 1, 2 },
0
);
List<TreeSet<Integer>> result = Combination.combination(new Integer[] {1, 2}, 0);
assertTrue(result == null);
}
@Test
void testLengthOne() {
List<TreeSet<Integer>> result = Combination.combination(
new Integer[] { 1, 2 },
1
);
List<TreeSet<Integer>> result = Combination.combination(new Integer[] {1, 2}, 1);
assertTrue(result.get(0).iterator().next() == 1);
assertTrue(result.get(1).iterator().next() == 2);
}
@Test
void testLengthTwo() {
List<TreeSet<Integer>> result = Combination.combination(
new Integer[] { 1, 2 },
2
);
List<TreeSet<Integer>> result = Combination.combination(new Integer[] {1, 2}, 2);
Integer[] arr = result.get(0).toArray(new Integer[2]);
assertTrue(arr[0] == 1);
assertTrue(arr[1] == 2);

View File

@ -17,8 +17,8 @@ class FloodFillTest {
@Test
void testForSingleElementImage() {
int[][] image = { { 1 } };
int[][] expected = { { 3 } };
int[][] image = {{1}};
int[][] expected = {{3}};
FloodFill.floodFill(image, 0, 0, 3, 1);
assertArrayEquals(expected, image);
@ -27,23 +27,23 @@ class FloodFillTest {
@Test
void testForImageOne() {
int[][] image = {
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 3, 3, 3, 3, 0, 0 },
{ 0, 3, 1, 1, 5, 0, 0 },
{ 0, 3, 1, 1, 5, 5, 3 },
{ 0, 3, 5, 5, 1, 1, 3 },
{ 0, 0, 0, 5, 1, 1, 3 },
{ 0, 0, 0, 3, 3, 3, 3 },
{0, 0, 0, 0, 0, 0, 0},
{0, 3, 3, 3, 3, 0, 0},
{0, 3, 1, 1, 5, 0, 0},
{0, 3, 1, 1, 5, 5, 3},
{0, 3, 5, 5, 1, 1, 3},
{0, 0, 0, 5, 1, 1, 3},
{0, 0, 0, 3, 3, 3, 3},
};
int[][] expected = {
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 3, 3, 3, 3, 0, 0 },
{ 0, 3, 2, 2, 5, 0, 0 },
{ 0, 3, 2, 2, 5, 5, 3 },
{ 0, 3, 5, 5, 2, 2, 3 },
{ 0, 0, 0, 5, 2, 2, 3 },
{ 0, 0, 0, 3, 3, 3, 3 },
{0, 0, 0, 0, 0, 0, 0},
{0, 3, 3, 3, 3, 0, 0},
{0, 3, 2, 2, 5, 0, 0},
{0, 3, 2, 2, 5, 5, 3},
{0, 3, 5, 5, 2, 2, 3},
{0, 0, 0, 5, 2, 2, 3},
{0, 0, 0, 3, 3, 3, 3},
};
FloodFill.floodFill(image, 2, 2, 2, 1);
@ -53,23 +53,23 @@ class FloodFillTest {
@Test
void testForImageTwo() {
int[][] image = {
{ 0, 0, 1, 1, 0, 0, 0 },
{ 1, 1, 3, 3, 3, 0, 0 },
{ 1, 3, 1, 1, 5, 0, 0 },
{ 0, 3, 1, 1, 5, 5, 3 },
{ 0, 3, 5, 5, 1, 1, 3 },
{ 0, 0, 0, 5, 1, 1, 3 },
{ 0, 0, 0, 1, 3, 1, 3 },
{0, 0, 1, 1, 0, 0, 0},
{1, 1, 3, 3, 3, 0, 0},
{1, 3, 1, 1, 5, 0, 0},
{0, 3, 1, 1, 5, 5, 3},
{0, 3, 5, 5, 1, 1, 3},
{0, 0, 0, 5, 1, 1, 3},
{0, 0, 0, 1, 3, 1, 3},
};
int[][] expected = {
{ 0, 0, 2, 2, 0, 0, 0 },
{ 2, 2, 3, 3, 3, 0, 0 },
{ 2, 3, 2, 2, 5, 0, 0 },
{ 0, 3, 2, 2, 5, 5, 3 },
{ 0, 3, 5, 5, 2, 2, 3 },
{ 0, 0, 0, 5, 2, 2, 3 },
{ 0, 0, 0, 2, 3, 2, 3 },
{0, 0, 2, 2, 0, 0, 0},
{2, 2, 3, 3, 3, 0, 0},
{2, 3, 2, 2, 5, 0, 0},
{0, 3, 2, 2, 5, 5, 3},
{0, 3, 5, 5, 2, 2, 3},
{0, 0, 0, 5, 2, 2, 3},
{0, 0, 0, 2, 3, 2, 3},
};
FloodFill.floodFill(image, 2, 2, 2, 1);
@ -79,15 +79,15 @@ class FloodFillTest {
@Test
void testForImageThree() {
int[][] image = {
{ 1, 1, 2, 3, 1, 1, 1 },
{ 1, 0, 0, 1, 0, 0, 1 },
{ 1, 1, 1, 0, 3, 1, 2 },
{1, 1, 2, 3, 1, 1, 1},
{1, 0, 0, 1, 0, 0, 1},
{1, 1, 1, 0, 3, 1, 2},
};
int[][] expected = {
{ 4, 4, 2, 3, 4, 4, 4 },
{ 4, 0, 0, 4, 0, 0, 4 },
{ 4, 4, 4, 0, 3, 4, 2 },
{4, 4, 2, 3, 4, 4, 4},
{4, 0, 0, 4, 0, 0, 4},
{4, 4, 4, 0, 3, 4, 2},
};
FloodFill.floodFill(image, 0, 1, 4, 1);

View File

@ -35,7 +35,7 @@ public class MazeRecursionTest {
map[3][1] = 1;
map[3][2] = 1;
//clone another map for setWay2 method
// clone another map for setWay2 method
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
map2[i][j] = map[i][j];
@ -46,25 +46,25 @@ public class MazeRecursionTest {
MazeRecursion.setWay2(map2, 1, 1);
int[][] expectedMap = new int[][] {
{ 1, 1, 1, 1, 1, 1, 1 },
{ 1, 2, 0, 0, 0, 0, 1 },
{ 1, 2, 2, 2, 0, 0, 1 },
{ 1, 1, 1, 2, 0, 0, 1 },
{ 1, 0, 0, 2, 0, 0, 1 },
{ 1, 0, 0, 2, 0, 0, 1 },
{ 1, 0, 0, 2, 2, 2, 1 },
{ 1, 1, 1, 1, 1, 1, 1 },
{1, 1, 1, 1, 1, 1, 1},
{1, 2, 0, 0, 0, 0, 1},
{1, 2, 2, 2, 0, 0, 1},
{1, 1, 1, 2, 0, 0, 1},
{1, 0, 0, 2, 0, 0, 1},
{1, 0, 0, 2, 0, 0, 1},
{1, 0, 0, 2, 2, 2, 1},
{1, 1, 1, 1, 1, 1, 1},
};
int[][] expectedMap2 = new int[][] {
{ 1, 1, 1, 1, 1, 1, 1 },
{ 1, 2, 2, 2, 2, 2, 1 },
{ 1, 0, 0, 0, 0, 2, 1 },
{ 1, 1, 1, 0, 0, 2, 1 },
{ 1, 0, 0, 0, 0, 2, 1 },
{ 1, 0, 0, 0, 0, 2, 1 },
{ 1, 0, 0, 0, 0, 2, 1 },
{ 1, 1, 1, 1, 1, 1, 1 },
{1, 1, 1, 1, 1, 1, 1},
{1, 2, 2, 2, 2, 2, 1},
{1, 0, 0, 0, 0, 2, 1},
{1, 1, 1, 0, 0, 2, 1},
{1, 0, 0, 0, 0, 2, 1},
{1, 0, 0, 0, 0, 2, 1},
{1, 0, 0, 0, 0, 2, 1},
{1, 1, 1, 1, 1, 1, 1},
};
assertArrayEquals(map, expectedMap);

View File

@ -16,16 +16,14 @@ public class PermutationTest {
@Test
void testSingleElement() {
List<Integer[]> result = Permutation.permutation(new Integer[] { 1 });
List<Integer[]> result = Permutation.permutation(new Integer[] {1});
assertEquals(result.get(0)[0], 1);
}
@Test
void testMultipleElements() {
List<Integer[]> result = Permutation.permutation(
new Integer[] { 1, 2 }
);
assertTrue(Arrays.equals(result.get(0), new Integer[] { 1, 2 }));
assertTrue(Arrays.equals(result.get(1), new Integer[] { 2, 1 }));
List<Integer[]> result = Permutation.permutation(new Integer[] {1, 2});
assertTrue(Arrays.equals(result.get(0), new Integer[] {1, 2}));
assertTrue(Arrays.equals(result.get(1), new Integer[] {2, 1}));
}
}

View File

@ -1,5 +1,6 @@
package com.thealgorithms.backtracking;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class PowerSumTest {
@ -17,12 +18,11 @@ public class PowerSumTest {
int result = powerSum.powSum(100, 2);
assertEquals(3, result);
}
@Test
void testNumberHundredAndPowerThree() {
PowerSum powerSum = new PowerSum();
int result = powerSum.powSum(100, 3);
assertEquals(1, result);
}
}

View File

@ -9,7 +9,7 @@ public class WordSearchTest {
@Test
void test1() {
WordSearch ws = new WordSearch();
char[][] board = {{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}};
char[][] board = {{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}};
String word = "ABCCED";
assertTrue(ws.exist(board, word));
}
@ -17,7 +17,7 @@ public class WordSearchTest {
@Test
void test2() {
WordSearch ws = new WordSearch();
char[][] board = {{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}};
char[][] board = {{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}};
String word = "SEE";
assertTrue(ws.exist(board, word));
}
@ -25,7 +25,7 @@ public class WordSearchTest {
@Test
void test3() {
WordSearch ws = new WordSearch();
char[][] board = {{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}};
char[][] board = {{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}};
String word = "ABCB";
Assertions.assertFalse(ws.exist(board, word));
}