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

Code before:
* @author [Neeraj Cherkara](https://github.com/iamnambiar)
*/
#include <cassert> /// for assert
#include <cmath> /// for std::pow
#include <iostream> /// for IO operations

/**
 * @brief Function to calculate the total number of digits in the number.
 * @param num Number
 * @return Total number of digits.
 */
int number_of_digits(int num) {
    int total_digits = 0;
    while (num > 0) {
        num = num / 10;
        ++total_digits;
    }
    return total_digits;
}

/**
* @brief Function to check whether the number is armstrong number or not.
* @param number to be checked
* @return `true` if the number is armstrong.
* @return `false` if the number is not armstrong.
*/
bool is_armstrong(int number) {
    // If the number is less than 0, then it is not an armstrong number.
    if (number < 0) {
        return false;
    }

    int sum = 0;
    int temp = number;
    // Finding the total number of digits in the number
    int total_digits = number_of_digits(number);
    while (temp > 0) {
        int rem = temp % 10;
        // Finding each digit raised to the power total digit and add it to the
        // total sum
        sum += static_cast<int>(std::pow(rem, total_digits));
        temp = temp / 10;
    }
    return number == sum;
}

/**
* @brief Self-test implementations
* @returns void
*/
static void test() {

Review comment (suggestion):
There should be spaces here (one of the last missing details).
```suggestion
 * @brief Function to check whether the number is armstrong number or not.
 * @param number to be checked
 * @return `true` if the number is armstrong.
 * @return `false` if the number is not armstrong.
 */
```


Code after:
* @author [Neeraj Cherkara](https://github.com/iamnambiar)
*/
#include <cassert> /// for assert
#include <cmath> /// for std::pow
#include <iostream> /// for IO operations

/**
 * @brief Function to calculate the total number of digits in the number.
 * @param num Number
 * @return Total number of digits.
 */
int number_of_digits(int num) {
    int total_digits = 0;
    while (num > 0) {
        num = num / 10;
        ++total_digits;
    }
    return total_digits;
}

/**
 * @brief Function to check whether the number is armstrong number or not.
 * @param number to be checked
 * @return `true` if the number is armstrong.
 * @return `false` if the number is not armstrong.
 */
bool is_armstrong(int number) {
    // If the number is less than 0, then it is not an armstrong number.
    if (number < 0) {
        return false;
    }

    int sum = 0;
    int temp = number;
    // Finding the total number of digits in the number
    int total_digits = number_of_digits(number);
    while (temp > 0) {
        int rem = temp % 10;
        // Finding each digit raised to the power total digit and add it to the
        // total sum
        sum += static_cast<int>(std::pow(rem, total_digits));
        temp = temp / 10;
    }
    return number == sum;
}

/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
Repository: TheAlgorithms/C-Plus-Plus
File: math/armstrong_number.cpp (C++)

Code before:
        // total sum
        sum += static_cast<int>(std::pow(rem, total_digits));
        temp = temp / 10;
    }
    return number == sum;
}

/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
    // is_armstrong(370) returns true.
    assert(is_armstrong(370) == true);
    // is_armstrong(225) returns false.
    assert(is_armstrong(225) == false);
    // is_armstrong(-23) returns false.
    assert(is_armstrong(-23) == false);
    // is_armstrong(153) returns true.
    assert(is_armstrong(153) == true);
    // is_armstrong(0) returns true.
    assert(is_armstrong(0) == true);
    // is_armstrong(12) returns false.
    assert(is_armstrong(12) == false);

    std::cout << "all tests have passed\n";
}

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


Review comment (suggestion):
```suggestion
    std::cout << "All tests have successfully passed!\n";
```


Code after:
        // total sum
        sum += static_cast<int>(std::pow(rem, total_digits));
        temp = temp / 10;
    }
    return number == sum;
}

/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
    // is_armstrong(370) returns true.
    assert(is_armstrong(370) == true);
    // is_armstrong(225) returns false.
    assert(is_armstrong(225) == false);
    // is_armstrong(-23) returns false.
    assert(is_armstrong(-23) == false);
    // is_armstrong(153) returns true.
    assert(is_armstrong(153) == true);
    // is_armstrong(0) returns true.
    assert(is_armstrong(0) == true);
    // is_armstrong(12) returns false.
    assert(is_armstrong(12) == false);

    std::cout << "All tests have successfully passed!\n";
}

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

Code before:
            return key[x];
        }
        return _getKth(childs[x][1], k);
    }
    /**
     * @brief Query the rank of specified element (Also internal method)
     * @param x Query the subtree of node x (Usually x=root)
     * @param k The queried element
     * @return The rank of element k
     */
    int _getRank(int x, int k) {
        if (!x)
            return 0;
        if (k == key[x])
            return size[childs[x][0]] + 1;
        else if (k < key[x])
            return _getRank(childs[x][0], k);
        else
            return size[childs[x][0]] + cnt[x] + _getRank(childs[x][1], k);
    }
    /**
     * @brief Get the predecessor node of element k
     * @param k The queried element
     * @return The predecessor
     */
    int getPre(int k) {
        int x = root, pre;
        while (x) {
            if (key[x] < k) {
                pre = key[x], x = childs[x][1];
            } else
                x = childs[x][0];
        }
        return pre;
    }
    /**
     * @brief Get the successor node of element k
     * @param k The queried element
     * @return The successor
     */
    int getNxt(int k) {
        int x = root, nxt;
        while (x) {
            if (key[x] > k) {
                nxt = key[x], x = childs[x][0];
            } else
                x = childs[x][1];
        }
        return nxt;
    }
    /**

Review comment (suggestion):
I think the function names should be fully typed out so there is no confusion.
the standards requires snake case for function names
 ```suggestion
    int get_predecessor(int k) {
```

Code after:
     * @param x Query the subtree of node x (Usually x=root)
     * @param k The queried rank
     * @return The element ranked number k
     */
    int _get_k_th(int &x, int k) {
        if (k <= size[childs[x][0]]) {
            return _get_k_th(childs[x][0], k);
        }
        k -= size[childs[x][0]] + cnt[x];
        if (k <= 0) {
            return key[x];
        }
        return _get_k_th(childs[x][1], k);
    }
    /**
     * @brief Query the rank of specified element (internal method)
     * @param x Query the subtree of node x (Usually x=root)
     * @param k The queried element
     * @return The rank of element k
     */
    int _get_rank(int x, int k) {
        if (!x) {
            return 0;
        }
        if (k == key[x]) {
            return size[childs[x][0]] + 1;
        }
        else if (k < key[x]) {
            return _get_rank(childs[x][0], k);
        }
        else {
            return size[childs[x][0]] + cnt[x] + _get_rank(childs[x][1], k);
        }
    }
    /**
     * @brief Get the predecessor node of element k
     * @param k The queried element
     * @return The predecessor
     */
    int get_predecessor(int k) {
        int x = root, pre = -1;
        while (x) {
            if (key[x] < k) {
                pre = key[x], x = childs[x][1];
            } else {
                x = childs[x][0];
            }
        }
        return pre;
    }
    /**
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/treap.cpp (C++)

Code before:
    }
    /**
     * @brief Get the predecessor node of element k
     * @param k The queried element
     * @return The predecessor
     */
    int getPre(int k) {
        int x = root, pre;
        while (x) {
            if (key[x] < k) {
                pre = key[x], x = childs[x][1];
            } else
                x = childs[x][0];
        }
        return pre;
    }
    /**
     * @brief Get the successor node of element k
     * @param k The queried element
     * @return The successor
     */
    int getNxt(int k) {
        int x = root, nxt;
        while (x) {
            if (key[x] > k) {
                nxt = key[x], x = childs[x][0];
            } else
                x = childs[x][1];
        }
        return nxt;
    }
    /**
     * @brief External methods to set opertion object to be the whole tree
     */
    void insert(int k) { _insert(root, k); }
    void erase(int k) { _erase(root, k); }
    int getKth(int k) { return _getKth(root, k); }
    int getRank(int k) { return _getRank(root, k); }
} mTreap;

// The solution to luogu P3369
// Problem Link: https://www.luogu.com.cn/problem/P3369
void solve() {
    int n;
    std::cin >> n;
    int op, x;
    while (std::cin >> op >> x) {
        switch (op) {
            case 1:
                mTreap.insert(x);
                break;

Review comment (suggestion):
```suggestion
                next = key[x], x = childs[x][0];
```

Code after:
     */
    int _get_rank(int x, int k) {
        if (!x) {
            return 0;
        }
        if (k == key[x]) {
            return size[childs[x][0]] + 1;
        }
        else if (k < key[x]) {
            return _get_rank(childs[x][0], k);
        }
        else {
            return size[childs[x][0]] + cnt[x] + _get_rank(childs[x][1], k);
        }
    }
    /**
     * @brief Get the predecessor node of element k
     * @param k The queried element
     * @return The predecessor
     */
    int get_predecessor(int k) {
        int x = root, pre = -1;
        while (x) {
            if (key[x] < k) {
                pre = key[x], x = childs[x][1];
            } else {
                x = childs[x][0];
            }
        }
        return pre;
    }
    /**
     * @brief Get the successor node of element k
     * @param k The queried element
     * @return The successor
     */
    int get_next(int k) {
        int x = root, next = -1;
        while (x) {
            if (key[x] > k) {
                next = key[x], x = childs[x][0];
            } else {
                x = childs[x][1];
            }
        }
        return next;
    }
    /**
     * @brief Insert element (External method)
     * @param k Key to insert
     */
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/treap.cpp (C++)

Code before:
            } else
                x = childs[x][0];
        }
        return pre;
    }
    /**
     * @brief Get the successor node of element k
     * @param k The queried element
     * @return The successor
     */
    int getNxt(int k) {
        int x = root, nxt;
        while (x) {
            if (key[x] > k) {
                nxt = key[x], x = childs[x][0];
            } else
                x = childs[x][1];
        }
        return nxt;
    }
    /**
     * @brief External methods to set opertion object to be the whole tree
     */
    void insert(int k) { _insert(root, k); }
    void erase(int k) { _erase(root, k); }
    int getKth(int k) { return _getKth(root, k); }
    int getRank(int k) { return _getRank(root, k); }
} mTreap;

// The solution to luogu P3369
// Problem Link: https://www.luogu.com.cn/problem/P3369
void solve() {
    int n;
    std::cin >> n;
    int op, x;
    while (std::cin >> op >> x) {
        switch (op) {
            case 1:
                mTreap.insert(x);
                break;
            case 2:
                mTreap.erase(x);
                break;
            case 3:
                std::cout << mTreap.getRank(x) << '\n';
                break;
            case 4:
                std::cout << mTreap.getKth(x) << '\n';
                break;
            case 5:
                std::cout << mTreap.getPre(x) << '\n';

Review comment (suggestion):
```suggestion
    int get_k_th(int k) { return _get_k_th(root, k); }
```
personal preference feel free to come up with a better and more descriptive function name 

Code after:
        else {
            return size[childs[x][0]] + cnt[x] + _get_rank(childs[x][1], k);
        }
    }
    /**
     * @brief Get the predecessor node of element k
     * @param k The queried element
     * @return The predecessor
     */
    int get_predecessor(int k) {
        int x = root, pre = -1;
        while (x) {
            if (key[x] < k) {
                pre = key[x], x = childs[x][1];
            } else {
                x = childs[x][0];
            }
        }
        return pre;
    }
    /**
     * @brief Get the successor node of element k
     * @param k The queried element
     * @return The successor
     */
    int get_next(int k) {
        int x = root, next = -1;
        while (x) {
            if (key[x] > k) {
                next = key[x], x = childs[x][0];
            } else {
                x = childs[x][1];
            }
        }
        return next;
    }
    /**
     * @brief Insert element (External method)
     * @param k Key to insert
     */
    void insert(int k) { _insert(root, k); }
    /**
     * @brief Erase element (External method)
     * @param k Key to erase
     */
    void erase(int k) { _erase(root, k); }
    /**
     * @brief Get the KTH largest value (External method)
     * @param k The queried rank
     * @return The element ranked number x
     */
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/treap.cpp (C++)

Code before:
                x = childs[x][0];
        }
        return pre;
    }
    /**
     * @brief Get the successor node of element k
     * @param k The queried element
     * @return The successor
     */
    int getNxt(int k) {
        int x = root, nxt;
        while (x) {
            if (key[x] > k) {
                nxt = key[x], x = childs[x][0];
            } else
                x = childs[x][1];
        }
        return nxt;
    }
    /**
     * @brief External methods to set opertion object to be the whole tree
     */
    void insert(int k) { _insert(root, k); }
    void erase(int k) { _erase(root, k); }
    int getKth(int k) { return _getKth(root, k); }
    int getRank(int k) { return _getRank(root, k); }
} mTreap;

// The solution to luogu P3369
// Problem Link: https://www.luogu.com.cn/problem/P3369
void solve() {
    int n;
    std::cin >> n;
    int op, x;
    while (std::cin >> op >> x) {
        switch (op) {
            case 1:
                mTreap.insert(x);
                break;
            case 2:
                mTreap.erase(x);
                break;
            case 3:
                std::cout << mTreap.getRank(x) << '\n';
                break;
            case 4:
                std::cout << mTreap.getKth(x) << '\n';
                break;
            case 5:
                std::cout << mTreap.getPre(x) << '\n';
                break;

Review comment (suggestion):
```suggestion
    int get_rank(int k) { return _get_rank(root, k); }
```

Code after:
            return size[childs[x][0]] + cnt[x] + _get_rank(childs[x][1], k);
        }
    }
    /**
     * @brief Get the predecessor node of element k
     * @param k The queried element
     * @return The predecessor
     */
    int get_predecessor(int k) {
        int x = root, pre = -1;
        while (x) {
            if (key[x] < k) {
                pre = key[x], x = childs[x][1];
            } else {
                x = childs[x][0];
            }
        }
        return pre;
    }
    /**
     * @brief Get the successor node of element k
     * @param k The queried element
     * @return The successor
     */
    int get_next(int k) {
        int x = root, next = -1;
        while (x) {
            if (key[x] > k) {
                next = key[x], x = childs[x][0];
            } else {
                x = childs[x][1];
            }
        }
        return next;
    }
    /**
     * @brief Insert element (External method)
     * @param k Key to insert
     */
    void insert(int k) { _insert(root, k); }
    /**
     * @brief Erase element (External method)
     * @param k Key to erase
     */
    void erase(int k) { _erase(root, k); }
    /**
     * @brief Get the KTH largest value (External method)
     * @param k The queried rank
     * @return The element ranked number x
     */
    int get_k_th(int k) { return _get_k_th(root, k); }
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/treap.cpp (C++)

Code before:
     * @param k The queried rank
     * @return The element ranked x
     */
    int _getKth(int &x, int k) {
        if (k <= size[childs[x][0]]) {
            return _getKth(childs[x][0], k);
        }
        k -= size[childs[x][0]] + cnt[x];
        if (k <= 0) {
            return key[x];
        }
        return _getKth(childs[x][1], k);
    }
    /**
     * @brief Query the rank of specified element (Also internal method)
     * @param x Query the subtree of node x (Usually x=root)
     * @param k The queried element
     * @return The rank of element k
     */
    int _getRank(int x, int k) {
        if (!x)
            return 0;
        if (k == key[x])
            return size[childs[x][0]] + 1;
        else if (k < key[x])
            return _getRank(childs[x][0], k);
        else
            return size[childs[x][0]] + cnt[x] + _getRank(childs[x][1], k);
    }
    /**
     * @brief Get the predecessor node of element k
     * @param k The queried element
     * @return The predecessor
     */
    int getPre(int k) {
        int x = root, pre;
        while (x) {
            if (key[x] < k) {
                pre = key[x], x = childs[x][1];
            } else
                x = childs[x][0];
        }
        return pre;
    }
    /**
     * @brief Get the successor node of element k
     * @param k The queried element
     * @return The successor
     */
    int getNxt(int k) {
        int x = root, nxt;

Review comment (suggestion):
```suggestion
            return _get_rank(childs[x][0], k);
```

Code after:
                _erase(x, k);
            }
        } else {  // Find the target value based on BST properties
            _erase(childs[x][key[x] < k], k);
        }
        update(x);
    }
    /**
     * @brief Find the KTH largest value (internal method)
     * @param x Query the subtree of node x (Usually x=root)
     * @param k The queried rank
     * @return The element ranked number k
     */
    int _get_k_th(int &x, int k) {
        if (k <= size[childs[x][0]]) {
            return _get_k_th(childs[x][0], k);
        }
        k -= size[childs[x][0]] + cnt[x];
        if (k <= 0) {
            return key[x];
        }
        return _get_k_th(childs[x][1], k);
    }
    /**
     * @brief Query the rank of specified element (internal method)
     * @param x Query the subtree of node x (Usually x=root)
     * @param k The queried element
     * @return The rank of element k
     */
    int _get_rank(int x, int k) {
        if (!x) {
            return 0;
        }
        if (k == key[x]) {
            return size[childs[x][0]] + 1;
        }
        else if (k < key[x]) {
            return _get_rank(childs[x][0], k);
        }
        else {
            return size[childs[x][0]] + cnt[x] + _get_rank(childs[x][1], k);
        }
    }
    /**
     * @brief Get the predecessor node of element k
     * @param k The queried element
     * @return The predecessor
     */
    int get_predecessor(int k) {
        int x = root, pre = -1;
        while (x) {
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/treap.cpp (C++)

Code before:
     * @brief Get the rank of specified element (External method)
     * @param k The queried element
     * @return The rank of element k
     */
    int get_rank(int k) { return _get_rank(root, k); }
} mTreap;
/**
 * @brief Main function
 * @returns 0 on exit
 */
int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);

    mTreap.insert(106465);
    assert(mTreap.get_k_th(1) == 106465);
    mTreap.insert(317721);
    mTreap.insert(460929);
    mTreap.insert(644985);
    mTreap.insert(84185);
    mTreap.insert(89851);
    assert(mTreap.get_next(81968) == 84185);
    mTreap.insert(492737);
    assert(mTreap.get_predecessor(493598) == 492737);
}


Review comment (suggestion):
```suggestion
/**
 * @brief Self-test implementations
 * @returns void
 */
 static void test() 
    mTreap.insert(106465);
    assert(mTreap.get_k_th(1) == 106465);
    mTreap.insert(317721);
    mTreap.insert(460929);
    mTreap.insert(644985);
    mTreap.insert(84185);
    mTreap.insert(89851);
    assert(mTreap.get_next(81968) == 84185);
    mTreap.insert(492737);
    assert(mTreap.get_predecessor(493598) == 492737);{
 }
/**
 * @brief Main function
 * @returns 0 on exit
 */
int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
    test(); // run self-test implementations
    return 0;
}
```
tests must always be separate from the main function

Code after:
        return next;
    }
    /**
     * @brief Insert element (External method)
     * @param k Key to insert
     */
    void insert(int k) { _insert(root, k); }
    /**
     * @brief Erase element (External method)
     * @param k Key to erase
     */
    void erase(int k) { _erase(root, k); }
    /**
     * @brief Get the KTH largest value (External method)
     * @param k The queried rank
     * @return The element ranked number x
     */
    int get_k_th(int k) { return _get_k_th(root, k); }
    /**
     * @brief Get the rank of specified element (External method)
     * @param k The queried element
     * @return The rank of element k
     */
    int get_rank(int k) { return _get_rank(root, k); }
};
}  // namespace treap
}  // namespace data_structures

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test() {
    data_structures::treap::Treap mTreap;  ///< Treap object instance

    mTreap.insert(1);
    mTreap.insert(2);
    mTreap.insert(3);
    assert(mTreap.get_k_th(2) == 2);
    mTreap.insert(4);
    mTreap.insert(5);
    mTreap.insert(6);
    assert(mTreap.get_next(4) == 5);
    mTreap.insert(7);
    assert(mTreap.get_predecessor(7) == 6);
    mTreap.erase(4);
    assert(mTreap.get_k_th(4) == 5);
    assert(mTreap.get_rank(5) == 4);
    mTreap.insert(10);
    assert(mTreap.get_rank(10) == 7);
    assert(mTreap.get_predecessor(10) == 7);