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

Code before:
/**
* @file
* @brief C++ program for maximum contiguous circular sum problem using [Kadane's Algorithm](https://en.wikipedia.org/wiki/Maximum_subarray_problem)
* @details
* The idea is to modify Kadane’s algorithm to find a minimum contiguous subarray sum and the maximum contiguous subarray sum,
* then check for the maximum value between the max_value and the value left after subtracting min_value from the total sum.
* [Geeks For Geeks](https://www.geeksforgeeks.org/maximum-contiguous-circular-sum/)
*/

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


/**
 * @namespace dynamic_programming
 * @brief Dynamic Programming algorithms
 */
namespace dynamic_programming {
/**
 * @brief returns the maximum contiguous circular sum of an array
 * 
 * @param arr is the array/vector
 * @return int which is the maximum sum
 */
int maxCircularSum(std::vector<int>& arr)
{
    // Edge Case
    if (arr.size() == 1)
        return arr[0];
  
    // Sum variable which stores total sum of the array.

Review comment (suggestion):
```suggestion
* For more information, check [Geeks For Geeks](https://www.geeksforgeeks.org/maximum-contiguous-circular-sum/) explanation page.
```


Code after:
/**
* @file
* @brief C++ program for maximum contiguous circular sum problem using [Kadane's Algorithm](https://en.wikipedia.org/wiki/Maximum_subarray_problem)
* @details
* The idea is to modify Kadane’s algorithm to find a minimum contiguous subarray sum and the maximum contiguous subarray sum,
* then check for the maximum value between the max_value and the value left after subtracting min_value from the total sum.
* For more information, check [Geeks For Geeks](https://www.geeksforgeeks.org/maximum-contiguous-circular-sum/) explanation page.
*/

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


/**
 * @namespace dynamic_programming
 * @brief Dynamic Programming algorithms
 */
namespace dynamic_programming {
/**
 * @brief returns the maximum contiguous circular sum of an array
 * 
 * @param arr is the array/vector
 * @return int which is the maximum sum
 */
int maxCircularSum(std::vector<int>& arr)
{
    // Edge Case
    if (arr.size() == 1)
        return arr[0];
  
    // Sum variable which stores total sum of the array.
Repository: TheAlgorithms/C-Plus-Plus
File: physics/ground_to_ground_projectile_motion.cpp (C++)

Code before:
    assert(horizontal_range_output == expected_horizontal_range);
    std::cout << "TEST PASSED" << std::endl << std::endl;

    // 3rd test
    double expected_max_height = 0.526; // expected height output
    double max_height_output =
        std::round(physics::max_height(initial_velocity, angle) * 1000.0) /
        1000.0;  // round output to 3 decimal places

    std::cout << "Projectile Max Height (double)" << std::endl;
    std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
    std::cout << "Input Angle: " << angle << std::endl;
    std::cout << "Expected Output: " << expected_max_height << std::endl;
    std::cout << "Output: " << max_height_output << std::endl;
    assert(max_height_output == expected_max_height);
    std::cout << "TEST PASSED" << std::endl << std::endl;
}

/**
 * @brief Main function
 * @returns 0 on exit
 */
int main() {
    test();  // run self-test implementations
    return 0;
}

Review comment (suggestion):
Missing an endline. Without this endline, there could be issues with some IDEs and configurations.
```suggestion
}

```


Code after:
    std::cout << "Projectile Horizontal Range (double)" << std::endl;
    std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
    std::cout << "Input Angle: " << angle << std::endl;
    std::cout << "Input Time Of Flight: " << flight_time_output << std::endl;
    std::cout << "Expected Output: " << expected_horizontal_range << std::endl;
    std::cout << "Output: " << horizontal_range_output << std::endl;
    assert(horizontal_range_output == expected_horizontal_range);
    std::cout << "TEST PASSED" << std::endl << std::endl;

    // 3rd test
    double expected_max_height = 0.526; // expected height output
    double max_height_output =
        std::round(physics::ground_to_ground_projectile_motion::max_height(initial_velocity, angle) * 1000.0) /
        1000.0;  // round output to 3 decimal places

    std::cout << "Projectile Max Height (double)" << std::endl;
    std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
    std::cout << "Input Angle: " << angle << std::endl;
    std::cout << "Expected Output: " << expected_max_height << std::endl;
    std::cout << "Output: " << max_height_output << std::endl;
    assert(max_height_output == expected_max_height);
    std::cout << "TEST PASSED" << std::endl << std::endl;
}

/**
 * @brief Main function
 * @returns 0 on exit
 */
int main() {
    test();  // run self-test implementations
    return 0;
}
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/queue.hpp (C++)

Code before:
        newNode->data = item;
        newNode->next = nullptr;
        if (isEmptyQueue()) {
            queueFront = newNode;
            queueRear = newNode;
        } else {
            queueRear->next = newNode;
            queueRear = queueRear->next;
        }
        ++size;
    }

    /** Return the first element of the queue */
    value_type front() const {
        ensureNotEmpty();
        return queueFront->data;
    }

    /** Remove the top element of the queue */
    void deQueue() {
        ensureNotEmpty();
        queueFront = queueFront->next;
        --size;
    }

    /** Clear queue */
    void clear() {
        queueFront = nullptr;
        queueRear = nullptr;
        size = 0;
    }

 private:
    std::shared_ptr<node_type> queueFront =
        {}; /**< Pointer to the front of the queue */
    std::shared_ptr<node_type> queueRear =
        {}; /**< Pointer to the rear of the queue  */
    std::size_t size = 0;
};

#endif  // DATA_STRUCTURES_QUEUE_HPP_


Review comment (suggestion):
IIRC, we might need to document all of these functions like this, so that Doxygen detects them properly.

```c++
/**
 * @brief <description>
 * @returns <return type>
 */
```


Code after:
            throw std::invalid_argument("Queue is empty.");
        }
    }

 public:
    /**
     * @brief checks if the queue has no elements
     * @return true if the queue is empty, false otherwise
     */
    bool isEmptyQueue() const { return (queueFront == nullptr); }

    /**
     * @brief inserts a new item into the queue
     */
    void enQueue(const value_type& item) {
        auto newNode = std::make_shared<node_type>();
        newNode->data = item;
        newNode->next = nullptr;
        if (isEmptyQueue()) {
            queueFront = newNode;
            queueRear = newNode;
        } else {
            queueRear->next = newNode;
            queueRear = queueRear->next;
        }
        ++size;
    }

    /**
     * @return the first element of the queue
     * @exception std::invalid_argument if queue is empty
     */
    value_type front() const {
        ensureNotEmpty();
        return queueFront->data;
    }

    /**
     * @brief removes the first element from the queue
     * @exception std::invalid_argument if queue is empty
     */
    void deQueue() {
        ensureNotEmpty();
        queueFront = queueFront->next;
        --size;
    }

    /**
     * @brief removes all elements from the queue
     */
    void clear() {
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/avltree.cpp (C++)

Code before:
/**
 * \file
 * \brief A simple tree implementation using nodes
 *
 * \todo update code to use C++ STL library features and OO structure
 * \warning This program is a poor implementation and does not utilize any of
 * the C++ STL features.
 */
#include <algorithm>  // for std::max
#include <iostream>   // for std::cout
#include <queue>      // for std::queue

using node = struct node {
    int data;
    int height;
    struct node *left;
    struct node *right;
};

/**
 * @brief creates and returns a new node
 * @param[in] data value stored in the node
 * @return newly created node
 */
node *createNode(int data) {
    node *nn = new node();
    nn->data = data;
    nn->height = 0;
    nn->left = nullptr;
    nn->right = nullptr;
    return nn;
}

/**
 * @param[in] root the root of the tree
 * @return height of tree

Review comment (suggestion):
```suggestion
#include <algorithm>  /// for std::max
#include <iostream>   /// for IO operations
#include <queue>      /// for std::queue
```


Code after:
/**
 * \file
 * \brief A simple tree implementation using nodes
 *
 * \todo update code to use C++ STL library features and OO structure
 * \warning This program is a poor implementation and does not utilize any of
 * the C++ STL features.
 */
#include <algorithm>  /// for std::max
#include <iostream>   /// for std::cout
#include <queue>      /// for std::queue

using node = struct node {
    int data;
    int height;
    struct node *left;
    struct node *right;
};

/**
 * @brief creates and returns a new node
 * @param[in] data value stored in the node
 * @return newly created node
 */
node *createNode(int data) {
    node *nn = new node();
    nn->data = data;
    nn->height = 0;
    nn->left = nullptr;
    nn->right = nullptr;
    return nn;
}

/**
 * @param[in] root the root of the tree
 * @return height of tree
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/avltree.cpp (C++)

Code before:
    if (root) {
        deleteAllNodes(root->left);
        deleteAllNodes(root->right);
        delete root;
    }
}

/**
 * @brief prints given tree in the LevelOrder
 * @param root of the tree
 */
void levelOrder(node *root) {
    std::queue<node *> q;
    q.push(root);
    while (!q.empty()) {
        root = q.front();
        std::cout << root->data << " ";
        q.pop();
        if (root->left) {
            q.push(root->left);
        }
        if (root->right) {
            q.push(root->right);
        }
    }
}

int main() {
    // Testing AVL Tree
    node *root = nullptr;
    int i = 0;
    for (i = 1; i <= 7; i++) root = insert(root, i);
    std::cout << "LevelOrder: ";
    levelOrder(root);
    root = deleteNode(root, 1);  // Deleting key with value 1
    std::cout << "\nLevelOrder: ";
    levelOrder(root);
    root = deleteNode(root, 4);  // Deletin key with value 4
    std::cout << "\nLevelOrder: ";
    levelOrder(root);
    deleteAllNodes(root);
    return 0;
}


Review comment (suggestion):
```suggestion
/**
 * @brief Main function
 * @returns 0 on exit
 */
int main() {
```


Code after:

/**
 * @brief prints given tree in the LevelOrder
 * @param[in] root of the tree
 */
void levelOrder(node *root) {
    std::queue<node *> q;
    q.push(root);
    while (!q.empty()) {
        root = q.front();
        std::cout << root->data << " ";
        q.pop();
        if (root->left) {
            q.push(root->left);
        }
        if (root->right) {
            q.push(root->right);
        }
    }
}

/**
 * @brief Main function
 * @returns 0 on exit
 */
int main() {
    // Testing AVL Tree
    node *root = nullptr;
    int i = 0;
    for (i = 1; i <= 7; i++) root = insert(root, i);
    std::cout << "LevelOrder: ";
    levelOrder(root);
    root = deleteNode(root, 1);  // Deleting key with value 1
    std::cout << "\nLevelOrder: ";
    levelOrder(root);
    root = deleteNode(root, 4);  // Deletin key with value 4
    std::cout << "\nLevelOrder: ";
    levelOrder(root);
    deleteAllNodes(root);
    return 0;
}
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/reverse_a_linked_list.cpp (C++)

Code before:
    assert(L.top() == 10);
    assert(otherList.top() == 10);
    assert(L.traverse(1) == 20);
    assert(otherList.traverse(1) == 20);

    assert(L.traverse(2) == 30);
    assert(otherList.traverse(2) == 30);

    assert(L.last() == 400);
    assert(otherList.last() == 40);
}

void test_assignment_operator() {
    data_structures::linked_list::list L;
    data_structures::linked_list::list otherList;
    L.insert(10);
    L.insert(20);
    L.insert(30);
    otherList = L;

    otherList.insert(40);
    L.insert(400);

    assert(L.top() == 10);
    assert(otherList.top() == 10);
    assert(L.traverse(1) == 20);
    assert(otherList.traverse(1) == 20);

    assert(L.traverse(2) == 30);
    assert(otherList.traverse(2) == 30);

    assert(L.last() == 400);
    assert(otherList.last() == 40);
}

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


Review comment (suggestion):
```suggestion
/**
 * @brief Self-test the assignment operator
 * @returns void
 */
static void test_assignment_operator() {
```


Code after:
    std::cout << "All tests have successfully passed!" << std::endl;
}

void test_copy_constructor() {
    data_structures::linked_list::list L;
    L.insert(10);
    L.insert(20);
    L.insert(30);
    data_structures::linked_list::list otherList(L);
    otherList.insert(40);

    L.insert(400);

    assert(L.top() == 10);
    assert(otherList.top() == 10);
    assert(L.traverse(1) == 20);
    assert(otherList.traverse(1) == 20);

    assert(L.traverse(2) == 30);
    assert(otherList.traverse(2) == 30);

    assert(L.last() == 400);
    assert(otherList.last() == 40);
}

void test_assignment_operator() {
    data_structures::linked_list::list L;
    data_structures::linked_list::list otherList;
    L.insert(10);
    L.insert(20);
    L.insert(30);
    otherList = L;

    otherList.insert(40);
    L.insert(400);

    assert(L.top() == 10);
    assert(otherList.top() == 10);
    assert(L.traverse(1) == 20);
    assert(otherList.traverse(1) == 20);

    assert(L.traverse(2) == 30);
    assert(otherList.traverse(2) == 30);

    assert(L.last() == 400);
    assert(otherList.last() == 40);
}

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

Code before:
    // 1st test
    L.insert(11);
    L.insert(12);
    L.insert(15);
    L.insert(10);
    L.insert(-12);
    L.insert(-20);
    L.insert(18);
    assert(L.top() == 11);
    assert(L.last() == 18);
    L.reverseList();
    // Reversal Testing
    assert(L.top() == 18);
    assert(L.traverse(1) == -20);
    assert(L.traverse(2) == -12);
    assert(L.traverse(3) == 10);
    assert(L.traverse(4) == 15);
    assert(L.traverse(5) == 12);
    assert(L.last() == 11);
    std::cout << "All tests have successfully passed!" << std::endl;
}

/**
 * @brief tests copy constructor
 */
void test_copy_constructor() {
    data_structures::linked_list::list L;
    L.insert(10);
    L.insert(20);
    L.insert(30);
    data_structures::linked_list::list otherList(L);
    otherList.insert(40);

    L.insert(400);

    assert(L.top() == 10);
    assert(otherList.top() == 10);
    assert(L.traverse(1) == 20);
    assert(otherList.traverse(1) == 20);

    assert(L.traverse(2) == 30);
    assert(otherList.traverse(2) == 30);

    assert(L.last() == 400);
    assert(otherList.last() == 40);
}

void test_assignment_operator() {
    data_structures::linked_list::list L;
    data_structures::linked_list::list otherList;
    L.insert(10);

Review comment (suggestion):
```suggestion
/**
 * @brief Self-tests by copying the constructor
 * @returns void
 */
static void test_copy_constructor() {
```


Code after:
    }
    delete_all_nodes();

    copy_all_nodes_from_list(other);
    return *this;
}

}  // namespace linked_list
}  // namespace data_structures

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test() {
    data_structures::linked_list::list L;
    // 1st test
    L.insert(11);
    L.insert(12);
    L.insert(15);
    L.insert(10);
    L.insert(-12);
    L.insert(-20);
    L.insert(18);
    assert(L.top() == 11);
    assert(L.last() == 18);
    L.reverseList();
    // Reversal Testing
    assert(L.top() == 18);
    assert(L.traverse(1) == -20);
    assert(L.traverse(2) == -12);
    assert(L.traverse(3) == 10);
    assert(L.traverse(4) == 15);
    assert(L.traverse(5) == 12);
    assert(L.last() == 11);
    std::cout << "All tests have successfully passed!" << std::endl;
}

void test_copy_constructor() {
    data_structures::linked_list::list L;
    L.insert(10);
    L.insert(20);
    L.insert(30);
    data_structures::linked_list::list otherList(L);
    otherList.insert(40);

    L.insert(400);

    assert(L.top() == 10);
    assert(otherList.top() == 10);
    assert(L.traverse(1) == 20);
Repository: TheAlgorithms/C-Plus-Plus
File: sorting/stooge_sort.cpp (C++)

Code before:
/**
 * @file
 * @brief [Stooge sort implementation](https://en.wikipedia.org/wiki/Stooge_sort)
 * in C++
 * @details
 * Stooge sort is a recursive sorting algorithm.
 * It divides the array into 3 parts and proceeds to:
 *	- sort first two thirds of the array
 *	- sort last two thirds of the array
 *  - sort first two thirds of the array
 * It's time complexity is O(n^(log3/log1.5)), which is about O(n^2.7), 
 * which makes it to be not the most efficient sorting algorithm 
 * on the street on average. Space complexity is O(1).
 */

#include <vector>
#include <iostream>

void stoogeSort(std::vector<int> *L, int i, int j) {
	if (i < j) {
		if ((*L)[i] > (*L)[j]) {	
			std::swap((*L)[i], (*L)[j]);
		}
		if (j - i > 1) {
			int third = (j - i + 1) / 3;
			stoogeSort(L, i, j - third);
			stoogeSort(L, i + third, j);
			stoogeSort(L, i, j - third);
		}
	}
	return;
}

int main() {
	std::vector<int> L = { 8, 9, 10, 4, 3, 5, 1 };
	stoogeSort(&L, 0, L.size() - 1);
	std::cout << "Sorted array:" << std::endl;
	for (int i = 0; i < L.size(); i++) {
		std::cout << L[i] << " ";
	}
	std::cout << std::endl;
	return 0;

Review comment (suggestion):
Please add a one-line description of what the library/header is for (**see the example below**).

```c++
#include <iostream>    /// for IO operations
#include <cassert>    /// for assert
```


Code after:
/**
 * @file
 * @brief [Stooge sort implementation](https://en.wikipedia.org/wiki/Stooge_sort)
 * in C++
 * @details
 * Stooge sort is a recursive sorting algorithm.
 * It divides the array into 3 parts and proceeds to:
 *	- sort first two thirds of the array
 *	- sort last two thirds of the array
 *  - sort first two thirds of the array
 * It's time complexity is O(n^(log3/log1.5)), which is about O(n^2.7),
 * which makes it to be not the most efficient sorting algorithm
 * on the street on average. Space complexity is O(1).
 */

#include <vector> /// for vector
#include <cassert> /// for assert
#include <algorithm>  /// for std::is_sorted
#include <iostream>   /// for IO operations

 /**
  * @brief The stoogeSort() function is used for sorting the array.
  * @param L - vector of values (int) to be sorted in in place (ascending order)
  * @param i - the first index of the array (0)
  * @param j - the last index of the array (L.size() - 1)
  * @returns void
  */
void stoogeSort(std::vector<int>* L, size_t i, size_t j) {
    if (i >= j) {
        return;
    }
    if ((*L)[i] > (*L)[j]) {
        std::swap((*L)[i], (*L)[j]);
    }
    if (j - i > 1) {
        size_t third = (j - i + 1) / 3;
        stoogeSort(L, i, j - third);
        stoogeSort(L, i + third, j);
        stoogeSort(L, i, j - third);
    }
}
Repository: TheAlgorithms/C-Plus-Plus
File: sorting/stooge_sort.cpp (C++)

Code before:

/**
 * The stoogeSort() function is used for sorting the array.
 *
 * @param L - vector of values (int) to be sorted in in place (ascending order)
 * @param i - the first index of the array (0)
 * @param j - the last index of the array (L.size() - 1)
 */
void stoogeSort(std::vector<int> *L, int i, int j) {
	if (i < j) {
		if ((*L)[i] > (*L)[j]) {	
			std::swap((*L)[i], (*L)[j]);
		}
		if (j - i > 1) {
			int third = (j - i + 1) / 3;
			stoogeSort(L, i, j - third);
			stoogeSort(L, i + third, j);
			stoogeSort(L, i, j - third);
		}
	}
	return;
}

/**
 * @brief Function to test sorting algorithm
 */
void test1() {
	std::vector<int> L = { 8, 9, 10, 4, 3, 5, 1 };
	stoogeSort(&L, 0, L.size() - 1);
	assert(std::is_sorted(std::begin(L), std::end(L)));
}

/**
 * @brief Function to test sorting algorithm, one element
 */
void test2() {
	std::vector<int> L = { -1};
	stoogeSort(&L, 0, L.size() - 1);
	assert(std::is_sorted(std::begin(L), std::end(L)));
}

/**
 * @brief Function to test sorting algorithm, repeating elements
 */
void test3() {
	std::vector<int> L = { 1, 2, 5, 4, 1, 5};
	stoogeSort(&L, 0, L.size() - 1);
	assert(std::is_sorted(std::begin(L), std::end(L)));
}

/**

Review comment (suggestion):
```suggestion
 * @brief Function to test sorting algorithm
 * @returns void
```


Code after:
#include <iostream>   /// for IO operations

 /**
  * @brief The stoogeSort() function is used for sorting the array.
  * @param L - vector of values (int) to be sorted in in place (ascending order)
  * @param i - the first index of the array (0)
  * @param j - the last index of the array (L.size() - 1)
  * @returns void
  */
void stoogeSort(std::vector<int>* L, size_t i, size_t j) {
    if (i >= j) {
        return;
    }
    if ((*L)[i] > (*L)[j]) {
        std::swap((*L)[i], (*L)[j]);
    }
    if (j - i > 1) {
        size_t third = (j - i + 1) / 3;
        stoogeSort(L, i, j - third);
        stoogeSort(L, i + third, j);
        stoogeSort(L, i, j - third);
    }
}

/**
 * @brief Function to test sorting algorithm
 * @returns void
 */
void test1() {
	std::vector<int> L = { 8, 9, 10, 4, 3, 5, 1 };
	stoogeSort(&L, 0, L.size() - 1);
	assert(std::is_sorted(std::begin(L), std::end(L)));
}

/**
 * @brief Function to test sorting algorithm, one element
 * @returns void
 */
void test2() {
	std::vector<int> L = { -1 };
	stoogeSort(&L, 0, L.size() - 1);
	assert(std::is_sorted(std::begin(L), std::end(L)));
}

/**
 * @brief Function to test sorting algorithm, repeating elements
 * @returns void
 */
void test3() {
	std::vector<int> L = { 1, 2, 5, 4, 1, 5 };
	stoogeSort(&L, 0, L.size() - 1);