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

Code before:
    head1->val = 1;

    ListNode* temp = head1;
    for (int i = 2; i < 6; ++i) {
        // Allocate next
        auto* temp1 = new ListNode;
        temp1->val = i;

        // Advance
        temp->next = temp1;
        temp = temp1;
    }
    temp->next = nullptr;

    ListNode* median = search::median_search2::middleNode(head1);
    assert(3 == median->val);  // 3 is the value of the median node.
    std::cout << "test case:1 passed\n";

    // Clean up
    while (head1) {
        ListNode* t = head1;
        head1 = head1->next;
        delete t;
    }
    delete head1;
    delete temp;

    // Test case # 2
    auto* head2 = new ListNode;
    head2->val = 1;

    ListNode* temp2 = head2;
    for (int i = 2; i < 7; ++i) {
        // Allocate next
        auto* temp3 = new ListNode;
        temp3->val = i;

        // Advance
        temp2->next = temp3;
        temp2 = temp3;
    }
    temp2->next = nullptr;

    ListNode* median1 = search::median_search2::middleNode(head2);
    assert(4 == median1->val);  // 4 is the value of the median node.
    std::cout << "test case:2 passed\n";

    // Clean up
    while (head2) {
        ListNode* t = head2;
        head2 = head2->next;

Review comment (suggestion):
We're already freeing the `head1` memory twice here.
```suggestion
```


Code after:
    head1->val = 1;

    ListNode* temp = head1;
    for (int i = 2; i < 6; ++i) {
        // Allocate next
        auto* temp1 = new ListNode;
        temp1->val = i;

        // Advance
        temp->next = temp1;
        temp = temp1;
    }
    temp->next = nullptr;

    ListNode* median = search::median_search2::middleNode(head1);
    assert(3 == median->val);  // 3 is the value of the median node.
    std::cout << "test case:1 passed\n";

    // Test case # 2
    auto* head2 = new ListNode;
    head2->val = 1;

    ListNode* temp2 = head2;
    for (int i = 2; i < 7; ++i) {
        // Allocate next
        auto* temp3 = new ListNode;
        temp3->val = i;

        // Advance
        temp2->next = temp3;
        temp2 = temp3;
    }
    temp2->next = nullptr;

    ListNode* median1 = search::median_search2::middleNode(head2);
    assert(4 == median1->val);  // 4 is the value of the median node.
    std::cout << "test case:2 passed\n";

    delete head1;
    delete temp;

    delete head2;
    delete temp2;

    std::cout << "--All tests passed--\n";
}

/**
 * @brief Main function
 * @returns 0 on exit
 */
Repository: TheAlgorithms/C-Plus-Plus
File: search/median_search2.cpp (C++)

Code before:
    // Test case # 2
    auto* head2 = new ListNode;
    head2->val = 1;

    ListNode* temp2 = head2;
    for (int i = 2; i < 7; ++i) {
        // Allocate next
        auto* temp3 = new ListNode;
        temp3->val = i;

        // Advance
        temp2->next = temp3;
        temp2 = temp3;
    }
    temp2->next = nullptr;

    ListNode* median1 = search::median_search2::middleNode(head2);
    assert(4 == median1->val);  // 4 is the value of the median node.
    std::cout << "test case:2 passed\n";

    // Clean up
    while (head2) {
        ListNode* t = head2;
        head2 = head2->next;
        delete t;
    }
    delete head2;
    delete temp2;

    std::cout << "--All tests passed--\n";
}

/**
 * @brief Main function
 * @returns 0 on exit
 */
int main() {
    test();  // run self-test implementations
    return 0;
}


Review comment (suggestion):
Same here.
```suggestion
    delete head1;
    delete temp;
    
```


Code after:

        // Advance
        temp2->next = temp3;
        temp2 = temp3;
    }
    temp2->next = nullptr;

    ListNode* median1 = search::median_search2::middleNode(head2);
    assert(4 == median1->val);  // 4 is the value of the median node.
    std::cout << "test case:2 passed\n";

    delete head1;
    delete temp;

    delete head2;
    delete temp2;

    std::cout << "--All tests passed--\n";
}

/**
 * @brief Main function
 * @returns 0 on exit
 */
int main() {
    test();  // run self-test implementations
    return 0;
}
Repository: TheAlgorithms/C-Plus-Plus
File: search/linear_search.cpp (C++)

Code before:
{
    int mode = 0;

    std::cout << "Choose mode\n";
    std::cout << "Self-test mode (1), interactive mode (2): ";

    std::cin >> mode;

    if (mode == 2)
    {
        int size = 0;
        std::cout << "\nEnter the size of the array [in range 1-30 ]: ";
        std::cin >> size;

        while (size <= 0 || size > 30)
        {
            if (size <= 0)
            {
                std::cout << "Size cannot be less than zero. Please choose another value: ";
                std::cin >> size;
            }
            if (size > 30)
            {
                std::cout << "Size entered more than 30. Please choose a lower value: ";
                std::cin >> size;
            }
        }

        int *array = new int[size];
        int key = 0;

        // Input for the array elements
        std::cout << "Enter the array of " << size << " numbers: ";
        for (int i = 0; i < size; i++)
        {
            std::cin >> array[i];
        }

        std::cout << "\nEnter the number to be searched: ";
        std::cin >> key;

        int index = LinearSearch(array, size, key);
        if (index != -1)
        {
            std::cout << "Number found at index: " << index << "\n";
        }
        else
        {
            std::cout << "Array element not found";
        }
        delete[] array;

Review comment (suggestion):
To make it shorter, we could do something like this. What do you think? 🙂
```suggestion
        while (size <= 0 || size > 30){
            std::cout << "Size can only be 1-30. Please choose another value: ";
            std::cin >> size;
        }
```


Code after:
{
    int mode = 0;

    std::cout << "Choose mode\n";
    std::cout << "Self-test mode (1), interactive mode (2): ";

    std::cin >> mode;

    if (mode == 2)
    {
        int size = 0;
        std::cout << "\nEnter the size of the array [in range 1-30 ]: ";
        std::cin >> size;

        while (size <= 0 || size > 30){
            std::cout << "Size can only be 1-30. Please choose another value: ";
            std::cin >> size;
        }

        int *array = new int[size];
        int key = 0;

        // Input for the array elements
        std::cout << "Enter the array of " << size << " numbers: ";
        for (int i = 0; i < size; i++)
        {
            std::cin >> array[i];
        }

        std::cout << "\nEnter the number to be searched: ";
        std::cin >> key;

        int index = LinearSearch(array, size, key);
        if (index != -1)
        {
            std::cout << "Number found at index: " << index << "\n";
        }
        else
        {
            std::cout << "Array element not found";
        }
        delete[] array;
    }
    else
    {
        tests(); // run self-test implementations
    }
    return 0;
}
Repository: TheAlgorithms/C-Plus-Plus
File: search/linear_search.cpp (C++)

Code before:
}

/**
 * @brief Main function
 * @returns 0 on exit
 */
int main()
{
    int mode = 0;

    std::cout << "Choose mode\n";
    std::cout << "Self-test mode (1), interactive mode (2): ";

    std::cin >> mode;

    if (mode == 2)
    {
        int size = 0;
        std::cout << "\nEnter the size of the array [in range 1-30 ]: ";
        std::cin >> size;

        while (size <= 0 || size > 30){
            std::cout << "Size can only be 1-30. Please choose another value: ";
            std::cin >> size;
        }
        }

        int *array = new int[size];
        int key = 0;

        // Input for the array elements
        std::cout << "Enter the array of " << size << " numbers: ";
        for (int i = 0; i < size; i++)
        {
            std::cin >> array[i];
        }

        std::cout << "\nEnter the number to be searched: ";
        std::cin >> key;

        int index = LinearSearch(array, size, key);
        if (index != -1)
        {
            std::cout << "Number found at index: " << index << "\n";
        }
        else
        {
            std::cout << "Array element not found";
        }
        delete[] array;
    }

Review comment (bug):
This seems to be causing the CI to fail.
```suggestion
```


Code after:
}

/**
 * @brief Main function
 * @returns 0 on exit
 */
int main()
{
    int mode = 0;

    std::cout << "Choose mode\n";
    std::cout << "Self-test mode (1), interactive mode (2): ";

    std::cin >> mode;

    if (mode == 2)
    {
        int size = 0;
        std::cout << "\nEnter the size of the array [in range 1-30 ]: ";
        std::cin >> size;

        while (size <= 0 || size > 30){
            std::cout << "Size can only be 1-30. Please choose another value: ";
            std::cin >> size;
        }

        int *array = new int[size];
        int key = 0;

        // Input for the array elements
        std::cout << "Enter the array of " << size << " numbers: ";
        for (int i = 0; i < size; i++)
        {
            std::cin >> array[i];
        }

        std::cout << "\nEnter the number to be searched: ";
        std::cin >> key;

        int index = LinearSearch(array, size, key);
        if (index != -1)
        {
            std::cout << "Number found at index: " << index << "\n";
        }
        else
        {
            std::cout << "Array element not found";
        }
        delete[] array;
    }
    else
Repository: TheAlgorithms/C-Plus-Plus
File: dynamic_programming/subset_sum.cpp (C++)

Code before:
/**
 * @file
 * @brief Implements [Sub-set sum problem]
 * (https://en.wikipedia.org/wiki/Subset_sum_problem) algorithm, which tells
 * whether a subset with target sum exists or not.
 *
 * @details
 * In this problem, we use dynamic programming to find if we can pull out a 
 * subset from an array whose sum is equal to a given target sum. The overall 
 * time complexity of the problem is O(n * targetSum) where n is the size of 
 * the array. For example, array = [1, -10, 2, 31, -6], targetSum = -14. 
 * Output: true => We can pick subset [-10, 2, -6] with sum as 
 * (-10) + 2 + (-6) = -14.
 * @author [KillerAV] (https://github.com/KillerAV)
 */

#include <cassert>        /// for std::assert
#include <iostream>       /// for IO operations
#include <vector>          /// for std::vector
#include <unordered_map>   /// for unordered map

/**
 * @namespace dynamic_programming
 * @brief Dynamic Programming algorithms
 */
namespace dynamic_programming {

/**
 * @namespace subset_sum
 * @brief Functions for [Sub-set sum problem]
 * (https://en.wikipedia.org/wiki/Subset_sum_problem) algorithm
 */
namespace subset_sum {

/**
 * Recursive function using dynamic programming to find if the required sum 
 * subset exists or not.
 * @param arr input array
 * @param targetSum the target sum of the subset

Review comment (security):
```suggestion
 * @author [KillerAV](https://github.com/KillerAV)
```


Code after:
/**
 * @file
 * @brief Implements [Sub-set sum problem]
 * (https://en.wikipedia.org/wiki/Subset_sum_problem) algorithm, which tells
 * whether a subset with target sum exists or not.
 *
 * @details
 * In this problem, we use dynamic programming to find if we can pull out a 
 * subset from an array whose sum is equal to a given target sum. The overall 
 * time complexity of the problem is O(n * targetSum) where n is the size of 
 * the array. For example, array = [1, -10, 2, 31, -6], targetSum = -14. 
 * Output: true => We can pick subset [-10, 2, -6] with sum as 
 * (-10) + 2 + (-6) = -14.
 * @author [KillerAV](https://github.com/KillerAV)
 */

#include <cassert>        /// for std::assert
#include <iostream>       /// for IO operations
#include <vector>          /// for std::vector
#include <unordered_map>   /// for unordered map

/**
 * @namespace dynamic_programming
 * @brief Dynamic Programming algorithms
 */
namespace dynamic_programming {

/**
 * @namespace subset_sum
 * @brief Functions for [Sub-set sum problem]
 * (https://en.wikipedia.org/wiki/Subset_sum_problem) algorithm
 */
namespace subset_sum {

/**
 * Recursive function using dynamic programming to find if the required sum 
 * subset exists or not.
 * @param arr input array
 * @param targetSum the target sum of the subset
Repository: TheAlgorithms/C-Plus-Plus
File: bit_manipulation/find_non_repeating_number.cpp (C++)

Code before:

 * @author [Ravidev Pandey](https://github.com/literalEval)
 */

#include <cassert>   /// for assert
#include <iostream>  /// for IO operations
#include <vector>    /// storing the numbers

/**
 * @namespace bit_manipulation
 * @brief Bit manipulation algorithms
 */
namespace bit_manipulation {
/**
 * @namespace setKthBit
 * @brief Functions to find the non repeating integer
 * in an array of repeating integers. [Single
 * Number](https://leetcode.com/problems/single-number/)
 */
namespace find_single_number {
/**
 * @brief The main function implements find single number
 * @param nums vector of integers
 * @returns returns the integer that occurs only once
 */
int find_single_number(std::vector<int>& nums) {
    // The idea is based on the property of XOR.
    // We know that 'a' XOR 'a' is '0' and '0' XOR 'b'
    // is b.
    // Using this, if we XOR all the elements of the array,
    // the repeating elements will give '0' and this '0'
    // with the single number will give the number itself.

    int _xor = 0;

    for (const int& num: nums) {
        _xor ^= num;
    }

    return _xor;
}
}  // namespace find_single_number
}  // namespace bit_manipulation

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test() {
    // n = 10,2 return 14


Review comment (suggestion):
You're not modifying `nums`, but you're accessing it. Using `const` will make the program faster and better.
```suggestion
int find_single_number(const std::vector<int>& nums) {
```


Code after:

 * @author [Ravidev Pandey](https://github.com/literalEval)
 */

#include <cassert>   /// for assert
#include <iostream>  /// for IO operations
#include <vector>    /// storing the numbers

/**
 * @namespace bit_manipulation
 * @brief Bit manipulation algorithms
 */
namespace bit_manipulation {
/**
 * @namespace find_non_repeating_integer
 * @brief Functions to find the non repeating integer
 * in an array of repeating integers. [Single
 * Number](https://leetcode.com/problems/single-number/)
 */
namespace find_non_repeating_integer {
/**
 * @brief The main function implements find single number
 * @param nums vector of integers
 * @returns returns the integer that occurs only once
 */
int64_t find_non_repeating_integer(const std::vector<int>& nums) {
    // The idea is based on the property of XOR.
    // We know that 'a' XOR 'a' is '0' and '0' XOR 'b'
    // is b.
    // Using this, if we XOR all the elements of the array,
    // the repeating elements will give '0' and this '0'
    // with the single number will give the number itself.

    int _xor = 0;

    for (const int& num: nums) {
        _xor ^= num;
    }

    return _xor;
}
}  // namespace find_non_repeating_integer
}  // namespace bit_manipulation

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test() {
    // n = 10,2 return 14
Repository: TheAlgorithms/C-Plus-Plus
File: bit_manipulation/find_non_repeating_number.cpp (C++)

Code before:

 * @author [Ravidev Pandey](https://github.com/literalEval)
 */

#include <cassert>   /// for assert
#include <iostream>  /// for IO operations
#include <vector>    /// storing the numbers

/**
 * @namespace bit_manipulation
 * @brief Bit manipulation algorithms
 */
namespace bit_manipulation {
/**
 * @namespace setKthBit
 * @brief Functions to find the non repeating integer
 * in an array of repeating integers. [Single
 * Number](https://leetcode.com/problems/single-number/)
 */
namespace find_single_number {
/**
 * @brief The main function implements find single number
 * @param nums vector of integers
 * @returns returns the integer that occurs only once
 */
int find_single_number(std::vector<int>& nums) {
    // The idea is based on the property of XOR.
    // We know that 'a' XOR 'a' is '0' and '0' XOR 'b'
    // is b.
    // Using this, if we XOR all the elements of the array,
    // the repeating elements will give '0' and this '0'
    // with the single number will give the number itself.

    int _xor = 0;

    for (const int& num: nums) {
        _xor ^= num;
    }

    return _xor;
}
}  // namespace find_single_number
}  // namespace bit_manipulation

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test() {
    // n = 10,2 return 14


Review comment (suggestion):
Consider using `uint64_t` for non-negative values (or their appropriate size: `uint32_t`, `uint16_t`, `uint8_t`) or `int64_t` for negative values. Check other parts of the code ([reference](https://en.cppreference.com/w/cpp/types/integer)). **Let us know if you need any help here or on our [Discord server](https://discord.gg/c7MnfGFGa6).** 🙂


Code after:

 * @author [Ravidev Pandey](https://github.com/literalEval)
 */

#include <cassert>   /// for assert
#include <iostream>  /// for IO operations
#include <vector>    /// storing the numbers

/**
 * @namespace bit_manipulation
 * @brief Bit manipulation algorithms
 */
namespace bit_manipulation {
/**
 * @namespace find_non_repeating_integer
 * @brief Functions to find the non repeating integer
 * in an array of repeating integers. [Single
 * Number](https://leetcode.com/problems/single-number/)
 */
namespace find_non_repeating_integer {
/**
 * @brief The main function implements find single number
 * @param nums vector of integers
 * @returns returns the integer that occurs only once
 */
int64_t find_non_repeating_integer(const std::vector<int>& nums) {
    // The idea is based on the property of XOR.
    // We know that 'a' XOR 'a' is '0' and '0' XOR 'b'
    // is b.
    // Using this, if we XOR all the elements of the array,
    // the repeating elements will give '0' and this '0'
    // with the single number will give the number itself.

    int _xor = 0;

    for (const int& num: nums) {
        _xor ^= num;
    }

    return _xor;
}
}  // namespace find_non_repeating_integer
}  // namespace bit_manipulation

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test() {
    // n = 10,2 return 14
Repository: TheAlgorithms/C-Plus-Plus
File: bit_manipulation/travelling_salesman_using_bit_manipulation.cpp (C++)

Code before:
 */
static void test() {
    //Testcase 1
    std::vector<std::vector<uint32_t>> dist = {
        {0, 20, 42, 35}, {20, 0, 30, 34}, {42, 30, 0, 12}, {35, 34, 12, 0}
    };
    uint32_t V = dist.size();
    std::vector<std::vector<uint32_t>> dp(1 << V, std::vector<uint32_t>(V, -1));
    assert(bit_manipulation::travelling_salesman_using_bit_manipulation::travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp) == 97);
    std::cout << "Testcase 1 Passed" << "\n";

    //TestCase 2;
    dist = {{0, 5, 10, 15}, {5, 0, 20, 30}, {10, 20, 0, 35}, {15, 30, 35, 0}};
    V = dist.size();
    std::vector<std::vector<uint32_t>> dp1(1 << V, std::vector<uint32_t>(V, -1));
    assert(bit_manipulation::travelling_salesman_using_bit_manipulation::travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp1) == 75);
    std::cout << "Testcase 2 Passed" << "\n";
    //TestCase 3
    dist = {
        {0, 10, 15, 20}, {10, 0, 35, 25}, {15, 35, 0, 30}, {20, 25, 30, 0}
    };
    V = dist.size();
    std::vector<std::vector<uint32_t>> dp2(1 << V, std::vector<uint32_t>(V, -1));
    assert(bit_manipulation::travelling_salesman_using_bit_manipulation::travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp2) == 80);

    std::cout << "Testcase 3 Passed" << "\n";

}
/**
 * @brief Main function
 * @returns 0 on exit
 */
int main() {
    test();  // run self-test implementations
    return 0;
}


Review comment (suggestion):
```suggestion
    std::cout << "3rd test-case: passed!" << "\n";
```


Code after:
    // 1st test-case
    std::vector<std::vector<uint32_t>> dist = {
        {0, 20, 42, 35}, {20, 0, 30, 34}, {42, 30, 0, 12}, {35, 34, 12, 0}
    };
    uint32_t V = dist.size();
    std::vector<std::vector<uint32_t>> dp(1 << V, std::vector<uint32_t>(V, -1));
    assert(bit_manipulation::travelling_salesman_using_bit_manipulation::travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp) == 97);
    std::cout << "1st test-case: passed!" << "\n";

    // 2nd test-case
    dist = {{0, 5, 10, 15}, {5, 0, 20, 30}, {10, 20, 0, 35}, {15, 30, 35, 0}};
    V = dist.size();
    std::vector<std::vector<uint32_t>> dp1(1 << V, std::vector<uint32_t>(V, -1));
    assert(bit_manipulation::travelling_salesman_using_bit_manipulation::travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp1) == 75);
    std::cout << "2nd test-case: passed!" << "\n";
    // 3rd test-case
    dist = {
        {0, 10, 15, 20}, {10, 0, 35, 25}, {15, 35, 0, 30}, {20, 25, 30, 0}
    };
    V = dist.size();
    std::vector<std::vector<uint32_t>> dp2(1 << V, std::vector<uint32_t>(V, -1));
    assert(bit_manipulation::travelling_salesman_using_bit_manipulation::travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp2) == 80);

    std::cout << "3rd test-case: passed!" << "\n";

}

/**
 * @brief Main function
 * @returns 0 on exit
 */
int main() {
    test();  // run self-test implementations
    return 0;
}