Add automatic linter (#4214)

This commit is contained in:
acbin
2023-06-09 20:05:14 +08:00
committed by GitHub
parent 00282efd8b
commit 415a04ea7f
188 changed files with 661 additions and 1133 deletions

View File

@@ -66,11 +66,7 @@ class BinarySearch implements SearchAlgorithm {
int size = 100;
int maxElement = 100000;
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement))
.limit(size)
.sorted()
.boxed()
.toArray(Integer[] ::new);
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[] ::new);
// The element that should be found
int shouldBeFound = integers[r.nextInt(size - 1)];
@@ -78,11 +74,9 @@ class BinarySearch implements SearchAlgorithm {
BinarySearch search = new BinarySearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n",
shouldBeFound, integers[atIndex], atIndex, size);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.printf(
"Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}
}

View File

@@ -42,11 +42,9 @@ public class BinarySearch2dArray {
if (arr[endRow][midCol] == target) return new int[] {endRow, midCol};
if (target <= arr[startRow][midCol - 1])
return binarySearch(arr, target, startRow, 0, midCol - 1);
if (target <= arr[startRow][midCol - 1]) return binarySearch(arr, target, startRow, 0, midCol - 1);
if (target >= arr[startRow][midCol + 1] && target <= arr[startRow][colCount - 1])
return binarySearch(arr, target, startRow, midCol + 1, colCount - 1);
if (target >= arr[startRow][midCol + 1] && target <= arr[startRow][colCount - 1]) return binarySearch(arr, target, startRow, midCol + 1, colCount - 1);
if (target <= arr[endRow][midCol - 1])
return binarySearch(arr, target, endRow, 0, midCol - 1);

View File

@@ -40,32 +40,22 @@ public class DepthFirstSearch {
return Optional.of(node);
}
return node.getSubNodes()
.stream()
.map(value -> search(value, name))
.flatMap(Optional::stream)
.findAny();
return node.getSubNodes().stream().map(value -> search(value, name)).flatMap(Optional::stream).findAny();
}
public static void assertThat(final Object actual, final Object expected) {
if (!Objects.equals(actual, expected)) {
throw new AssertionError(
String.format("expected=%s but was actual=%s", expected, actual));
throw new AssertionError(String.format("expected=%s but was actual=%s", expected, actual));
}
}
public static void main(final String[] args) {
final Node rootNode = new Node("A",
List.of(
new Node("B",
List.of(new Node("D"), new Node("F", List.of(new Node("H"), new Node("I"))))),
new Node("C", List.of(new Node("G"))), new Node("E")));
final Node rootNode = new Node("A", List.of(new Node("B", List.of(new Node("D"), new Node("F", List.of(new Node("H"), new Node("I"))))), new Node("C", List.of(new Node("G"))), new Node("E")));
{
final String expected = "I";
final Node result = search(rootNode, expected)
.orElseThrow(() -> new AssertionError("Node not found!"));
final Node result = search(rootNode, expected).orElseThrow(() -> new AssertionError("Node not found!"));
assertThat(result.getName(), expected);
}
@@ -73,8 +63,7 @@ public class DepthFirstSearch {
{
final String expected = "G";
final Node result = search(rootNode, expected)
.orElseThrow(() -> new AssertionError("Node not found!"));
final Node result = search(rootNode, expected).orElseThrow(() -> new AssertionError("Node not found!"));
assertThat(result.getName(), expected);
}
@@ -82,8 +71,7 @@ public class DepthFirstSearch {
{
final String expected = "E";
final Node result = search(rootNode, expected)
.orElseThrow(() -> new AssertionError("Node not found!"));
final Node result = search(rootNode, expected).orElseThrow(() -> new AssertionError("Node not found!"));
assertThat(result.getName(), expected);
}

View File

@@ -14,11 +14,7 @@ class ExponentialSearch implements SearchAlgorithm {
int size = 100;
int maxElement = 100000;
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement))
.limit(size)
.sorted()
.boxed()
.toArray(Integer[] ::new);
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[] ::new);
// The element that should be found
int shouldBeFound = integers[r.nextInt(size - 1)];
@@ -26,12 +22,10 @@ class ExponentialSearch implements SearchAlgorithm {
ExponentialSearch search = new ExponentialSearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n",
shouldBeFound, integers[atIndex], atIndex, size);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.printf(
"Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}
@Override

View File

@@ -66,7 +66,6 @@ public class FibonacciSearch implements SearchAlgorithm {
FibonacciSearch fsearch = new FibonacciSearch();
int atIndex = fsearch.find(integers, shouldBeFound);
System.out.println("Should be found: " + shouldBeFound + ". Found " + integers[atIndex]
+ " at index " + atIndex + ". An array length " + size);
System.out.println("Should be found: " + shouldBeFound + ". Found " + integers[atIndex] + " at index " + atIndex + ". An array length " + size);
}
}

View File

@@ -30,8 +30,7 @@ class InterpolationSearch {
while (start <= end && key >= array[start] && key <= array[end]) {
// Probing the position with keeping
// uniform distribution in mind.
int pos
= start + (((end - start) / (array[end] - array[start])) * (key - array[start]));
int pos = start + (((end - start) / (array[end] - array[start])) * (key - array[start]));
// Condition of target found
if (array[pos] == key) {
@@ -54,8 +53,7 @@ class InterpolationSearch {
Random r = new Random();
int size = 100;
int maxElement = 100000;
int[] integers
= IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray();
int[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray();
// the element that should be found
int shouldBeFound = integers[r.nextInt(size - 1)];
@@ -63,11 +61,9 @@ class InterpolationSearch {
InterpolationSearch search = new InterpolationSearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n",
shouldBeFound, integers[atIndex], atIndex, size);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.printf(
"Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}
}

View File

@@ -58,10 +58,7 @@ public final class IterativeBinarySearch implements SearchAlgorithm {
Random r = new Random();
int size = 100;
int maxElement = 100000;
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement))
.limit(size)
.sorted()
.toArray(Integer[] ::new);
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray(Integer[] ::new);
// the element that should be found
Integer shouldBeFound = integers[r.nextInt(size - 1)];
@@ -69,11 +66,9 @@ public final class IterativeBinarySearch implements SearchAlgorithm {
IterativeBinarySearch search = new IterativeBinarySearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n",
shouldBeFound, integers[atIndex], atIndex, size);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.printf(
"Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}
}

View File

@@ -56,10 +56,7 @@ public class IterativeTernarySearch implements SearchAlgorithm {
Random r = new Random();
int size = 100;
int maxElement = 100000;
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement))
.limit(size)
.sorted()
.toArray(Integer[] ::new);
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray(Integer[] ::new);
// the element that should be found
Integer shouldBeFound = integers[r.nextInt(size - 1)];
@@ -67,11 +64,9 @@ public class IterativeTernarySearch implements SearchAlgorithm {
IterativeTernarySearch search = new IterativeTernarySearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n",
shouldBeFound, integers[atIndex], atIndex, size);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.printf(
"Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}
}

View File

@@ -42,8 +42,7 @@ public class LinearSearch implements SearchAlgorithm {
Random r = new Random();
int size = 200;
int maxElement = 100;
Integer[] integers
= Stream.generate(() -> r.nextInt(maxElement)).limit(size).toArray(Integer[] ::new);
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).toArray(Integer[] ::new);
// the element that should be found
Integer shouldBeFound = integers[r.nextInt(size - 1)];
@@ -51,7 +50,6 @@ public class LinearSearch implements SearchAlgorithm {
LinearSearch search = new LinearSearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n",
shouldBeFound, integers[atIndex], atIndex, size);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
}
}

View File

@@ -33,11 +33,7 @@ class LowerBound implements SearchAlgorithm {
int size = 100;
int maxElement = 100000;
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement))
.limit(size)
.sorted()
.boxed()
.toArray(Integer[] ::new);
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[] ::new);
// The element for which the lower bound is to be found
int val = integers[r.nextInt(size - 1)] + 1;
@@ -45,12 +41,10 @@ class LowerBound implements SearchAlgorithm {
LowerBound search = new LowerBound();
int atIndex = search.find(integers, val);
System.out.printf("Val: %d. Lower Bound Found %d at index %d. An array length %d%n", val,
integers[atIndex], atIndex, size);
System.out.printf("Val: %d. Lower Bound Found %d at index %d. An array length %d%n", val, integers[atIndex], atIndex, size);
boolean toCheck = integers[atIndex] >= val || integers[size - 1] < val;
System.out.printf(
"Lower Bound found at an index: %d. Is greater or max element: %b%n", atIndex, toCheck);
System.out.printf("Lower Bound found at an index: %d. Is greater or max element: %b%n", atIndex, toCheck);
}
/**

View File

@@ -78,8 +78,7 @@ public class MonteCarloTreeSearch {
winnerNode = getWinnerNode(rootNode);
printScores(rootNode);
System.out.format(
"\nThe optimal node is: %02d\n", rootNode.childNodes.indexOf(winnerNode) + 1);
System.out.format("\nThe optimal node is: %02d\n", rootNode.childNodes.indexOf(winnerNode) + 1);
return winnerNode;
}
@@ -119,10 +118,7 @@ public class MonteCarloTreeSearch {
break;
}
uctTemp = ((double) childNode.score / childNode.visitCount)
+ 1.41
* Math.sqrt(
Math.log(promisingNode.visitCount) / (double) childNode.visitCount);
uctTemp = ((double) childNode.score / childNode.visitCount) + 1.41 * Math.sqrt(Math.log(promisingNode.visitCount) / (double) childNode.visitCount);
if (uctTemp > uctIndex) {
uctIndex = uctTemp;
@@ -161,8 +157,7 @@ public class MonteCarloTreeSearch {
tempNode.visitCount++;
// Add wining scores to bouth player and opponent depending on the turn.
if ((tempNode.isPlayersTurn && isPlayerWinner)
|| (!tempNode.isPlayersTurn && !isPlayerWinner)) {
if ((tempNode.isPlayersTurn && isPlayerWinner) || (!tempNode.isPlayersTurn && !isPlayerWinner)) {
tempNode.score += WIN_SCORE;
}
@@ -178,8 +173,7 @@ public class MonteCarloTreeSearch {
System.out.println("N.\tScore\t\tVisits");
for (int i = 0; i < rootNode.childNodes.size(); i++) {
System.out.printf("%02d\t%d\t\t%d%n", i + 1, rootNode.childNodes.get(i).score,
rootNode.childNodes.get(i).visitCount);
System.out.printf("%02d\t%d\t\t%d%n", i + 1, rootNode.childNodes.get(i).score, rootNode.childNodes.get(i).visitCount);
}
}
}

View File

@@ -49,8 +49,7 @@ public final class QuickSelect {
return selectIndex(list, 0, list.size() - 1, n);
}
private static <T extends Comparable<T>> int selectIndex(
List<T> list, int left, int right, int n) {
private static <T extends Comparable<T>> int selectIndex(List<T> list, int left, int right, int n) {
while (true) {
if (left == right) return left;
int pivotIndex = pivot(list, left, right);
@@ -65,8 +64,7 @@ public final class QuickSelect {
}
}
private static <T extends Comparable<T>> int partition(
List<T> list, int left, int right, int pivotIndex, int n) {
private static <T extends Comparable<T>> int partition(List<T> list, int left, int right, int pivotIndex, int n) {
T pivotValue = list.get(pivotIndex);
Collections.swap(list, pivotIndex, right);
int storeIndex = left;

View File

@@ -66,10 +66,7 @@ public class TernarySearch implements SearchAlgorithm {
Random r = new Random();
int size = 100;
int maxElement = 100000;
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement))
.limit(size)
.sorted()
.toArray(Integer[] ::new);
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray(Integer[] ::new);
// the element that should be found
Integer shouldBeFound = integers[r.nextInt(size - 1)];
@@ -77,11 +74,9 @@ public class TernarySearch implements SearchAlgorithm {
TernarySearch search = new TernarySearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n",
shouldBeFound, integers[atIndex], atIndex, size);
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.printf(
"Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}
}

View File

@@ -33,11 +33,7 @@ class UpperBound implements SearchAlgorithm {
int size = 100;
int maxElement = 100000;
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement))
.limit(size)
.sorted()
.boxed()
.toArray(Integer[] ::new);
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[] ::new);
// The element for which the upper bound is to be found
int val = integers[r.nextInt(size - 1)] + 1;
@@ -45,12 +41,10 @@ class UpperBound implements SearchAlgorithm {
UpperBound search = new UpperBound();
int atIndex = search.find(integers, val);
System.out.printf("Val: %d. Upper Bound Found %d at index %d. An array length %d%n", val,
integers[atIndex], atIndex, size);
System.out.printf("Val: %d. Upper Bound Found %d at index %d. An array length %d%n", val, integers[atIndex], atIndex, size);
boolean toCheck = integers[atIndex] > val || integers[size - 1] < val;
System.out.printf(
"Upper Bound found at an index: %d. Is greater or max element: %b%n", atIndex, toCheck);
System.out.printf("Upper Bound found at an index: %d. Is greater or max element: %b%n", atIndex, toCheck);
}
/**

View File

@@ -4,8 +4,7 @@ public class sortOrderAgnosticBinarySearch {
public static int find(int[] arr, int key) {
int start = 0;
int end = arr.length - 1;
boolean arrDescending = arr[start]
> arr[end]; // checking for Array is in ascending order or descending order.
boolean arrDescending = arr[start] > arr[end]; // checking for Array is in ascending order or descending order.
while (start <= end) {
int mid = end - start / 2;
if (arr[mid] == key) {