ronantakizawa/codereview-bench huggingface dataset:
Repository: TheAlgorithms/C-Plus-Plus
File: math/factorial_memoization.cpp (C++)

Code before:
/**
 * @file
 * @brief [Factorial calculation using recursion and memoization (dynamic programming)](https://en.wikipedia.org/wiki/Factorial)
 * @details
 * This program computes the factorial of a non-negative integer using recursion
 * with memoization (top-down dynamic programming). It stores intermediate results
 * to avoid redundant calculations for improved efficiency.
 * 
 * Example:
 * Input: n = 5
 * Output: 120
 * 
 * Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120
 * 
 * The program uses a recursive function fact_recursion which caches computed 
 * results in a memo array to avoid recalculating factorials for the same numbers.
 *
 * Time Complexity: O(n)
 * Space Complexity: O(n)
 * @author [Vedant Mukhedkar](https://github.com/git5v)
 */

#include <iostream> // for std::cout
#include <cassert>  // For test cases
#include <array>    // For std::array
#include <cstdint>  // For uint64_t

/// Array to store computed factorials for memoization
std::array<uint64_t, 1000> memo{0};

/**
 * @namespace math
 * @brief Math algorithms

Review comment (performance):
```suggestion
 * 
 * Memoization is a form of caching where the result to an expensive function call
 * is stored and returned.
```

Code after:
/**
 * @file
 * @brief [Factorial](https://en.wikipedia.org/wiki/Factorial) calculation using recursion and [memoization](https://en.wikipedia.org/wiki/Memoization) 
 * @details
 * This program computes the factorial of a non-negative integer using recursion
 * with memoization (top-down dynamic programming). It stores intermediate results
 * to avoid redundant calculations for improved efficiency.
 * 
 * Memoization is a form of caching where the result to an expensive function call
 * is stored and returned.
 * Example:
 * Input: n = 5
 * Output: 120
 * 
 * Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120
 * 
 * The program uses a recursive function fact_recursion which caches computed 
 * results in a memo array to avoid recalculating factorials for the same numbers.
 *
 * Time Complexity: O(n)
 * Space Complexity: O(n)
 * @author [Vedant Mukhedkar](https://github.com/git5v)
 */

#include <iostream> // for std::cout
#include <cassert>  // For test cases
#include <array>    // For std::array
#include <cstdint>  // For uint64_t

/// Array to store computed factorials for memoization
std::array<uint64_t, 1000> memo{0};

/**
Repository: TheAlgorithms/C-Plus-Plus
File: math/vector_cross_product.cpp (C++)

Code before:
}
}  // namespace vector_cross
}  // namespace math

/**
 * @brief test function.
 * @details test the cross() and the mag() functions.
 */
static void test() {
    /// Tests the cross() function.
    std::array<double, 3> t_vec =
        math::vector_cross::cross({1, 2, 3}, {4, 5, 6});
    assert(t_vec[0] == -3 && t_vec[1] == 6 && t_vec[2] == -3);

    /// Tests the mag() function.
    double t_mag = math::vector_cross::mag({6, 8, 0});
    assert(t_mag == 10);
}

/**
 * @brief Main Function
 * @details Asks the user to enter the direction ratios for each of the two
 * mathematical vectors using std::cin
 * @returns 0 on exit
 */
int main() {
    /// Tests the functions with sample input before asking for user input.
    test();
    return 0;
}


Review comment (suggestion):
Add a test for A ⨯ A
```suggestion

    /// Tests A ⨯ A = 0
    std::array<double, 3> t_vec2 =
        math::vector_cross::cross({1, 2, 3}, {1, 2, 3});
    assert(t_vec2[0] == 0 && t_vec2[1] == 0 &&
           t_vec2[2] == 0);  // checking each element
    assert(math::vector_cross::mag(t_vec2) ==
           0);  // checking the magnitude is also zero
}
```

Code after:
}
}  // namespace vector_cross
}  // namespace math

/**
 * @brief test function.
 * @details test the cross() and the mag() functions.
 */
static void test() {
    /// Tests the cross() function.
    std::array<double, 3> t_vec =
        math::vector_cross::cross({1, 2, 3}, {4, 5, 6});
    assert(t_vec[0] == -3 && t_vec[1] == 6 && t_vec[2] == -3);

    /// Tests the mag() function.
    double t_mag = math::vector_cross::mag({6, 8, 0});
    assert(t_mag == 10);

    /// Tests A ⨯ A = 0
    std::array<double, 3> t_vec2 =
        math::vector_cross::cross({1, 2, 3}, {1, 2, 3});
    assert(t_vec2[0] == 0 && t_vec2[1] == 0 &&
           t_vec2[2] == 0);  // checking each element
    assert(math::vector_cross::mag(t_vec2) ==
           0);  // checking the magnitude is also zero
}

/**
 * @brief Main Function
 * @details Asks the user to enter the direction ratios for each of the two
 * mathematical vectors using std::cin
 * @returns 0 on exit
 */
int main() {
    /// Tests the functions with sample input before asking for user input.
    test();
    return 0;
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/datastructures/queues/PriorityQueues.java (Java)

Code before:
// package com.thealgorithms.datastructures.queues;


/**
 * This class implements a PriorityQueue.
 *
 * <p>
 * A priority queue adds elements into positions based on their priority. So the
 * most important elements are placed at the front/on the top. In this example I
 * give numbers that are bigger, a higher priority. Queues in theory have no
 * fixed size but when using an array implementation it does.
 * 
 * Additional contibutions made by: PuneetTri(https://github.com/PuneetTri)
 */
class PriorityQueue {

    /**
     * The max size of the queue
     */
    private int maxSize;
    /**
     * The array for the queue
     */
    private int[] queueArray;
    /**
     * How many items are in the queue

Review comment (suggestion):
```suggestion
package com.thealgorithms.datastructures.queues;
```

Code after:
package com.thealgorithms.datastructures.queues;




/**
 * This class implements a PriorityQueue.
 *
 * <p>
 * A priority queue adds elements into positions based on their priority. So the
 * most important elements are placed at the front/on the top. In this example I
 * give numbers that are bigger, a higher priority. Queues in theory have no
 * fixed size but when using an array implementation it does.
 * <p>
 * Additional contibutions made by: PuneetTri(https://github.com/PuneetTri)
 */
class PriorityQueue {

    /**
     * The max size of the queue
     */
    private int maxSize;
    /**
     * The array for the queue
     */
    private int[] queueArray;
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/others/Conway.java (Java)

Code before:
    *1 is read off as "one 1" or 11.
    *11 is read off as "two 1s" or 21.
    *21 is read off as "one 2, one 1" or 1211.
    *1211 is read off as "one 1, one 2, two 1s" or 111221.
    *111221 is read off as "three 1s, two 2s, one 1" or 312211.
    * https://en.wikipedia.org/wiki/Look-and-say_sequence
    * */
    private static final StringBuilder builder = new StringBuilder();

    public static void main(String[]args) {
        Scanner scanner;
        String originalString;
        System.out.println("Enter any character you want : ");
        scanner = new Scanner(System.in);originalString = scanner.nextLine();

        List<String> strings = generateList(originalString);
        System.out.println(strings);

    }

    protected static List<String> generateList(String originalString) {
        List<String> numbers = new ArrayList<>();
        for(int i=0; i<20; i++) {
            originalString =   generateNextElement(originalString);
            numbers.add(originalString);
            }
        return numbers;
    }


    public static String generateNextElement(String originalString) {

        builder.setLength(0);
        String[] stp = originalString.split("(?<=(.))(?!\\1)");
        Arrays.stream(stp).forEach(s -> builder.append(s.length()).append(s.charAt(0)));
        return builder.toString();
    }



}


Review comment (suggestion):
```suggestion
            originalString = generateNextElement(originalString);
            numbers.add(originalString);
        }
```

Code after:
     *11 is read off as "two 1s" or 21.
     *21 is read off as "one 2, one 1" or 1211.
     *1211 is read off as "one 1, one 2, two 1s" or 111221.
     *111221 is read off as "three 1s, two 2s, one 1" or 312211.
     * https://en.wikipedia.org/wiki/Look-and-say_sequence
     * */

    private static final StringBuilder builder = new StringBuilder();

    protected static List<String> generateList(String originalString, int maxIteration) {
        List<String> numbers = new ArrayList<>();
        for(int i=0; i<maxIteration; i++) {
            originalString = generateNextElement(originalString);
            numbers.add(originalString);
        }
        return numbers;
    }


    public static String generateNextElement(String originalString) {
        builder.setLength(0);
        String[] stp = originalString.split("(?<=(.))(?!\\1)");
        Arrays.stream(stp).forEach(s -> builder.append(s.length()).append(s.charAt(0)));
        return builder.toString();
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java (Java)

Code before:
        }

        // Mark the current node
        isVisited[u] = true;

        // Recursion for all the vertices adjacent to current vertex
        
        for (Integer i : adjList[u]) {
            if (!isVisited[i]) {
                // store current node in path[]
                localPathList.add(i);
                printAllPathsUtil(i, d, isVisited, localPathList,a);

                // remove current node in path[]
                localPathList.remove(i);
            }
        }

        // Mark the current node
        isVisited[u] = false;
        return a[0];
        // returns number of paths from source to destination
    }

    // Driver program
    public static boolean allPathsFromSourceToTarget(int vertices, int a[][], int source, int destination, int num_of_paths)
    {
        // Create a sample graph
        AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget(vertices);
        for(int i=0 ; i<a.length ; i++)
        {
            g.addEdge(a[i][0], a[i][1]);
            // edges are added
        }
        System.out.println("Following are all different paths from "+ source + " to " + destination);
        int c = g.printAllPaths(source, destination);
        // method call to find number of paths
        return c == num_of_paths;
        // returns true if number of paths calculated from source to destination matches with given paths
    }
}

Review comment (suggestion):
```suggestion
    public static boolean allPathsFromSourceToTarget(int vertices, int a[][], int source, int destination, int num_of_paths)
```
Parameter names are not camelCase

Code after:
        for (Integer i : adjList[u]) {
            if (!isVisited[i]) {
                // store current node in path[]
                localPathList.add(i);
                storeAllPathsUtil(i, d, isVisited, localPathList);

                // remove current node in path[]
                localPathList.remove(i);
            }
        }

        // Mark the current node
        isVisited[u] = false;
    }

    // Driver program
    public static List<List<Integer>> allPathsFromSourceToTarget(int vertices, int a[][], int source, int destination)
    {
        // Create a sample graph
        AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget(vertices);
        for(int i=0 ; i<a.length ; i++)
        {
            g.addEdge(a[i][0], a[i][1]);
            // edges are added
        }
        g.storeAllPaths(source, destination);
        // method call to store all possible paths
        return nm;
        // returns all possible paths from source to destination
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/strings/StringCompression.java (Java)

Code before:
        res = checkCount(res, count, ch, i);
        break;
      } else if (ch[i] != ch[i + 1]) {
        if ((i + 1) == ch.length - 1) {
          res = checkCount(res, count, ch, i) + ch[i + 1];
          break;
        } else {
          res = checkCount(res, count, ch, i);
          count = 1;
        }
      }
    }
    // This loop is not necessary unless you are doing it for leetcode submission
    for (int i = 0; i < res.length(); i++) {
      ch[i] = res.charAt(i);
    }
    return res;
  }
  /**
   * @param res   the resulting string
   * @param count current count
   * @param ch    the character array
   * @param i     the index at which the count is to be appended
   * @return the res string appended with the count
   */
  public static String checkCount(String res, int count, char[] ch, int i) {
    if (count > 1) {
      res += ch[i] + "" + count;
      count = 1;
    } else {
      res += ch[i] + "";
    }
    return res;
  }
}

Review comment (suggestion):
This funciton only uses `ch[i]`, so you could only pass the current character to it and not the whole array

Code after:
        compressedString = appendCount(compressedString, count, input.charAt(i));
        break;
      } else if (input.charAt(i) != input.charAt(i+1)) {
        if ((i + 1) == input.length() - 1) {
          compressedString = appendCount(compressedString, count, input.charAt(i)) + input.charAt(i+1);
          break;
        } else {
          compressedString = appendCount(compressedString, count, input.charAt(i));
          count = 1;
        }
      }
    }
    return compressedString;
  }
  /**
   * @param res   the resulting string
   * @param count current count
   * @param ch    the character at a particular index
   * @return the res string appended with the count
   */
  public static String appendCount(String res, int count, char ch) {
    if (count > 1) {
      res += ch + "" + count;
      count = 1;
    } else {
      res += ch + "";
    }
    return res;
  }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/datastructures/hashmap/hashing/MajorityElement.java (Java)

Code before:
import java.util.HashMap;

public class MajorityElement {
  
    public static void majority(int[] nums){
        HashMap<Integer,Integer> map = new HashMap<>();
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            if (map.containsKey(nums[i])){ //true
                map.put(nums[i],map.get(nums[i])+1);
            }else { //false or element appeared first time
                map.put(nums[i],1);
            }
        }
        for (int key: map.keySet()) {
            if (map.get(key) > n/3){
                System.out.println(key);
            }
        }
    }
  
    public static void main(String[] args) {
        int[] nums = {1,3,3,3,2,5,1,3,1,5,1};
        majority(nums);
    }
}


Review comment (suggestion):
Add some Description about the Algorithm you are proposing. You could add some links that talks about the problem. like a GFG link mayby

Code after:
package com.thealgorithms.datastructures.hashmap.hashing;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
/*
This class finds the majority element(s) in an array of integers.
A majority element is an element that appears more than or equal to n/2 times, where n is the length of the array.
*/
public class MajorityElement {
      /*
     This method returns the majority element(s) in the given array of integers.
     @param nums: an array of integers
     @return a list of majority elements
     */
    public static List<Integer> majority(int[] nums){
        HashMap<Integer,Integer> numToCount  = new HashMap<>();
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            if (numToCount.containsKey(nums[i])){
                numToCount.put(nums[i],numToCount.get(nums[i])+1);
            } else {
                numToCount.put(nums[i],1);
            }
        }
        List<Integer> majorityElements = new ArrayList<>();
        for (int key: numToCount.keySet()) {
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/datastructures/hashmap/hashing/MajorityElement.java (Java)

Code before:
import java.util.HashMap;

public class MajorityElement {
  
    public static void majority(int[] nums){
        HashMap<Integer,Integer> map = new HashMap<>();
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            if (map.containsKey(nums[i])){ //true
                map.put(nums[i],map.get(nums[i])+1);
            }else { //false or element appeared first time
                map.put(nums[i],1);
            }
        }
        for (int key: map.keySet()) {
            if (map.get(key) > n/3){
                System.out.println(key);
            }
        }
    }
  
    public static void main(String[] args) {
        int[] nums = {1,3,3,3,2,5,1,3,1,5,1};
        majority(nums);
    }
}


Review comment (refactor):
remove this main method and instead put some Junit tests. You could refer to https://github.com/TheAlgorithms/Java/tree/master/src/test/java/com/thealgorithms/datastructures/hashmap/hashing

Code after:
package com.thealgorithms.datastructures.hashmap.hashing;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
/*
This class finds the majority element(s) in an array of integers.
A majority element is an element that appears more than or equal to n/2 times, where n is the length of the array.
*/
public class MajorityElement {
      /*
     This method returns the majority element(s) in the given array of integers.
     @param nums: an array of integers
     @return a list of majority elements
     */
    public static List<Integer> majority(int[] nums){
        HashMap<Integer,Integer> numToCount  = new HashMap<>();
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            if (numToCount.containsKey(nums[i])){
                numToCount.put(nums[i],numToCount.get(nums[i])+1);
            } else {
                numToCount.put(nums[i],1);
            }
        }
        List<Integer> majorityElements = new ArrayList<>();
        for (int key: numToCount.keySet()) {
            if (numToCount.get(key) >= n/2){
                majorityElements.add(key);
            }
        }
        return majorityElements;
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/LeonardoNumber.java (Java)

Code before:
package com.thealgorithms.maths;

 /**
  * https://en.wikipedia.org/wiki/Leonardo_number
  */
public class LeonardoNumber {

    /**
     * Calculate nth number of Leonardo Number (1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, ...)
     *
     * @param n nth
     * @return nth number of Leonardo sequences
     */
    public static int leonardoNumber(int n) {
        if (n < 0) {
            return 0;
        }
        if (n == 0 || n == 1) {
            return 1;
        }
        return (leonardoNumber(n - 1) + leonardoNumber(n - 2) + 1);
    }
}


Review comment (suggestion):
```suggestion
     * Calculate nth Leonardo Number (1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, ...)
```

Code after:
package com.thealgorithms.maths;

 /**
  * https://en.wikipedia.org/wiki/Leonardo_number
  */
public class LeonardoNumber {

    /**
     * Calculate nth Leonardo Number (1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, ...)
     *
     * @param n the index of Leonardo Number to calculate
     * @return nth number of Leonardo sequences
     */
    public static int leonardoNumber(int n) {
        if (n < 0) {
            throw new ArithmeticException();
        }
        if (n == 0 || n == 1) {
            return 1;
        }
        return (leonardoNumber(n - 1) + leonardoNumber(n - 2) + 1);
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/LeonardoNumber.java (Java)

Code before:
package com.thealgorithms.maths;

 /**
  * https://en.wikipedia.org/wiki/Leonardo_number
  */
public class LeonardoNumber {

    /**
     * Calculate nth number of Leonardo Number (1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, ...)
     *
     * @param n nth
     * @return nth number of Leonardo sequences
     */
    public static int leonardoNumber(int n) {
        if (n < 0) {
            return 0;
        }
        if (n == 0 || n == 1) {
            return 1;
        }
        return (leonardoNumber(n - 1) + leonardoNumber(n - 2) + 1);
    }
}


Review comment (suggestion):
```suggestion
     * @param n the index of Leonardo Number to calculate
```

Code after:
package com.thealgorithms.maths;

 /**
  * https://en.wikipedia.org/wiki/Leonardo_number
  */
public class LeonardoNumber {

    /**
     * Calculate nth Leonardo Number (1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, ...)
     *
     * @param n the index of Leonardo Number to calculate
     * @return nth number of Leonardo sequences
     */
    public static int leonardoNumber(int n) {
        if (n < 0) {
            throw new ArithmeticException();
        }
        if (n == 0 || n == 1) {
            return 1;
        }
        return (leonardoNumber(n - 1) + leonardoNumber(n - 2) + 1);
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/Median.java (Java)

Code before:
package com.thealgorithms.maths;

import java.util.Arrays;

/**
 * Wikipedia: https://en.wikipedia.org/wiki/Median
 */
public class Median {

    /**
     * Calculate average median
     * Sorting is guaranteed
     * @param values number series
     * @return median of given {@code values}
     */
    public static double median(int[] values) {
        Arrays.sort(values);
        int length = values.length;
        return length % 2 == 0
            ? (values[length / 2] + values[length / 2 - 1]) / 2.0
            : values[length / 2];
    }
}


Review comment (suggestion):
```suggestion
     * @param values sorted numbers to find median of
```

Code after:
package com.thealgorithms.maths;

import java.util.Arrays;

/**
 * Wikipedia: https://en.wikipedia.org/wiki/Median
 */
public class Median {

    /**
     * Calculate average median
     * @param values sorted numbers to find median of
     * @return median of given {@code values}
     */
    public static double median(int[] values) {
        Arrays.sort(values);
        int length = values.length;
        return length % 2 == 0
            ? (values[length / 2] + values[length / 2 - 1]) / 2.0
            : values[length / 2];
    }
}
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/maths/LeonardoNumberTest.java (Java)

Code before:
package com.thealgorithms.maths;

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

public class LeonardoNumberTest {
    @Test
        //Negative Leonardo Number
    void leonardoNumberNegative() {
        assertEquals(0, LeonardoNumber.leonardoNumber(-1));
    }
    @Test
        //Zero Leonardo Number
    void leonardoNumberZero() {
        assertEquals(1, LeonardoNumber.leonardoNumber(0));
    }
    @Test
        //First Leonardo Number
    void leonardoNumberOne() {
        assertEquals(1, LeonardoNumber.leonardoNumber(1));
    }
    @Test
        //Fifth Leonardo Number
    void leonardoNumberFive() {
        assertEquals(15, LeonardoNumber.leonardoNumber(5));
    }
    @Test
        //Twentieth Leonardo Number
    void leonardoNumberTwenty() {
        assertEquals(21891 , LeonardoNumber.leonardoNumber(20));
    }
}


Review comment (suggestion):
An exception must be thrown because there is no -1st Leonardo Number

Code after:
package com.thealgorithms.maths;

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

public class LeonardoNumberTest {
    @Test
    void leonardoNumberNegative() {
        assertThrows(ArithmeticException.class, ()-> LeonardoNumber.leonardoNumber(-1));
    }
    @Test
    void leonardoNumberZero() {
        assertEquals(1, LeonardoNumber.leonardoNumber(0));
    }
    @Test
    void leonardoNumberOne() {
        assertEquals(1, LeonardoNumber.leonardoNumber(1));
    }
    @Test
    void leonardoNumberFive() {
        assertEquals(15, LeonardoNumber.leonardoNumber(5));
    }
    @Test
    void leonardoNumberTwenty() {
        assertEquals(21891 , LeonardoNumber.leonardoNumber(20));
    }
}