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

Code before:
/*
 * Tests For Aho-Corasick String Matching Algorithm
 *
 *            Author: Prabhat-Kumar-42
 *    GitHub: https://github.com/Prabhat-Kumar-42
 */

package com.thealgorithms.strings;

import static org.junit.jupiter.api.Assertions.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
 * This class contains test cases for the Aho-Corasick String Matching Algorithm.
 * The Aho-Corasick algorithm is used to efficiently find all occurrences of multiple
 * patterns in a given text.
 */
class AhoCorasickTest {
    private String[] patterns; // The array of patterns to search for
    private String text; // The input text to search within

    /**
     * This method sets up the test environment before each test case.
     * It initializes the patterns and text to be used for testing.
     */
    @BeforeEach
    void setUp() {
        patterns = new String[] {"ACC", "ATC", "CAT", "GCG", "C", "T"};
        text = "GCATCG";
    }

Review comment (suggestion):
```suggestion
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
```

Code after:
/*
 * Tests For Aho-Corasick String Matching Algorithm
 *
 *            Author: Prabhat-Kumar-42
 *    GitHub: https://github.com/Prabhat-Kumar-42
 */

package com.thealgorithms.strings;

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
 * This class contains test cases for the Aho-Corasick String Matching Algorithm.
 * The Aho-Corasick algorithm is used to efficiently find all occurrences of multiple
 * patterns in a given text.
 */
class AhoCorasickTest {
    private String[] patterns; // The array of patterns to search for
    private String text; // The input text to search within

    /**
     * This method sets up the test environment before each test case.
     * It initializes the patterns and text to be used for testing.
     */
    @BeforeEach
    void setUp() {
        patterns = new String[] {"ACC", "ATC", "CAT", "GCG", "C", "T"};
        text = "GCATCG";
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/strings/AhoCorasick.java (Java)

Code before:
/*
 * Aho-Corasick String Matching Algorithm Implementation
 *
 * This code implements the Aho-Corasick algorithm, which is used for efficient
 * string matching in a given text. It can find multiple patterns simultaneously
 * and records their positions in the text.
 *
 * Author: Prabhat-Kumar-42
 * GitHub: https://github.com/Prabhat-Kumar-42
 */

package com.thealgorithms.strings;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public final class AhoCorasick {

    // Trie Node Class
    private static class Node {
        // Represents a character in the trie
        private HashMap<Character, Node> child = new HashMap<>(); // Child nodes of the current node
        private Node suffixLink; // Suffix link to another node in the trie
        private Node outputLink; // Output link to another node in the trie
        private int patternInd; // Index of the pattern that ends at this node

        Node() {
            this.suffixLink = null;
            this.outputLink = null;
            this.patternInd = -1;
        }

        public HashMap<Character, Node> getChild() {
            return child;
        }

        public Node getSuffixLink() {
            return suffixLink;
        }

        public void setSuffixLink(final Node suffixLink) {
            this.suffixLink = suffixLink;
        }

        public Node getOutputLink() {

Review comment (suggestion):
```suggestion
    private AhoCorasick() {
    }

```

Code after:
/*
 * Aho-Corasick String Matching Algorithm Implementation
 *
 * This code implements the Aho-Corasick algorithm, which is used for efficient
 * string matching in a given text. It can find multiple patterns simultaneously
 * and records their positions in the text.
 *
 * Author: Prabhat-Kumar-42
 * GitHub: https://github.com/Prabhat-Kumar-42
 */

package com.thealgorithms.strings;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public final class AhoCorasick {
    private AhoCorasick() {
    }

    // Trie Node Class
    private static class Node {
        // Represents a character in the trie
        private HashMap<Character, Node> child = new HashMap<>(); // Child nodes of the current node
        private Node suffixLink; // Suffix link to another node in the trie
        private Node outputLink; // Output link to another node in the trie
        private int patternInd; // Index of the pattern that ends at this node

        Node() {
            this.suffixLink = null;
            this.outputLink = null;
            this.patternInd = -1;
        }

        public HashMap<Character, Node> getChild() {
            return child;
        }

        public Node getSuffixLink() {
            return suffixLink;
        }

        public void setSuffixLink(final Node suffixLink) {
            this.suffixLink = suffixLink;
        }
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java (Java)

Code before:

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // Added user inputs
        System.out.print("Enter the number of elements in the array: ");
        int n = sc.nextInt();

        int a[] = new int[n];

        System.out.println("Enter the elements in sorted order:");

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

        System.out.print("Enter the target element to search for: ");
        int t = sc.nextInt();

        int res = binsear(a, 0, n - 1, t);

        if (res == -1)
            System.out.println("Element not found in the array.");
        else
            System.out.println("Element found at index " + res);
    }
}

// Explanation:- https://www.tutorialspoint.com/java-program-for-binary-search-recursive


Review comment (suggestion):
It is better if you write some junit test instead of the main function.
https://github.com/TheAlgorithms/Java/tree/master/src/test/java/com/thealgorithms/searches

Code after:
        if (right >= left) {
            int mid = left + (right - left) / 2;

            // Compare the element at the middle with the target
            int comparison = arr[mid].compareTo(target);

            // If the element is equal to the target, return its index
            if (comparison == 0) {
                return mid;
            }

            // If the element is greater than the target, search in the left subarray
            if (comparison > 0) {
                return binsear(arr, left, mid - 1, target);
            }

            // Otherwise, search in the right subarray
            return binsear(arr, mid + 1, right, target);
        }

        // Element is not present in the array
        return -1;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // User inputs
        System.out.print("Enter the number of elements in the array: ");
        int n = sc.nextInt();

        Integer[] a = new Integer[n]; // You can change the array type as needed

        System.out.println("Enter the elements in sorted order:");

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

        System.out.print("Enter the target element to search for: ");
        int t = sc.nextInt();

        RecursiveBinarySearch<Integer> searcher = new RecursiveBinarySearch<>();
        int res = searcher.find(a, t);

        if (res == -1)
            System.out.println("Element not found in the array.");
        else
            System.out.println("Element found at index " + res);
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/MatrixRank.java (Java)

Code before:
package com.thealgorithms.maths;

/**
 *
 *
 * <h1>Rank of Matrix</h1>
 *
 * <p>
 * The rank of a matrix is the largest number of linearly independent rows/columns of the matrix.
 * The rank is not only defined for square matrices.
 * </p>
 *
 * <p>
 * The rank of a matrix can also be defined as the largest order of any non-zero minor in the matrix.
 * </p>
 *
 * <p> Read more about rank of matrix <a href="https://en.wikipedia.org/wiki/Rank_(linear_algebra)">here</a></p>
 *
 * @author Anup Omkar
 */
public class MatrixRank {

    // Small value to handle precision issues with floating point numbers
    private final static double EPSILON = 1e-10;

    /**
     * Returns rank of matrix
     *
     * @param matrix matrix whose rank is to be found
     * @return rank of matrix
     */
    public static int computeRank(double[][] matrix) {
        int numRows = matrix.length;
        int numColumns = matrix[0].length;
        int rank = 0;

        boolean[] rowMarked = new boolean[numRows];
        for (int colIndex = 0; colIndex < numColumns; ++colIndex) {
            int pivotRow;
            for (pivotRow = 0; pivotRow < numRows; ++pivotRow) {
                if (!rowMarked[pivotRow] && Math.abs(matrix[pivotRow][colIndex]) > EPSILON) {
                    break;
                }

Review comment (suggestion):
I would suggest to introduce a method of the type: `bool isZero(final double x)`.

In general I am not sure how to handle this properly. If you will have some suggestions please let me know.

Code after:
package com.thealgorithms.maths;

/**
 * This class provides a method to compute the rank of a matrix.
 * In linear algebra, the rank of a matrix is the maximum number of linearly independent rows or columns in the matrix.
 * For example, consider the following 3x3 matrix:
 * 1 2 3
 * 2 4 6
 * 3 6 9
 * Despite having 3 rows and 3 columns, this matrix only has a rank of 1 because all rows (and columns) are multiples of each other.
 * It's a fundamental concept that gives key insights into the structure of the matrix.
 * It's important to note that the rank is not only defined for square matrices but for any m x n matrix.
 *
 * @author Anup Omkar
 */
public final class MatrixRank {

    private MatrixRank() {
    }

    private static final double EPSILON = 1e-10;

    /**
     * @brief Computes the rank of the input matrix
     *
     * @param matrix The input matrix
     * @return The rank of the input matrix
     */
    public static int computeRank(double[][] matrix) {
        validateInputMatrix(matrix);

        int numRows = matrix.length;
        int numColumns = matrix[0].length;
        int rank = 0;

        boolean[] rowMarked = new boolean[numRows];

        double[][] matrixCopy = deepCopy(matrix);

        for (int colIndex = 0; colIndex < numColumns; ++colIndex) {
            int pivotRow = findPivotRow(matrixCopy, rowMarked, colIndex);
            if (pivotRow != numRows) {
                ++rank;
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/MatrixRank.java (Java)

Code before:
package com.thealgorithms.maths;

/**
 * This class provides a method to compute the rank of a matrix.
 * In linear algebra, the rank of a matrix is the maximum number of linearly independent rows or columns in the matrix.
 * For example, consider the following 3x3 matrix:
 * 1 2 3
 * 2 4 6
 * 3 6 9
 * Despite having 3 rows and 3 columns, this matrix only has a rank of 1 because all rows (and columns) are multiples of each other.
 * It's a fundamental concept that gives key insights into the structure of the matrix.
 * It's important to note that the rank is not only defined for square matrices but for any m x n matrix.
 *
 * @author Anup Omkar
 */
public final class MatrixRank {

    private final static double EPSILON = 1e-10;

    /**
     * @brief Computes the rank of the input matrix
     *
     * @param matrix The input matrix
     * @return The rank of the input matrix
     */
    public static int computeRank(double[][] matrix) {
        validateInputMatrix(matrix);

        int numRows = matrix.length;
        int numColumns = matrix[0].length;
        int rank = 0;

        boolean[] rowMarked = new boolean[numRows];

        double[][] matrixCopy = new double[numRows][numColumns];
        for (int rowIndex = 0; rowIndex < numRows; ++rowIndex) {
            System.arraycopy(matrix[rowIndex], 0, matrixCopy[rowIndex], 0, numColumns);
        }

        for (int colIndex = 0; colIndex < numColumns; ++colIndex) {
            int pivotRow = findPivotRow(matrixCopy, rowMarked, colIndex);

Review comment (suggestion):
```suggestion
public final class MatrixRank {
    private MatrixRank() {
    }
```

Code after:
package com.thealgorithms.maths;

/**
 * This class provides a method to compute the rank of a matrix.
 * In linear algebra, the rank of a matrix is the maximum number of linearly independent rows or columns in the matrix.
 * For example, consider the following 3x3 matrix:
 * 1 2 3
 * 2 4 6
 * 3 6 9
 * Despite having 3 rows and 3 columns, this matrix only has a rank of 1 because all rows (and columns) are multiples of each other.
 * It's a fundamental concept that gives key insights into the structure of the matrix.
 * It's important to note that the rank is not only defined for square matrices but for any m x n matrix.
 *
 * @author Anup Omkar
 */
public final class MatrixRank {

    private MatrixRank() {
    }

    private static final double EPSILON = 1e-10;

    /**
     * @brief Computes the rank of the input matrix
     *
     * @param matrix The input matrix
     * @return The rank of the input matrix
     */
    public static int computeRank(double[][] matrix) {
        validateInputMatrix(matrix);

        int numRows = matrix.length;
        int numColumns = matrix[0].length;
        int rank = 0;

        boolean[] rowMarked = new boolean[numRows];

        double[][] matrixCopy = deepCopy(matrix);

        for (int colIndex = 0; colIndex < numColumns; ++colIndex) {
            int pivotRow = findPivotRow(matrixCopy, rowMarked, colIndex);
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/HeronsFormula.java (Java)

Code before:
package com.thealgorithms.maths;

/**
 * Wikipedia for HeronsFormula => https://en.wikipedia.org/wiki/Heron%27s_formula
 * Find the area of a triangle using only side lengths
 */

public class HeronsFormula {
    /*
     * A function to get the Area of a Triangle using Heron's Formula
     * @param s1,s2,s3 => the three sides of the Triangle
     * @return area using the formula (√(s(s – s1)(s – s2)(s – s3)))
     * here s is called semi-perimeter and it is the half of the perimeter (i.e; s = (s1+s2+s3)/2)
     * @author satyabarghav
     */
    public static double Herons(double a, double b, double c) {
        if (!(a + b > c && b + c > a && c + a > b)) {
            throw new IllegalArgumentException("Triangle can't be formed with the given side lengths");
        } else {
            double s = (a + b + c) / 2.0;
            double area = 0;
            area = Math.sqrt((s) * (s - a) * (s - b) * (s - c));
            return area;
        }
    }
}


Review comment (refactor):
We can make it a proper _utility class_:

```suggestion
public final class HeronsFormula {
    private HeronsFormula() {
    }
```

Code after:
package com.thealgorithms.maths;

/**
 * Wikipedia for HeronsFormula => https://en.wikipedia.org/wiki/Heron%27s_formula
 * Find the area of a triangle using only side lengths
 */

public final class HeronsFormula {

    /*
     * A function to get the Area of a Triangle using Heron's Formula
     * @param s1,s2,s3 => the three sides of the Triangle
     * @return area using the formula (√(s(s – s1)(s – s2)(s – s3)))
     * here s is called semi-perimeter and it is the half of the perimeter (i.e; s = (s1+s2+s3)/2)
     * @author satyabarghav
     */
    private HeronsFormula() {
    }

    private static boolean areAllSidesPositive(final double a, final double b, final double c) {
        return a > 0 && b > 0 && c > 0;
    }

    private static boolean canFormTriangle(final double a, final double b, final double c) {
        return a + b > c && b + c > a && c + a > b;
    }

    public static double herons(final double a, final double b, final double c) {
        if (!areAllSidesPositive(a, b, c) || !canFormTriangle(a, b, c)) {
            throw new IllegalArgumentException("Triangle can't be formed with the given side lengths");
        }
        final double s = (a + b + c) / 2.0;
        return Math.sqrt((s) * (s - a) * (s - b) * (s - c));
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/HeronsFormula.java (Java)

Code before:
package com.thealgorithms.maths;

/**
 * Wikipedia for HeronsFormula => https://en.wikipedia.org/wiki/Heron%27s_formula
 * Find the area of a triangle using only side lengths
 */

public class HeronsFormula {
    /*
     * A function to get the Area of a Triangle using Heron's Formula
     * @param s1,s2,s3 => the three sides of the Triangle
     * @return area using the formula (√(s(s – s1)(s – s2)(s – s3)))
     * here s is called semi-perimeter and it is the half of the perimeter (i.e; s = (s1+s2+s3)/2)
     * @author satyabarghav
     */
    public static double Herons(double a, double b, double c) {
        if (!(a + b > c && b + c > a && c + a > b)) {
            throw new IllegalArgumentException("Triangle can't be formed with the given side lengths");
        } else {
            double s = (a + b + c) / 2.0;
            double area = 0;
            area = Math.sqrt((s) * (s - a) * (s - b) * (s - c));
            return area;
        }
    }
}


Review comment (suggestion):
```suggestion
    public static double Herons(final double a, final double b, final double c) {
```

Code after:
package com.thealgorithms.maths;

/**
 * Wikipedia for HeronsFormula => https://en.wikipedia.org/wiki/Heron%27s_formula
 * Find the area of a triangle using only side lengths
 */

public final class HeronsFormula {

    /*
     * A function to get the Area of a Triangle using Heron's Formula
     * @param s1,s2,s3 => the three sides of the Triangle
     * @return area using the formula (√(s(s – s1)(s – s2)(s – s3)))
     * here s is called semi-perimeter and it is the half of the perimeter (i.e; s = (s1+s2+s3)/2)
     * @author satyabarghav
     */
    private HeronsFormula() {
    }

    private static boolean areAllSidesPositive(final double a, final double b, final double c) {
        return a > 0 && b > 0 && c > 0;
    }

    private static boolean canFormTriangle(final double a, final double b, final double c) {
        return a + b > c && b + c > a && c + a > b;
    }

    public static double herons(final double a, final double b, final double c) {
        if (!areAllSidesPositive(a, b, c) || !canFormTriangle(a, b, c)) {
            throw new IllegalArgumentException("Triangle can't be formed with the given side lengths");
        }
        final double s = (a + b + c) / 2.0;
        return Math.sqrt((s) * (s - a) * (s - b) * (s - c));
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/HeronsFormula.java (Java)

Code before:
package com.thealgorithms.maths;

/**
 * Wikipedia for HeronsFormula => https://en.wikipedia.org/wiki/Heron%27s_formula
 * Find the area of a triangle using only side lengths
 */

public final class HeronsFormula {

    /*
     * A function to get the Area of a Triangle using Heron's Formula
     * @param s1,s2,s3 => the three sides of the Triangle
     * @return area using the formula (√(s(s – s1)(s – s2)(s – s3)))
     * here s is called semi-perimeter and it is the half of the perimeter (i.e; s = (s1+s2+s3)/2)
     * @author satyabarghav
     */
    private HeronsFormula() {
    }
    public static double Herons(final double a, final double b, final double c) {
        if (!(a + b > c && b + c > a && c + a > b)) {
            throw new IllegalArgumentException("Triangle can't be formed with the given side lengths");
        } else {
            final double s = (a + b + c) / 2.0;
            return Math.sqrt((s) * (s - a) * (s - b) * (s - c));
        }
    }
}


Review comment (bug):
What do you think about changing the inequalities to `>=`? I think there is nothing wrong in a triangle with side lengths 1, 2, 3 (i.e. it has area 0). Moreover, I would suggest to add a check if the provided side lengths are `>= 0`.

```suggestion
        if (!(a + b >= c && b + c >= a && c + a >= b)) {
```

Code after:
package com.thealgorithms.maths;

/**
 * Wikipedia for HeronsFormula => https://en.wikipedia.org/wiki/Heron%27s_formula
 * Find the area of a triangle using only side lengths
 */

public final class HeronsFormula {

    /*
     * A function to get the Area of a Triangle using Heron's Formula
     * @param s1,s2,s3 => the three sides of the Triangle
     * @return area using the formula (√(s(s – s1)(s – s2)(s – s3)))
     * here s is called semi-perimeter and it is the half of the perimeter (i.e; s = (s1+s2+s3)/2)
     * @author satyabarghav
     */
    private HeronsFormula() {
    }

    private static boolean areAllSidesPositive(final double a, final double b, final double c) {
        return a > 0 && b > 0 && c > 0;
    }

    private static boolean canFormTriangle(final double a, final double b, final double c) {
        return a + b > c && b + c > a && c + a > b;
    }

    public static double herons(final double a, final double b, final double c) {
        if (!areAllSidesPositive(a, b, c) || !canFormTriangle(a, b, c)) {
            throw new IllegalArgumentException("Triangle can't be formed with the given side lengths");
        }
        final double s = (a + b + c) / 2.0;
        return Math.sqrt((s) * (s - a) * (s - b) * (s - c));
    }
}