ronantakizawa/codereview-bench huggingface dataset:
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java (Java)

Code before:
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class FactorialRecursionTest {
    @ParameterizedTest
    @MethodSource("inputStream")
    void testFactorialRecursion(long expected, int number) {
        assertEquals(expected, FactorialRecursion.factorial(number));
    }

    private static Stream<Arguments> inputStream() {
        return Stream.of(Arguments.of(1, 0), Arguments.of(1, 1), Arguments.of(2, 2), Arguments.of(6, 3), Arguments.of(120, 5));
    }

    @Test
    void testNegativeNumber() {
        Exception exception = assertThrows(IllegalArgumentException.class, () -> FactorialRecursion.factorial(-1));
    }
}


Review comment (suggestion):
What do you think about renaming it to:
```suggestion
    void testThrowsForNegativeInput() {
```

Code after:
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class FactorialRecursionTest {
    @ParameterizedTest
    @MethodSource("inputStream")
    void testFactorialRecursion(long expected, int number) {
        assertEquals(expected, FactorialRecursion.factorial(number));
    }

    private static Stream<Arguments> inputStream() {
        return Stream.of(Arguments.of(1, 0), Arguments.of(1, 1), Arguments.of(2, 2), Arguments.of(6, 3), Arguments.of(120, 5));
    }

    @Test
    void testThrowsForNegativeInput() {
        assertThrows(IllegalArgumentException.class, () -> FactorialRecursion.factorial(-1));
    }
}
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java (Java)

Code before:
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class FactorialRecursionTest {
    @ParameterizedTest
    @MethodSource("inputStream")
    void testFactorialRecursion(long expected, int number) {
        assertEquals(expected, FactorialRecursion.factorial(number));
    }

    private static Stream<Arguments> inputStream() {
        return Stream.of(Arguments.of(1, 0), Arguments.of(1, 1), Arguments.of(2, 2), Arguments.of(6, 3), Arguments.of(120, 5));
    }

    @Test
    void testNegativeNumber() {
        Exception exception = assertThrows(IllegalArgumentException.class, () -> FactorialRecursion.factorial(-1));
    }
}


Review comment (suggestion):
The `exception` appears to be unused. What do you think about removing it?

```suggestion
        assertThrows(IllegalArgumentException.class, () -> FactorialRecursion.factorial(-1));
```

Code after:
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class FactorialRecursionTest {
    @ParameterizedTest
    @MethodSource("inputStream")
    void testFactorialRecursion(long expected, int number) {
        assertEquals(expected, FactorialRecursion.factorial(number));
    }

    private static Stream<Arguments> inputStream() {
        return Stream.of(Arguments.of(1, 0), Arguments.of(1, 1), Arguments.of(2, 2), Arguments.of(6, 3), Arguments.of(120, 5));
    }

    @Test
    void testThrowsForNegativeInput() {
        assertThrows(IllegalArgumentException.class, () -> FactorialRecursion.factorial(-1));
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java (Java)

Code before:
package com.thealgorithms.bitmanipulation;
import java.util.Optional;

/**
 * Find Highest Set Bit
 * This class provides a function calculating the position (or index)
 * of the most significant bit being set to 1 in a given integer.
 * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
 */

public final class HighestSetBit {
    private HighestSetBit() {
    }

    public static final Optional<Integer> findHighestSetBit(int num) {
        if (num < 0) {
            throw new IllegalArgumentException("Input cannot be negative");
        }

        if (num == 0) {
            return Optional.empty();
        }

        int position = 0;
        while (num > 0) {
            num >>= 1;
            position++;
        }

        return Optional.of(position - 1);
    }
}


Review comment (suggestion):
```suggestion
    public static Optional<Integer> findHighestSetBit(int num) {
```

This `final` can be removed.

Code after:
package com.thealgorithms.bitmanipulation;
import java.util.Optional;

/**
 * Find Highest Set Bit
 * This class provides a function calculating the position (or index)
 * of the most significant bit being set to 1 in a given integer.
 * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
 */

public final class HighestSetBit {
    private HighestSetBit() {
    }

    public static Optional<Integer> findHighestSetBit(int num) {
        if (num < 0) {
            throw new IllegalArgumentException("Input cannot be negative");
        }

        if (num == 0) {
            return Optional.empty();
        }

        int position = 0;
        while (num > 0) {
            num >>= 1;
            position++;
        }

        return Optional.of(position - 1);
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/searches/UnionFind.java (Java)

Code before:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class UnionFind {

    private final int[] p;
    private final int[] r;

    public UnionFind(int n) {
        p = new int[n];
        r = new int[n];

        for (int i = 0; i < n; i++) {
            p[i] = i;
        }
    }

    public int find(int i) {
        int parent = p[i];

        if (i == parent) {
            return i;
        }

        int result = find(parent);
        p[i] = result;

        return result;
    }

    public void union(int x, int y) {
        int r0 = find(x);
        int r1 = find(y);

        if (r1 == r0) {
            return;
        }

        if (r[r0] > r[r1]) {
            p[r1] = r0;
        } else if (r[r1] > r[r0]) {
            p[r0] = r1;
        } else {
            p[r1] = r0;
            r[r0]++;
        }
    }

    public int count() {
        List<Integer> parents = new ArrayList<>();

Review comment (suggestion):
```suggestion
        final int result = find(parent);
```

Code after:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class UnionFind {

    private final int[] p;
    private final int[] r;

    public UnionFind(int n) {
        p = new int[n];
        r = new int[n];

        for (int i = 0; i < n; i++) {
            p[i] = i;
        }
    }

    public int find(int i) {
        int parent = p[i];

        if (i == parent) {
            return i;
        }

        final int result = find(parent);
        p[i] = result;

        return result;
    }

    public void union(int x, int y) {
        int r0 = find(x);
        int r1 = find(y);

        if (r1 == r0) {
            return;
        }

        if (r[r0] > r[r1]) {
            p[r1] = r0;
        } else if (r[r1] > r[r0]) {
            p[r0] = r1;
        } else {
            p[r1] = r0;
            r[r0]++;
        }
    }

    public int count() {
        List<Integer> parents = new ArrayList<>();
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/backtracking/ArrayCombination.java (Java)

Code before:
package com.thealgorithms.backtracking;

import java.util.ArrayList;
import java.util.List;

/**
 * Finds all permutations of 1...n of length k
 * @author TheClerici (<a href="https://github.com/TheClerici">git-TheClerici</a>)
 */
public final class ArrayCombination {
    private ArrayCombination() {
    }

    /**
     * Find all combinations of 1..n using backtracking.
     *
     * @param n Max value of the elements.
     * @param k Length of the combination.
     * @return A list of all combinations of length k.
     *         Returns an empty list if k is 0 or n is less than k.
     */
    public static List<List<Integer>> combination(int n, int k) {
        if (k <= 0 || n < k) {
            return new ArrayList<>(); // Return empty list for invalid input
        }

        List<List<Integer>> combinations = new ArrayList<>();
        combine(combinations, new ArrayList<>(), 1, n, k);
        return combinations;
    }

    private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
        if (current.size() == k) { // Base case: combination found
            combinations.add(new ArrayList<>(current)); // Copy to avoid modification
            return;
        }

        for (int i = start; i <= n; i++) {
            current.add(i);
            combine(combinations, current, i + 1, n, k); // Recursive call
            current.remove(current.size() - 1); // Backtrack
        }
    }
}


Review comment (suggestion):
There is nothing bad with `n >= 0` and `k == 0`. Please throw `IllegalArgumentException` if one of the inputs is negative or `n < k`.

Please add tests checking if the exception is thrown.

Code after:
package com.thealgorithms.backtracking;

import java.util.ArrayList;
import java.util.List;

/**
 * Finds all combinations of 0...n-1 of length k
 */
public final class ArrayCombination {
    private ArrayCombination() {
    }

    /**
     * Finds all combinations of length k of 0..n-1 using backtracking.
     *
     * @param n Number of the elements.
     * @param k Length of the combination.
     * @return A list of all combinations of length k.
     */
    public static List<List<Integer>> combination(int n, int k) {
        if (n < 0 || k < 0 || k > n) {
            throw new IllegalArgumentException("Wrong input.");
        }

        List<List<Integer>> combinations = new ArrayList<>();
        combine(combinations, new ArrayList<>(), 0, n, k);
        return combinations;
    }

    private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
        if (current.size() == k) { // Base case: combination found
            combinations.add(new ArrayList<>(current)); // Copy to avoid modification
            return;
        }

        for (int i = start; i < n; i++) {
            current.add(i);
            combine(combinations, current, i + 1, n, k);
            current.removeLast(); // Backtrack
        }
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/backtracking/ArrayCombination.java (Java)

Code before:
package com.thealgorithms.backtracking;

import java.util.ArrayList;
import java.util.List;

/**
 * Finds all permutations of 1...n of length k
 * @author TheClerici (<a href="https://github.com/TheClerici">git-TheClerici</a>)
 */
public final class ArrayCombination {
    private ArrayCombination() {
    }

    /**
     * Find all combinations of 1..n using backtracking.
     *
     * @param n Max value of the elements.
     * @param k Length of the combination.
     * @return A list of all combinations of length k.
     *         Returns an empty list if k is 0 or n is less than k.
     */
    public static List<List<Integer>> combination(int n, int k) {
        if (k <= 0 || n < k) {
            return new ArrayList<>(); // Return empty list for invalid input
        }

        List<List<Integer>> combinations = new ArrayList<>();
        combine(combinations, new ArrayList<>(), 1, n, k);
        return combinations;
    }

    private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
        if (current.size() == k) { // Base case: combination found
            combinations.add(new ArrayList<>(current)); // Copy to avoid modification
            return;
        }

        for (int i = start; i <= n; i++) {
            current.add(i);
            combine(combinations, current, i + 1, n, k); // Recursive call

Review comment (suggestion):
Wat do you think about
```suggestion
     * Find all combinations of 0..n-1 using backtracking.
```
Clearly this will require changes in the code and tests, but in my opinion it will be much more useful.

Code after:
package com.thealgorithms.backtracking;

import java.util.ArrayList;
import java.util.List;

/**
 * Finds all combinations of 0...n-1 of length k
 */
public final class ArrayCombination {
    private ArrayCombination() {
    }

    /**
     * Finds all combinations of length k of 0..n-1 using backtracking.
     *
     * @param n Number of the elements.
     * @param k Length of the combination.
     * @return A list of all combinations of length k.
     */
    public static List<List<Integer>> combination(int n, int k) {
        if (n < 0 || k < 0 || k > n) {
            throw new IllegalArgumentException("Wrong input.");
        }

        List<List<Integer>> combinations = new ArrayList<>();
        combine(combinations, new ArrayList<>(), 0, n, k);
        return combinations;
    }

    private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
        if (current.size() == k) { // Base case: combination found
            combinations.add(new ArrayList<>(current)); // Copy to avoid modification
            return;
        }

        for (int i = start; i < n; i++) {
            current.add(i);
            combine(combinations, current, i + 1, n, k);
            current.removeLast(); // Backtrack
        }
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/others/RotateMatrixBy90Degrees.java (Java)

Code before:
package com.thealgorithms.others;

/**
 * Given a matrix of size n x n We have to rotate this matrix by 90 Degree Here
 * is the algorithm for this problem .
 */
import java.util.Scanner;

final class RotateMatrixBy90Degrees  {
    private RotateMatrixBy90Degrees() {
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        while (t-- > 0) {
            int n = sc.nextInt();
            int[][] arr = new int[n][n];

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    arr[i][j] = sc.nextInt();
                }
            }

            Rotate.rotate(arr);
            printMatrix(arr);
        }
        sc.close();
    }

    static void printMatrix(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {

Review comment (suggestion):
This should make `clang-format` happy:

```suggestion
final class RotateMatrixBy90Degrees {
```

Code after:
package com.thealgorithms.others;

/**
 * Given a matrix of size n x n We have to rotate this matrix by 90 Degree Here
 * is the algorithm for this problem .
 */
import java.util.Scanner;

final class RotateMatrixBy90Degrees {
    private RotateMatrixBy90Degrees() {
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        while (t-- > 0) {
            int n = sc.nextInt();
            int[][] arr = new int[n][n];

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    arr[i][j] = sc.nextInt();
                }
            }

            Rotate.rotate(arr);
            printMatrix(arr);
        }
        sc.close();
    }

    static void printMatrix(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java (Java)

Code before:
package com.thealgorithms.strings;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class LetterCombinationsOfPhoneNumber {

    // Mapping of numbers to corresponding letters on a phone keypad
    private static final List<String> KEYPAD = List.of(" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz");

    private LetterCombinationsOfPhoneNumber() {
    }

    /**
     * Generates a list of all possible letter combinations that the provided
     * array of numbers could represent on a phone keypad.
     *
     * @param numbers an array of integers representing the phone numbers
     * @return a list of possible letter combinations
     */
    public static List<String> getCombinations(int[] numbers) {
        if (numbers == null) {
            return List.of("");
        }
        return generateCombinations(numbers, 0, new StringBuilder());
    }

    /**
     * Recursive method to generate combinations of letters from the phone keypad.
     *
     * @param numbers the input array of phone numbers
     * @param index   the current index in the numbers array being processed
     * @param current a StringBuilder holding the current combination of letters
     * @return a list of letter combinations formed from the given numbers

Review comment (suggestion):
Why not simply

```suggestion
    private static final String[] KEYPAD = new String[] {" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
```

Code after:
package com.thealgorithms.strings;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class LetterCombinationsOfPhoneNumber {

    private static final char EMPTY = '\0';

    // Mapping of numbers to corresponding letters on a phone keypad
    private static final String[] KEYPAD = new String[] {" ", String.valueOf(EMPTY), "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

    private LetterCombinationsOfPhoneNumber() {
    }

    /**
     * Generates a list of all possible letter combinations that the provided
     * array of numbers could represent on a phone keypad.
     *
     * @param numbers an array of integers representing the phone numbers
     * @return a list of possible letter combinations
     */
    public static List<String> getCombinations(int[] numbers) {
        if (numbers == null) {
            return List.of("");
        }
        return generateCombinations(numbers, 0, new StringBuilder());
    }

    /**
     * Recursive method to generate combinations of letters from the phone keypad.
     *
     * @param numbers the input array of phone numbers
     * @param index   the current index in the numbers array being processed
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java (Java)

Code before:
package com.thealgorithms.strings;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class LetterCombinationsOfPhoneNumber {

    // Mapping of numbers to corresponding letters on a phone keypad
    private static final String[] KEYPAD = new String[] {" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

    private LetterCombinationsOfPhoneNumber() {
    }

    /**
     * Generates a list of all possible letter combinations that the provided
     * array of numbers could represent on a phone keypad.
     *
     * @param numbers an array of integers representing the phone numbers
     * @return a list of possible letter combinations
     */
    public static List<String> getCombinations(int[] numbers) {
        if (numbers == null) {
            return List.of("");
        }
        return generateCombinations(numbers, 0, new StringBuilder());
    }

    /**
     * Recursive method to generate combinations of letters from the phone keypad.
     *
     * @param numbers the input array of phone numbers
     * @param index   the current index in the numbers array being processed
     * @param current a StringBuilder holding the current combination of letters
     * @return a list of letter combinations formed from the given numbers

Review comment (suggestion):
```suggestion
    private static final String[] KEYPAD = new String[] {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
```

I think this space was introduced by accident in 97e26d3f4850d38cd0b1c6a604fad0bf71a41042.

Code after:
package com.thealgorithms.strings;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class LetterCombinationsOfPhoneNumber {

    private static final char EMPTY = '\0';

    // Mapping of numbers to corresponding letters on a phone keypad
    private static final String[] KEYPAD = new String[] {" ", String.valueOf(EMPTY), "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

    private LetterCombinationsOfPhoneNumber() {
    }

    /**
     * Generates a list of all possible letter combinations that the provided
     * array of numbers could represent on a phone keypad.
     *
     * @param numbers an array of integers representing the phone numbers
     * @return a list of possible letter combinations
     */
    public static List<String> getCombinations(int[] numbers) {
        if (numbers == null) {
            return List.of("");
        }
        return generateCombinations(numbers, 0, new StringBuilder());
    }

    /**
     * Recursive method to generate combinations of letters from the phone keypad.
     *
     * @param numbers the input array of phone numbers
     * @param index   the current index in the numbers array being processed