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

Code before:
/**
 * @file
 * @brief [Exponential Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 * @brief [Exponential Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 * 
 * The exponential distribution is used to model
 * events occuring between a Poisson process like radioactive decay.
 * 
 * \( P(x, \lambda) = \lambda e^{-\lambda x} \)
 * 
 * Summary of variables used:
 * \lambda : rate parameter
 */

#include<cmath> // For power function
#include<cassert> // For asserting the test cases
#include<iostream> // For I/O operation

/** 
 * @brief the expected value of the exponential distribution
 * @returns \( \mu = \frac{1}{\lambda} \)
 */
double exponential_expected(double lambda){
    if (lambda<=0){
        std::cout << "Error: Lambda must be greater than 0." << std::endl;
        assert(lambda>0);
    }
    return 1/lambda;
}

/** 
 * @brief the variance of the exponential distribution
 * @returns \( \sigma^2 = \frac{1}{\lambda^2} \)
 */
double exponential_var(double lambda){
    if (lambda<=0){
        std::cout << "Error: Lambda must be greater than 0." << std::endl;

Review comment (suggestion):
```suggestion
 * \f$\lambda\f$ : rate parameter
```

Code after:
/**
 * @file
 * @brief [Exponential
 * Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 *
 * The exponential distribution is used to model
 * events occuring between a Poisson process like radioactive decay.
 *
 * \f[P(x, \lambda) = \lambda e^{-\lambda x}\f]
 *
 * Summary of variables used:
 * \f$\lambda\f$ : rate parameter
 */

#include <cassert>    // For assert
#include <cmath>      // For std::pow
#include <iostream>   // For I/O operation
#include <stdexcept>  // For std::invalid_argument
#include <string>     // For std::string

/**
 * @namespace probability
 * @brief Probability algorithms
 */
namespace probability {
/**
 * @namespace exponential_dist
 * @brief Functions for the [Exponential
 * Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 * algorithm implementation
 */
namespace geometric_dist {
/**
 * @brief the expected value of the exponential distribution
 * @returns \f[\mu = \frac{1}{\lambda}\f]
 */
double exponential_expected(double lambda) {
Repository: TheAlgorithms/C-Plus-Plus
File: probability/exponential_dist.cpp (C++)

Code before:
    std::cout<<std::endl;

    //Test 2
    std::cout << "Variance" << std::endl;
    std::cout << "Lambda : " << lambda << std::endl;
    std::cout << "Expected Output : " << var <<std::endl;
    std::cout << "Output : " << exponential_var(lambda) << std::endl;
    assert(exponential_var(lambda)==var);
    std::cout << "TEST PASSED" << std::endl;
    std::cout<<std::endl;

    //Test 3
    std::cout << "Standard Deviation" << std::endl;
    std::cout << "Lambda : " << lambda << std::endl;
    std::cout << "Expected Output : " << std <<std::endl;
    std::cout << "Output : " << exponential_std(lambda) << std::endl;
    assert(exponential_std(lambda)==std);
    std::cout << "TEST PASSED" << std::endl;
    std::cout<<std::endl;
}

/**
 * @brief Main function
 * @return 0 on exit
 */
int main(){
    test(); // Self test implementation
    return 0;
}



Review comment (suggestion):
```suggestion

/**
 * @brief Self-test implementation
 * @returns void
 */
```

Code after:

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test() {
    double lambda_1 = 1;
    double expected_1 = 1;
    double var_1 = 1;
    double std_1 = 1;

    double lambda_2 = 2;
    double expected_2 = 0.5;
    double var_2 = 0.25;
    double std_2 = 0.5;

    double lambda_3 = 3;
    double expected_3 = 0.333333;
    double var_3 = 0.111111;
    double std_3 = 0.333333;

    double lambda_4 = 0;     // Test 0
    double lambda_5 = -2.3;  // Test negative value

    const float threshold = 1e-3f;

    std::cout << "Test for lambda = 1 \n";
    assert(
        std::abs(expected_1 - probability::geometric_dist::exponential_expected(
                                  lambda_1)) < threshold);
    assert(std::abs(var_1 - probability::geometric_dist::exponential_var(
                                lambda_1)) < threshold);
    assert(std::abs(std_1 - probability::geometric_dist::exponential_std(
                                lambda_1)) < threshold);
    std::cout << "ALL TEST PASSED\n\n";

    std::cout << "Test for lambda = 2 \n";
    assert(
        std::abs(expected_2 - probability::geometric_dist::exponential_expected(
                                  lambda_2)) < threshold);
    assert(std::abs(var_2 - probability::geometric_dist::exponential_var(
                                lambda_2)) < threshold);
    assert(std::abs(std_2 - probability::geometric_dist::exponential_std(
                                lambda_2)) < threshold);
    std::cout << "ALL TEST PASSED\n\n";

    std::cout << "Test for lambda = 3 \n";
    assert(
        std::abs(expected_3 - probability::geometric_dist::exponential_expected(
                                  lambda_3)) < threshold);
    assert(std::abs(var_3 - probability::geometric_dist::exponential_var(
Repository: TheAlgorithms/C-Plus-Plus
File: probability/exponential_dist.cpp (C++)

Code before:
/**
 * @file
 * @brief [Exponential Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 * 
 * The exponential distribution is used to model
 * events occuring between a Poisson process like radioactive decay.
 * 
 * \f[P(x, \lambda) = \lambda e^{-\lambda x}\f]
 * 
 * Summary of variables used:
 * \f$\lambda\f$ : rate parameter
 */

#include<cmath> // For power function
#include<cassert> // For asserting the test cases
#include<iostream> // For I/O operation

/** 
 * @brief the expected value of the exponential distribution
 * @returns \f[\mu = \frac{1}{\lambda}\f]
 */
double exponential_expected(double lambda){
    if (lambda<=0){
        std::cout << "Error: Lambda must be greater than 0." << std::endl;
        assert(lambda>0);
    }
    return 1/lambda;
}

/** 
 * @brief the variance of the exponential distribution
 * @returns \f[\sigma^2 = \frac{1}{\lambda^2}\f]
 */
double exponential_var(double lambda){
    if (lambda<=0){
        std::cout << "Error: Lambda must be greater than 0." << std::endl;
        assert(lambda>0);
    }
    return 1/pow(lambda,2);

Review comment (suggestion):
```suggestion
#include<cmath> // For std::pow
```

Code after:
/**
 * @file
 * @brief [Exponential
 * Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 *
 * The exponential distribution is used to model
 * events occuring between a Poisson process like radioactive decay.
 *
 * \f[P(x, \lambda) = \lambda e^{-\lambda x}\f]
 *
 * Summary of variables used:
 * \f$\lambda\f$ : rate parameter
 */

#include <cassert>    // For assert
#include <cmath>      // For std::pow
#include <iostream>   // For I/O operation
#include <stdexcept>  // For std::invalid_argument
#include <string>     // For std::string

/**
 * @namespace probability
 * @brief Probability algorithms
 */
namespace probability {
/**
 * @namespace exponential_dist
 * @brief Functions for the [Exponential
 * Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 * algorithm implementation
 */
namespace geometric_dist {
/**
 * @brief the expected value of the exponential distribution
 * @returns \f[\mu = \frac{1}{\lambda}\f]
 */
double exponential_expected(double lambda) {
    if (lambda <= 0) {
        throw std::invalid_argument("lambda must be greater than 0");
Repository: TheAlgorithms/C-Plus-Plus
File: probability/exponential_dist.cpp (C++)

Code before:
/**
 * @file
 * @brief [Exponential Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 * 
 * The exponential distribution is used to model
 * events occuring between a Poisson process like radioactive decay.
 * 
 * \f[P(x, \lambda) = \lambda e^{-\lambda x}\f]
 * 
 * Summary of variables used:
 * \f$\lambda\f$ : rate parameter
 */

#include<cmath> // For power function
#include<cassert> // For asserting the test cases
#include<iostream> // For I/O operation

/** 
 * @brief the expected value of the exponential distribution
 * @returns \f[\mu = \frac{1}{\lambda}\f]
 */
double exponential_expected(double lambda){
    if (lambda<=0){
        std::cout << "Error: Lambda must be greater than 0." << std::endl;
        assert(lambda>0);
    }
    return 1/lambda;
}

/** 
 * @brief the variance of the exponential distribution
 * @returns \f[\sigma^2 = \frac{1}{\lambda^2}\f]
 */
double exponential_var(double lambda){
    if (lambda<=0){
        std::cout << "Error: Lambda must be greater than 0." << std::endl;
        assert(lambda>0);
    }
    return 1/pow(lambda,2);
}

Review comment (suggestion):
```suggestion
#include<cassert> // For assert
```

Code after:
/**
 * @file
 * @brief [Exponential
 * Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 *
 * The exponential distribution is used to model
 * events occuring between a Poisson process like radioactive decay.
 *
 * \f[P(x, \lambda) = \lambda e^{-\lambda x}\f]
 *
 * Summary of variables used:
 * \f$\lambda\f$ : rate parameter
 */

#include <cassert>    // For assert
#include <cmath>      // For std::pow
#include <iostream>   // For I/O operation
#include <stdexcept>  // For std::invalid_argument
#include <string>     // For std::string

/**
 * @namespace probability
 * @brief Probability algorithms
 */
namespace probability {
/**
 * @namespace exponential_dist
 * @brief Functions for the [Exponential
 * Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 * algorithm implementation
 */
namespace geometric_dist {
/**
 * @brief the expected value of the exponential distribution
 * @returns \f[\mu = \frac{1}{\lambda}\f]
 */
double exponential_expected(double lambda) {
    if (lambda <= 0) {
        throw std::invalid_argument("lambda must be greater than 0");
    }
Repository: TheAlgorithms/C-Plus-Plus
File: others/lru_cache2.cpp (C++)

Code before:
    assert(cache.size() == 2);
    assert(cache.capacity() == 5);
    assert(!cache.empty());

    // test getting items from the cache
    assert(cache.get(1) == 10);
    assert(cache.get(-2) == 20);

    cache.put(-3, -30);
    cache.put(4, 40);
    cache.put(5, -50);
    cache.put(6, 60);

    // test the state after inserting more items than the capacity
    assert(cache.size() == 5);
    assert(cache.capacity() == 5);
    assert(!cache.empty());

    // fetching 1 throws runtime_error
    // as 1 was evicted being the least recently used
    // when 6 was added
    try {
        cache.get(1);
        assert(false);
    } catch (const std::runtime_error &e) {
        std::cout << "Expected - std::runtime_error\n";
    }

    // test retrieval of all items in the cache
    assert(cache.get(-2) == 20);
    assert(cache.get(-3) == -30);
    assert(cache.get(4) == 40);
    assert(cache.get(5) == -50);
    assert(cache.get(6) == 60);

    std::cout << "test - passed\n";
}

/**
 * @brief main function
 * @return 0 on exit
 */
int main() {
    test();  // run the self test implementation
    return 0;
}


Review comment (performance):
```suggestion
        assert(e.what() == "key is not present in the cache");
```

Code after:
    // test the state of cache after inserting some items
    assert(cache.size() == 2);
    assert(cache.capacity() == 5);
    assert(!cache.empty());

    // test getting items from the cache
    assert(cache.get(1) == 10);
    assert(cache.get(-2) == 20);

    cache.put(-3, -30);
    cache.put(4, 40);
    cache.put(5, -50);
    cache.put(6, 60);

    // test the state after inserting more items than the capacity
    assert(cache.size() == 5);
    assert(cache.capacity() == 5);
    assert(!cache.empty());

    // fetching 1 throws runtime_error
    // as 1 was evicted being the least recently used
    // when 6 was added
    try {
        cache.get(1);
    } catch (const std::runtime_error &e) {
        assert(std::string(e.what()) == "key is not present in the cache");
    }

    // test retrieval of all items in the cache
    assert(cache.get(-2) == 20);
    assert(cache.get(-3) == -30);
    assert(cache.get(4) == 40);
    assert(cache.get(5) == -50);
    assert(cache.get(6) == 60);

    std::cout << "test - passed\n";
}

/**
 * @brief main function
 * @return 0 on exit
 */
int main() {
    test();  // run the self test implementation
    return 0;
}
Repository: TheAlgorithms/C-Plus-Plus
File: others/lru_cache2.cpp (C++)

Code before:
/**
 * @file
 * @brief Implementation for [LRU Cache]
 * (https://en.wikipedia.org/wiki/Cache_replacement_policies#:~:text=Least%20Recently%20Used%20(LRU))
 *
 * @details
 * LRU discards the least recently used value.
 * Data structures used - doubly linked list and unordered_map
 *
 * unordered_map maps the key to the address of the node of the linked list.
 * If the element is accessed, the element is moved to the beginning of the
 * linked list.
 *
 * When the cache is full, the last element in the linked list is popped.
 *
 * @author [Karan Sharma](https://github.com/deDSeC00720)
 */

#include <cassert>        // for assert
#include <iostream>       // for std::cout
#include <unordered_map>  // for std::unordered_map

/**
 * @namespace
 * @brief Other algorithms
 */
namespace others {

/**
 * @namespace
 * @brief Cache algorithm
 */
namespace Cache {

/**
 * @class
 * @brief Node for a doubly linked list with data, prev and next pointers
 * @tparam T type of the data of the node
 */
template <typename T>
class D_Node {
 public:
    T data;           ///< data of the node
    D_Node<T> *prev;  ///< previous node in the doubly linked list
    D_Node<T> *next;  ///< next node in the doubly linked list


Review comment (suggestion):
```suggestion
#include <cassert>        // for assert
#include <iostream>       // for std::cout
#include <unordered_map>  // for std::unordered_map
#include <cstdint>        // for std::uint32_t
```

Code after:
/**
 * @file
 * @brief Implementation for [LRU Cache]
 * (https://en.wikipedia.org/wiki/Cache_replacement_policies#:~:text=Least%20Recently%20Used%20(LRU))
 *
 * @details
 * LRU discards the least recently used value.
 * Data structures used - doubly linked list and unordered_map
 *
 * unordered_map maps the key to the address of the node of the linked list.
 * If the element is accessed, the element is moved to the beginning of the
 * linked list.
 *
 * When the cache is full, the last element in the linked list is popped.
 *
 * @author [Karan Sharma](https://github.com/deDSeC00720)
 */

#include <cassert>        // for assert
#include <cstdint>        // for std::uint32_t
#include <iostream>       // for std::cout
#include <unordered_map>  // for std::unordered_map

/**
 * @namespace
 * @brief Other algorithms
 */
namespace others {

/**
 * @namespace
 * @brief Cache algorithm
 */
namespace Cache {

/**
 * @class
 * @brief Node for a doubly linked list with data, prev and next pointers
 * @tparam T type of the data of the node
 */
template <typename T>
class D_Node {
 public:
    T data;           ///< data of the node
    D_Node<T> *prev;  ///< previous node in the doubly linked list
    D_Node<T> *next;  ///< next node in the doubly linked list
Repository: TheAlgorithms/C-Plus-Plus
File: dynamic_programming/Unbounded_0_1_Knapsack.cpp (C++)

Code before:
    assert(unboundedKnapsack(N3, W3, val3, wt3) == 27);
    std::cout << "Maximum Knapsack value " << unboundedKnapsack(N3, W3, val3, wt3) << std::endl;

    // Test Case 4
    std::uint16_t N4 = 0; // Number of items
    std::vector<std::uint16_t> wt4 = {}; // Weights of the items
    std::vector<std::uint16_t> val4 = {}; // Values of the items 
    std::uint16_t W4 = 10; // Maximum capacity of the knapsack
    assert(unboundedKnapsack(N4, W4, val4, wt4) == 0); 
    std::cout << "Maximum Knapsack value for empty arrays: " << unboundedKnapsack(N4, W4, val4, wt4) << std::endl;
  
    std::cout << "All test cases passed!" << std::endl;

}

/**
 * @brief main function
 * @return 0 on successful exit
 */
int main() {
    tests(); // Run self test implementation 
    return 0;
}

}
}


Review comment (suggestion):
name spaces should not include main & test functions. they should have comments specifying which name space they enclose
```cpp
} // namespace_name
``` 

Code after:
    std::vector<std::uint16_t> wt3 = {2, 4, 6}; // Weights of the items
    std::vector<std::uint16_t> val3 = {5, 11, 13};// Values of the items 
    std::uint16_t W3 = 27;// Maximum capacity of the knapsack 
    // Test the function and assert the expected output
    assert(unboundedKnapsack(N3, W3, val3, wt3) == 27);
    std::cout << "Maximum Knapsack value " << unboundedKnapsack(N3, W3, val3, wt3) << std::endl;

    // Test Case 4
    std::uint16_t N4 = 0; // Number of items
    std::vector<std::uint16_t> wt4 = {}; // Weights of the items
    std::vector<std::uint16_t> val4 = {}; // Values of the items 
    std::uint16_t W4 = 10; // Maximum capacity of the knapsack
    assert(unboundedKnapsack(N4, W4, val4, wt4) == 0); 
    std::cout << "Maximum Knapsack value for empty arrays: " << unboundedKnapsack(N4, W4, val4, wt4) << std::endl;
  
    std::cout << "All test cases passed!" << std::endl;

}

/**
 * @brief main function
 * @return 0 on successful exit
 */
int main() {
    tests(); // Run self test implementation 
    return 0;
}
Repository: TheAlgorithms/C-Plus-Plus
File: cpu_scheduling_algorithms/non_preemptive_sjf_scheduling.cpp (C++)

Code before:
            get<1>(input[i]) = distr(eng);  // Random arrival time
            get<2>(input[i]) = distr(eng);  // Random burst time
        }

        // Print processes before scheduling
        // cout << "Processes before SJF scheduling:" << endl;
        // readyQueue.printResult(input);

        // Add processes to the queue
        for (uint32_t i{}; i < n; i++) {
            readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]),
                                  get<2>(input[i]));
        }

        // Perform SJF schedulings
        auto finalResult = readyQueue.scheduleForSJF();

        // Print processes after scheduling
        // cout << "\nProcesses after SJF scheduling:" << endl;
        // readyQueue.printResult(finalResult);
    }
    cout << "All the tests have successfully passed!" << endl;
}

/**
 * @brief Entry point of the program
 */
int main() {
    test();
    return 0;
}


Review comment (suggestion):
```suggestion
 * @brief Main function
 * @returns 0 on successful exit
```

Code after:

        // Print processes before scheduling
        cout << "Processes before SJF scheduling:" << endl;
        readyQueue.printResult(input);

        // Add processes to the queue
        for (uint32_t i{}; i < n; i++) {
            readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]),
                                  get<2>(input[i]));
        }

        // Perform SJF schedulings
        auto finalResult = readyQueue.scheduleForSJF();

        // Print processes after scheduling
        cout << "\nProcesses after SJF scheduling:" << endl;
        readyQueue.printResult(finalResult);
    }
    cout << "All the tests have successfully passed!" << endl;
}

/**
 * @brief Main function
 * @returns 0 on successful exit
 */
int main() {
    test();
    return 0;
}
Repository: TheAlgorithms/C-Plus-Plus
File: greedy_algorithms/binary_addition.cpp (C++)

Code before:

    // Valid binary string tests
    assert(binaryAddition.addBinary("1010", "1101") == "10111");
    assert(binaryAddition.addBinary("1111", "1111") == "11110");
    assert(binaryAddition.addBinary("101", "11") == "1000");
    assert(binaryAddition.addBinary("0", "0") == "0");
    assert(binaryAddition.addBinary("1111", "1111") == "11110");
    assert(binaryAddition.addBinary("0", "10101") == "10101");
    assert(binaryAddition.addBinary("10101", "0") == "10101");
    assert(binaryAddition.addBinary("101010101010101010101010101010",
                                    "110110110110110110110110110110") ==
           "1100001100001100001100001100000");
    assert(binaryAddition.addBinary("1", "11111111") == "100000000");
    assert(binaryAddition.addBinary("10101010", "01010101") == "11111111");

    // Invalid binary string tests (should return empty string)
    assert(binaryAddition.addBinary("10102", "1101") == "");
    assert(binaryAddition.addBinary("ABC", "1101") == "");
    assert(binaryAddition.addBinary("1010", "1102") == "");
    assert(binaryAddition.addBinary("111", "1x1") == "");
    assert(binaryAddition.addBinary("1x1", "111") == "");
    assert(binaryAddition.addBinary("1234", "1101") == "");
}

/**
 * Main function to execute the program.
 */
int main() {
    tests();
    std::cout << "All tests passed.\n";
    return 0;
}


Review comment (suggestion):
```suggestion
 * @brief main function
 * @return 0 on successful exit
```

Code after:

    // Valid binary string tests
    assert(binaryAddition.addBinary("1010", "1101") == "10111");
    assert(binaryAddition.addBinary("1111", "1111") == "11110");
    assert(binaryAddition.addBinary("101", "11") == "1000");
    assert(binaryAddition.addBinary("0", "0") == "0");
    assert(binaryAddition.addBinary("1111", "1111") == "11110");
    assert(binaryAddition.addBinary("0", "10101") == "10101");
    assert(binaryAddition.addBinary("10101", "0") == "10101");
    assert(binaryAddition.addBinary("101010101010101010101010101010",
                                    "110110110110110110110110110110") ==
           "1100001100001100001100001100000");
    assert(binaryAddition.addBinary("1", "11111111") == "100000000");
    assert(binaryAddition.addBinary("10101010", "01010101") == "11111111");

    // Invalid binary string tests (should return empty string)
    assert(binaryAddition.addBinary("10102", "1101") == "");
    assert(binaryAddition.addBinary("ABC", "1101") == "");
    assert(binaryAddition.addBinary("1010", "1102") == "");
    assert(binaryAddition.addBinary("111", "1x1") == "");
    assert(binaryAddition.addBinary("1x1", "111") == "");
    assert(binaryAddition.addBinary("1234", "1101") == "");
}

/**
 * @brief main function
 * @returns 0 on successful exit
 */
int main() {
    tests(); /// To execute tests
    return 0;
}