General performance improvement (#6078)

This commit is contained in:
Strange Developer
2024-11-01 18:52:42 +01:00
committed by GitHub
parent 7b962a4a1d
commit df0c997e4b
29 changed files with 92 additions and 75 deletions

View File

@@ -1,5 +1,6 @@
package com.thealgorithms.backtracking;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
@@ -11,7 +12,7 @@ public class NQueensTest {
@Test
public void testNQueens1() {
List<List<String>> expected = Arrays.asList(Arrays.asList("Q"));
List<List<String>> expected = singletonList(singletonList("Q"));
assertEquals(expected, NQueens.getNQueensArrangements(1));
}

View File

@@ -1,5 +1,6 @@
package com.thealgorithms.bitmanipulation;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
@@ -14,8 +15,8 @@ class GenerateSubsetsTest {
int[] set = {1, 2};
List<List<Integer>> expected = new ArrayList<>();
expected.add(new ArrayList<>());
expected.add(Arrays.asList(1));
expected.add(Arrays.asList(2));
expected.add(singletonList(1));
expected.add(singletonList(2));
expected.add(Arrays.asList(1, 2));
List<List<Integer>> result = GenerateSubsets.generateSubsets(set);
@@ -27,7 +28,7 @@ class GenerateSubsetsTest {
int[] set = {3};
List<List<Integer>> expected = new ArrayList<>();
expected.add(new ArrayList<>());
expected.add(Arrays.asList(3));
expected.add(singletonList(3));
List<List<Integer>> result = GenerateSubsets.generateSubsets(set);
assertEquals(expected, result);
@@ -38,10 +39,10 @@ class GenerateSubsetsTest {
int[] set = {4, 5, 6};
List<List<Integer>> expected = new ArrayList<>();
expected.add(new ArrayList<>());
expected.add(Arrays.asList(4));
expected.add(Arrays.asList(5));
expected.add(singletonList(4));
expected.add(singletonList(5));
expected.add(Arrays.asList(4, 5));
expected.add(Arrays.asList(6));
expected.add(singletonList(6));
expected.add(Arrays.asList(4, 6));
expected.add(Arrays.asList(5, 6));
expected.add(Arrays.asList(4, 5, 6));

View File

@@ -1,5 +1,6 @@
package com.thealgorithms.datastructures.graphs;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -41,6 +42,6 @@ public class AStarTest {
public void testAStarSameNode() {
AStar.PathAndDistance result = AStar.aStar(0, 0, graph, heuristic);
assertEquals(0, result.getDistance(), "Expected distance from 0 to 0 is 0");
assertEquals(Arrays.asList(0), result.getPath(), "Expected path should only contain the start node");
assertEquals(singletonList(0), result.getPath(), "Expected path should only contain the start node");
}
}

View File

@@ -1,5 +1,7 @@
package com.thealgorithms.dynamicprogramming;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
@@ -10,7 +12,7 @@ public class AllConstructTest {
@Test
public void testAllConstructBasic() {
List<List<String>> expected = Arrays.asList(Arrays.asList("he", "l", "l", "o"));
List<List<String>> expected = singletonList(Arrays.asList("he", "l", "l", "o"));
List<List<String>> result = AllConstruct.allConstruct("hello", Arrays.asList("he", "l", "o"));
assertEquals(expected, result);
}
@@ -24,14 +26,14 @@ public class AllConstructTest {
@Test
public void testAllConstructNoWays() {
List<List<String>> expected = Arrays.asList();
List<List<String>> expected = emptyList();
List<List<String>> result = AllConstruct.allConstruct("abcdef", Arrays.asList("gh", "ijk"));
assertEquals(expected, result);
}
@Test
public void testAllConstructEmptyTarget() {
List<List<String>> expected = Arrays.asList(Arrays.asList());
List<List<String>> expected = singletonList(emptyList());
List<List<String>> result = AllConstruct.allConstruct("", Arrays.asList("a", "b", "c"));
assertEquals(expected, result);
}

View File

@@ -1,5 +1,6 @@
package com.thealgorithms.dynamicprogramming;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
@@ -23,7 +24,7 @@ public final class AssignmentUsingBitmaskTest {
public void testNoPossibleAssignments() {
int totalTasks = 3;
List<List<Integer>> taskPerformed = Arrays.asList(Arrays.asList(2), Arrays.asList(3));
List<List<Integer>> taskPerformed = Arrays.asList(singletonList(2), singletonList(3));
AssignmentUsingBitmask assignment = new AssignmentUsingBitmask(taskPerformed, totalTasks);
int ways = assignment.countNoOfWays();
@@ -34,7 +35,7 @@ public final class AssignmentUsingBitmaskTest {
public void testSinglePersonMultipleTasks() {
int totalTasks = 3;
List<List<Integer>> taskPerformed = Arrays.asList(Arrays.asList(1, 2, 3));
List<List<Integer>> taskPerformed = singletonList(Arrays.asList(1, 2, 3));
AssignmentUsingBitmask assignment = new AssignmentUsingBitmask(taskPerformed, totalTasks);
int ways = assignment.countNoOfWays();
@@ -45,7 +46,7 @@ public final class AssignmentUsingBitmaskTest {
public void testMultiplePeopleSingleTask() {
int totalTasks = 1;
List<List<Integer>> taskPerformed = Arrays.asList(Arrays.asList(1), Arrays.asList(1));
List<List<Integer>> taskPerformed = Arrays.asList(singletonList(1), singletonList(1));
AssignmentUsingBitmask assignment = new AssignmentUsingBitmask(taskPerformed, totalTasks);
int ways = assignment.countNoOfWays();

View File

@@ -1,3 +1,5 @@
package com.thealgorithms.graph;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;

View File

@@ -1,9 +1,11 @@
package com.thealgorithms.greedyalgorithms;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
public class ActivitySelectionTest {
@@ -24,7 +26,7 @@ public class ActivitySelectionTest {
int[] end = {2};
ArrayList<Integer> result = ActivitySelection.activitySelection(start, end);
ArrayList<Integer> expected = new ArrayList<>(Arrays.asList(0));
List<Integer> expected = singletonList(0);
assertEquals(expected, result);
}

View File

@@ -1,9 +1,11 @@
package com.thealgorithms.greedyalgorithms;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
public class CoinChangeTest {
@@ -16,7 +18,7 @@ public class CoinChangeTest {
@Test
public void testCoinChangeProblemWithLargeAmount() {
ArrayList<Integer> expected = new ArrayList<>(Arrays.asList(2000));
List<Integer> expected = singletonList(2000);
ArrayList<Integer> coins = CoinChange.coinChangeProblem(2000);
assertEquals(expected, coins);
}

View File

@@ -1,5 +1,6 @@
package com.thealgorithms.maths;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
@@ -27,8 +28,8 @@ public class ChineseRemainderTheoremTest {
@Test
public void testCRTWithSingleCongruence() {
List<Integer> remainders = Arrays.asList(4);
List<Integer> moduli = Arrays.asList(7);
List<Integer> remainders = singletonList(4);
List<Integer> moduli = singletonList(7);
int expected = 4;
int result = ChineseRemainderTheorem.solveCRT(remainders, moduli);
assertEquals(expected, result);

View File

@@ -59,8 +59,7 @@ class MeansTest {
@Test
void geometricMeanMultipleNumbers() {
LinkedList<Double> numbers = new LinkedList<>();
numbers.addAll(Lists.newArrayList(1d, 2d, 3d, 4d, 5d, 6d, 1.25));
LinkedList<Double> numbers = new LinkedList<>(Lists.newArrayList(1d, 2d, 3d, 4d, 5d, 6d, 1.25));
assertEquals(2.6426195539300585, Means.geometric(numbers));
}

View File

@@ -7,7 +7,7 @@ import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
public class MedianOfMatrixtest {
public class MedianOfMatrixTest {
@Test
public void testMedianWithOddNumberOfElements() {

View File

@@ -1,5 +1,7 @@
package com.thealgorithms.others;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
@@ -10,37 +12,37 @@ public class FloydTriangleTest {
@Test
public void testGenerateFloydTriangleWithValidInput() {
List<List<Integer>> expectedOutput = Arrays.asList(Arrays.asList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6));
List<List<Integer>> expectedOutput = Arrays.asList(singletonList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6));
assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(3));
}
@Test
public void testGenerateFloydTriangleWithOneRow() {
List<List<Integer>> expectedOutput = Arrays.asList(Arrays.asList(1));
List<List<Integer>> expectedOutput = singletonList(singletonList(1));
assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(1));
}
@Test
public void testGenerateFloydTriangleWithZeroRows() {
List<List<Integer>> expectedOutput = Arrays.asList();
List<List<Integer>> expectedOutput = emptyList();
assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(0));
}
@Test
public void testGenerateFloydTriangleWithNegativeRows() {
List<List<Integer>> expectedOutput = Arrays.asList();
List<List<Integer>> expectedOutput = emptyList();
assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(-3));
}
@Test
public void testGenerateFloydTriangleWithMultipleRows() {
List<List<Integer>> expectedOutput = Arrays.asList(Arrays.asList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9, 10), Arrays.asList(11, 12, 13, 14, 15));
List<List<Integer>> expectedOutput = Arrays.asList(singletonList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9, 10), Arrays.asList(11, 12, 13, 14, 15));
assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(5));
}
@Test
public void testGenerateFloydTriangleWithMoreMultipleRows() {
List<List<Integer>> expectedOutput = Arrays.asList(Arrays.asList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9, 10), Arrays.asList(11, 12, 13, 14, 15), Arrays.asList(16, 17, 18, 19, 20, 21), Arrays.asList(22, 23, 24, 25, 26, 27, 28));
List<List<Integer>> expectedOutput = Arrays.asList(singletonList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9, 10), Arrays.asList(11, 12, 13, 14, 15), Arrays.asList(16, 17, 18, 19, 20, 21), Arrays.asList(22, 23, 24, 25, 26, 27, 28));
assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(7));
}
}

View File

@@ -1,5 +1,6 @@
package com.thealgorithms.scheduling.diskscheduling;
import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
@@ -31,8 +32,8 @@ public class CircularLookSchedulingTest {
@Test
public void testCircularLookSchedulingEmptyRequests() {
CircularLookScheduling scheduling = new CircularLookScheduling(50, true, 200);
List<Integer> requests = Arrays.asList();
List<Integer> expected = Arrays.asList();
List<Integer> requests = emptyList();
List<Integer> expected = emptyList();
List<Integer> result = scheduling.execute(requests);
assertEquals(expected, result);

View File

@@ -1,5 +1,6 @@
package com.thealgorithms.scheduling.diskscheduling;
import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
@@ -39,8 +40,8 @@ public class CircularScanSchedulingTest {
@Test
public void testCircularScanSchedulingEmptyRequests() {
CircularScanScheduling circularScan = new CircularScanScheduling(50, true, 200);
List<Integer> requests = Arrays.asList();
List<Integer> expectedOrder = Arrays.asList();
List<Integer> requests = emptyList();
List<Integer> expectedOrder = emptyList();
List<Integer> result = circularScan.execute(requests);
assertEquals(expectedOrder, result);

View File

@@ -1,5 +1,6 @@
package com.thealgorithms.scheduling.diskscheduling;
import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
@@ -31,8 +32,8 @@ public class LookSchedulingTest {
@Test
public void testLookSchedulingEmptyRequests() {
LookScheduling lookScheduling = new LookScheduling(50, true, 200);
List<Integer> requests = Arrays.asList();
List<Integer> expected = Arrays.asList();
List<Integer> requests = emptyList();
List<Integer> expected = emptyList();
List<Integer> result = lookScheduling.execute(requests);
assertEquals(expected, result);

View File

@@ -1,5 +1,6 @@
package com.thealgorithms.scheduling.diskscheduling;
import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
@@ -31,8 +32,8 @@ public class ScanSchedulingTest {
@Test
public void testScanSchedulingEmptyRequests() {
ScanScheduling scanScheduling = new ScanScheduling(50, true, 200);
List<Integer> requests = Arrays.asList();
List<Integer> expected = Arrays.asList();
List<Integer> requests = emptyList();
List<Integer> expected = emptyList();
List<Integer> result = scanScheduling.execute(requests);
assertEquals(expected, result);

View File

@@ -1,5 +1,6 @@
package com.thealgorithms.strings;
import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
@@ -52,7 +53,7 @@ public class WordLadderTest {
@Test
public void testWordLadder3() {
List<String> wordList3 = Arrays.asList();
List<String> wordList3 = emptyList();
assertEquals(WordLadder.ladderLength("hit", "cog", wordList3), 0);
}