barty/EvalRepair-Cpp huggingface dataset:
// Buggy implementation
/*
Filter given vector of any python values only for integers
>>> filter_integers({"a", 3.14, 5})
{5}
>>> filter_integers({1, 2, 3, "abc", {}, {}})
{1, 2, 3}
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<boost/any.hpp>
#include<list>
typedef std::list<boost::any> list_any;
using namespace std;
vector<int> filter_integers(list_any values){
    list_any::iterator it;
    boost::any anyone;
    vector<int> out;
    for (it=values.begin();it!=values.end();it++)
    {
         anyone=*it;
         out.push_back(boost::any_cast<int>(*it));
    }
    return out;
}



// Fixed implementation
/*
Filter given vector of any python values only for integers
>>> filter_integers({"a", 3.14, 5})
{5}
>>> filter_integers({1, 2, 3, "abc", {}, {}})
{1, 2, 3}
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<boost/any.hpp>
#include<list>
typedef std::list<boost::any> list_any;
using namespace std;
vector<int> filter_integers(list_any values){
    list_any::iterator it;
    boost::any anyone;
    vector<int> out;
    for (it=values.begin();it!=values.end();it++)
    {
         anyone=*it;
        if( anyone.type() == typeid(int) )
           out.push_back(boost::any_cast<int>(*it));
    }
    return out;
}
// Buggy implementation
/*
Given a string of words, return a vector of words split on whitespace, if no whitespaces exists in the text you
should split on commas ',' if no commas exists you should return a vector with one element, the number of lower-case letters with odd order in the
alphabet, ord("a") = 0, ord("b") = 1, ... ord("z") = 25
Examples
split_words("Hello world!") ➞ {"Hello", "world!"}
split_words("Hello,world!") ➞ {"Hello", "world!"}
split_words("abcdef") == {"3"} 
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
vector<string> split_words(string txt){
    int i;
    string current="";
    vector<string> out={};
    if (find(txt.begin(),txt.end(),' ')!=txt.end())
    {
        txt=txt+' ';
        for (i=0;i<txt.length();i++)
            if (txt[i]==' ') 
            {
                if (current.length()>0)out.push_back(current); 
                current="";
            }
            else current=current+txt[i];
        return out;
    }
    if (find(txt.begin(),txt.end(),',')!=txt.end())
    {
        txt=txt+',';
        for (i=0;i<txt.length();i++)
            if (txt[i]==',') 
            {
                if (current.length()>0)out.push_back(current); 
                current="";
            }
            else current=current+txt[i];
        return out;
    }
    int num=0;
    for (i=0;i<txt.length();i++)
        if (txt[i]>=65 and txt[i]<=90 and txt[i]%2==0)
            num+=1;
    return {to_string(num)};
}



// Fixed implementation
/*
Given a string of words, return a vector of words split on whitespace, if no whitespaces exists in the text you
should split on commas ',' if no commas exists you should return a vector with one element, the number of lower-case letters with odd order in the
alphabet, ord("a") = 0, ord("b") = 1, ... ord("z") = 25
Examples
split_words("Hello world!") ➞ {"Hello", "world!"}
split_words("Hello,world!") ➞ {"Hello", "world!"}
split_words("abcdef") == {"3"} 
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
vector<string> split_words(string txt){
    int i;
    string current="";
    vector<string> out={};
    if (find(txt.begin(),txt.end(),' ')!=txt.end())
    {
        txt=txt+' ';
        for (i=0;i<txt.length();i++)
            if (txt[i]==' ') 
            {
                if (current.length()>0)out.push_back(current); 
                current="";
            }
            else current=current+txt[i];
        return out;
    }
    if (find(txt.begin(),txt.end(),',')!=txt.end())
    {
        txt=txt+',';
        for (i=0;i<txt.length();i++)
            if (txt[i]==',') 
            {
                if (current.length()>0)out.push_back(current); 
                current="";
            }
            else current=current+txt[i];
        return out;
    }
    int num=0;
    for (i=0;i<txt.length();i++)
        if (txt[i]>=97 and txt[i]<=122 and txt[i]%2==0)
            num+=1;
    return {to_string(num)};
}
// Buggy implementation
/*
Given a string s and a natural number n, you have been tasked to implement 
a function that returns a vector of all words from string s that contain exactly 
n consonants, in order these words appear in the string s.
If the string s is empty then the function should return an empty vector.
Note: you may assume the input string contains only letters and spaces.
Examples:
select_words("Mary had a little lamb", 4) ==> {"little"}
select_words("Mary had a little lamb", 3) ==> {"Mary", "lamb"}
select_words('simple white space", 2) ==> {}
select_words("Hello world", 4) ==> {"world"}
select_words("Uncle sam", 3) ==> {"Uncle"}
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
vector<string> select_words(string s,int n){
    string vowels="AEIOU";
    string current="";
    vector<string> out={};
    int numc=0;
    s=s+' ';
    for (int i=0;i<s.length();i++)
        if (s[i]==' ')
        {
            if (numc==n) out.push_back(current);
            current="";
            numc=0;
        }
        else
        {
            current=current+s[i];
            if ((s[i]>=65 and s[i]<=90) or (s[i]>=97 and s[i]<=122))
            if (find(vowels.begin(),vowels.end(),s[i])==vowels.end())
                numc+=1;
        }
    return out;
}



// Fixed implementation
/*
Given a string s and a natural number n, you have been tasked to implement 
a function that returns a vector of all words from string s that contain exactly 
n consonants, in order these words appear in the string s.
If the string s is empty then the function should return an empty vector.
Note: you may assume the input string contains only letters and spaces.
Examples:
select_words("Mary had a little lamb", 4) ==> {"little"}
select_words("Mary had a little lamb", 3) ==> {"Mary", "lamb"}
select_words('simple white space", 2) ==> {}
select_words("Hello world", 4) ==> {"world"}
select_words("Uncle sam", 3) ==> {"Uncle"}
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
vector<string> select_words(string s,int n){
    string vowels="aeiouAEIOU";
    string current="";
    vector<string> out={};
    int numc=0;
    s=s+' ';
    for (int i=0;i<s.length();i++)
        if (s[i]==' ')
        {
            if (numc==n) out.push_back(current);
            current="";
            numc=0;
        }
        else
        {
            current=current+s[i];
            if ((s[i]>=65 and s[i]<=90) or (s[i]>=97 and s[i]<=122))
            if (find(vowels.begin(),vowels.end(),s[i])==vowels.end())
                numc+=1;
        }
    return out;
}
// Buggy implementation
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
double poly(vector<double> xs, double x){
    /* 
    Evaluates polynomial with coefficients xs at point x. return xs[0] + xs[1] * x + xs[1] * x^2 + .... xs[n] * x^n 
    */
    double sum=0;
    int i;
    for (i=0;i<xs.size();i++)
    {
        sum+=xs[i]*pow(x,i);
    }
    return sum;
}

double find_zero(vector<double> xs){
    /*
    xs are coefficients of a polynomial. find_zero find x such that poly(x) = 0. find_zero returns only only zero point, even if there are many. 
    Moreover, find_zero only takes list xs having even number of coefficients and largest non zero coefficient as it guarantees a solution.
    >>> round(find_zero([1, 2]), 2) #f(x) = 1 + 2x 
    -0.5 
    >>> round(find_zero([-6, 11, -6, 1]), 2) # (x - 1) * (x - 2) * (x - 3) = -6 + 11x - 6x^2 + x^3 
    1.0
    */
    double ans=0;
    double value;
    value=poly(xs,ans);
    while (abs(value)>1e-6)
    {
        double driv=0;
        for (int i=1;i<xs.size();i++)
        {
            driv+=xs[i]*pow(ans,i-1)*i;
        }
        ans=ans+value/driv;
        value=poly(xs,ans);
    }
    return ans;

}



// Fixed implementation
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
double poly(vector<double> xs, double x){
    /* 
    Evaluates polynomial with coefficients xs at point x. return xs[0] + xs[1] * x + xs[1] * x^2 + .... xs[n] * x^n 
    */
    double sum=0;
    int i;
    for (i=0;i<xs.size();i++)
    {
        sum+=xs[i]*pow(x,i);
    }
    return sum;
}

double find_zero(vector<double> xs){
    /*
    xs are coefficients of a polynomial. find_zero find x such that poly(x) = 0. find_zero returns only only zero point, even if there are many. 
    Moreover, find_zero only takes list xs having even number of coefficients and largest non zero coefficient as it guarantees a solution.
    >>> round(find_zero([1, 2]), 2) #f(x) = 1 + 2x 
    -0.5 
    >>> round(find_zero([-6, 11, -6, 1]), 2) # (x - 1) * (x - 2) * (x - 3) = -6 + 11x - 6x^2 + x^3 
    1.0
    */
    double ans=0;
    double value;
    value=poly(xs,ans);
    while (abs(value)>1e-6)
    {
        double driv=0;
        for (int i=1;i<xs.size();i++)
        {
            driv+=xs[i]*pow(ans,i-1)*i;
        }
        ans=ans-value/driv;
        value=poly(xs,ans);
    }
    return ans;

}
// Buggy implementation
/*
You are given a positive integer n. You have to create an integer vector a of length n.
    For each i (1 ≤ i ≤ n), the value of a{i} = i * i - i + 1.
    Return the number of triples (a{i}, a{j}, a{k}) of a where i < j < k, 
and a[i] + a[j] + a[k] is a multiple of 3.

Example :
    Input: n = 5
    Output: 1
    Explanation: 
    a = {1, 3, 7, 13, 21}
    The only valid triple is (1, 7, 13).
*/
#include<stdio.h>
#include<vector>
using namespace std;
int get_matrix_triples(int n){
    vector<int> a;
    vector<vector<int>> sum={{0,0,0}};
    vector<vector<int>> sum2={{0,0,0}};
    for (int i=1;i<=n;i++)
    {
        a.push_back((i*i-i+1)%3);
        sum.push_back(sum[sum.size()-1]);
        sum[i][a[i-1]]+=1;
    }
    for (int times=1;times<3;times++)
    {
    for (int i=1;i<=n;i++)
    {
        sum2.push_back(sum2[sum2.size()-1]);
        if (i>=1)
        for (int j=1;j<=3;j++)
            sum2[i][(a[i-1]+j)%3]+=sum[i-1][j];
    }
    sum=sum2;
    sum2={{0,0,0}};
    }

    return sum[n][0];
}



// Fixed implementation
/*
You are given a positive integer n. You have to create an integer vector a of length n.
    For each i (1 ≤ i ≤ n), the value of a{i} = i * i - i + 1.
    Return the number of triples (a{i}, a{j}, a{k}) of a where i < j < k, 
and a[i] + a[j] + a[k] is a multiple of 3.

Example :
    Input: n = 5
    Output: 1
    Explanation: 
    a = {1, 3, 7, 13, 21}
    The only valid triple is (1, 7, 13).
*/
#include<stdio.h>
#include<vector>
using namespace std;
int get_matrix_triples(int n){
    vector<int> a;
    vector<vector<int>> sum={{0,0,0}};
    vector<vector<int>> sum2={{0,0,0}};
    for (int i=1;i<=n;i++)
    {
        a.push_back((i*i-i+1)%3);
        sum.push_back(sum[sum.size()-1]);
        sum[i][a[i-1]]+=1;
    }
    for (int times=1;times<3;times++)
    {
    for (int i=1;i<=n;i++)
    {
        sum2.push_back(sum2[sum2.size()-1]);
        if (i>=1)
        for (int j=0;j<=2;j++)
            sum2[i][(a[i-1]+j)%3]+=sum[i-1][j];
    }
    sum=sum2;
    sum2={{0,0,0}};
    }

    return sum[n][0];
}
// Buggy implementation
/*
Given two positive integers a and b, return the even digits between a
and b, in ascending order.

For example:
generate_integers(2, 8) => {2, 4, 6, 8}
generate_integers(8, 2) => {2, 4, 6, 8}
generate_integers(10, 14) => {}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> generate_integers(int a,int b){
    int m;
    vector<int> out={};
    for (int i=a;i<=b;i++)
    if (i<10 and i%2==0) out.push_back(i);
    return out;
}



// Fixed implementation
/*
Given two positive integers a and b, return the even digits between a
and b, in ascending order.

For example:
generate_integers(2, 8) => {2, 4, 6, 8}
generate_integers(8, 2) => {2, 4, 6, 8}
generate_integers(10, 14) => {}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> generate_integers(int a,int b){
    int m;
    if (b<a)
    {
        m=a;a=b;b=m;
    }

    vector<int> out={};
    for (int i=a;i<=b;i++)
    if (i<10 and i%2==0) out.push_back(i);
    return out;
}
// Buggy implementation
/*
Complete the function that takes two integers and returns 
the product of their unit digits.
Assume the input is always valid.
Examples:
multiply(148, 412) should return 16.
multiply(19, 28) should return 72.
multiply(2020, 1851) should return 0.
multiply(14,-15) should return 20.
*/
#include<stdio.h>
#include<math.h>
using namespace std;
int multiply(int a,int b){
    return (a%10)*(b%10);
}



// Fixed implementation
/*
Complete the function that takes two integers and returns 
the product of their unit digits.
Assume the input is always valid.
Examples:
multiply(148, 412) should return 16.
multiply(19, 28) should return 72.
multiply(2020, 1851) should return 0.
multiply(14,-15) should return 20.
*/
#include<stdio.h>
#include<math.h>
using namespace std;
int multiply(int a,int b){
    return (abs(a)%10)*(abs(b)%10);
}
// Buggy implementation
/*
We have a vector "arr" of N integers arr[1], arr[2], ..., arr[N].The
numbers in the vector will be randomly ordered. Your task is to determine if
it is possible to get a vector sorted in non-decreasing order by performing 
the following operation on the given vector:
    You are allowed to perform right shift operation any number of times.

One right shift operation means shifting all elements of the vector by one
position in the right direction. The last element of the vector will be moved to
the starting position in the vector i.e. 0th index. 

If it is possible to obtain the sorted vector by performing the above operation
then return true else return false.
If the given vector is empty then return true.

Note: The given vector is guaranteed to have unique elements.

For Example:

move_one_ball({3, 4, 5, 1, 2})==>true
Explanation: By performing 2 right shift operations, non-decreasing order can
             be achieved for the given vector.
move_one_ball({3, 5, 4, 1, 2})==>false
Explanation:It is not possible to get non-decreasing order for the given
            vector by performing any number of right shift operations.
            
*/
#include<stdio.h>
#include<vector>
using namespace std;
bool move_one_ball(vector<int> arr){
    int num=0;
    if (arr.size()==0) return true;
    for (int i=1;i<arr.size();i++)
        if (arr[i]<arr[i-1]) num+=1;
    if (arr[arr.size()-1]<arr[0]) num+=1;
    if (num<2) return true;
    return false;
}



// Fixed implementation
/*
We have a vector "arr" of N integers arr[1], arr[2], ..., arr[N].The
numbers in the vector will be randomly ordered. Your task is to determine if
it is possible to get a vector sorted in non-decreasing order by performing 
the following operation on the given vector:
    You are allowed to perform right shift operation any number of times.

One right shift operation means shifting all elements of the vector by one
position in the right direction. The last element of the vector will be moved to
the starting position in the vector i.e. 0th index. 

If it is possible to obtain the sorted vector by performing the above operation
then return true else return false.
If the given vector is empty then return true.

Note: The given vector is guaranteed to have unique elements.

For Example:

move_one_ball({3, 4, 5, 1, 2})==>true
Explanation: By performing 2 right shift operations, non-decreasing order can
             be achieved for the given vector.
move_one_ball({3, 5, 4, 1, 2})==>false
Explanation:It is not possible to get non-decreasing order for the given
            vector by performing any number of right shift operations.
            
*/
#include<stdio.h>
#include<vector>
using namespace std;
bool move_one_ball(vector<int> arr){
    int num=0;
    if (arr.size()==0) return true;
    for (int i=1;i<arr.size();i++)
        if (arr[i]<arr[i-1]) num+=1;
    if (arr[arr.size()-1]>arr[0]) num+=1;
    if (num<2) return true;
    return false;
}
// Buggy implementation
/*
Return 2^n modulo p (be aware of numerics).
>>> modp(3, 5)
3
>>> modp(1101, 101)
2
>>> modp(0, 101)
1
>>> modp(3, 11)
8
>>> modp(100, 101)
1
*/
#include<stdio.h>
using namespace std;
int modp(int n,int p){
    int out=1;
    for (int i=0;i<n;i++)
        out=out*2;
    return out%p;
}



// Fixed implementation
/*
Return 2^n modulo p (be aware of numerics).
>>> modp(3, 5)
3
>>> modp(1101, 101)
2
>>> modp(0, 101)
1
>>> modp(3, 11)
8
>>> modp(100, 101)
1
*/
#include<stdio.h>
using namespace std;
int modp(int n,int p){
    int out=1;
    for (int i=0;i<n;i++)
        out=(out*2)%p;
    return out%p;
}
// Buggy implementation
/*
Return vector of prime factors of given integer in the order from smallest to largest.
Each of the factors should be vectored number of times corresponding to how many times it appeares in factorization.
Input number should be equal to the product of all factors
>>> factorize(8)
{2, 2, 2}
>>> factorize(25)
{5, 5}
>>> factorize(70)
{2, 5, 7}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> factorize(int n){
    vector<int> out={};
    for (int i=2;i*i<=n;i++)
        if (n%i==0)
        {
            n=n/i;
            out.push_back(i);
            i-=1;
        }
    return out;
}



// Fixed implementation
/*
Return vector of prime factors of given integer in the order from smallest to largest.
Each of the factors should be vectored number of times corresponding to how many times it appeares in factorization.
Input number should be equal to the product of all factors
>>> factorize(8)
{2, 2, 2}
>>> factorize(25)
{5, 5}
>>> factorize(70)
{2, 5, 7}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> factorize(int n){
    vector<int> out={};
    for (int i=2;i*i<=n;i++)
        if (n%i==0)
        {
            n=n/i;
            out.push_back(i);
            i-=1;
        }
    out.push_back(n);
    return out;
}
// Buggy implementation
/*
Given a positive integer n, return a sorted vector that has the odd numbers in collatz sequence.

The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined
as follows: start with any positive integer n. Then each term is obtained from the 
previous term as follows: if the previous term is even, the next term is one half of 
the previous term. If the previous term is odd, the next term is 3 times the previous
term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1.

Note: 
    1. Collatz(1) is {1}.
    2. returned vector sorted in increasing order.

For example:
get_odd_collatz(5) returns {1, 5} // The collatz sequence for 5 is {5, 16, 8, 4, 2, 1}, so the odd numbers are only 1, and 5.
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> get_odd_collatz(int n){
    vector<int> out={1};
    while (n>=1)
    {
        if (n%2==0) {out.push_back(n); n=n*3+1;}
        else n=n/2;
    }
    sort(out.begin(),out.end());
    return out;
}



// Fixed implementation
/*
Given a positive integer n, return a sorted vector that has the odd numbers in collatz sequence.

The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined
as follows: start with any positive integer n. Then each term is obtained from the 
previous term as follows: if the previous term is even, the next term is one half of 
the previous term. If the previous term is odd, the next term is 3 times the previous
term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1.

Note: 
    1. Collatz(1) is {1}.
    2. returned vector sorted in increasing order.

For example:
get_odd_collatz(5) returns {1, 5} // The collatz sequence for 5 is {5, 16, 8, 4, 2, 1}, so the odd numbers are only 1, and 5.
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> get_odd_collatz(int n){
    vector<int> out={1};
    while (n!=1)
    {
        if (n%2==1) {out.push_back(n); n=n*3+1;}
        else n=n/2;
    }
    sort(out.begin(),out.end());
    return out;
}
// Buggy implementation
/*
A simple program which should return the value of x if n is 
a prime number and should return the value of y otherwise.

Examples:
for x_or_y(7, 34, 12) == 34
for x_or_y(15, 8, 5) == 5

*/
#include<stdio.h>
using namespace std;
int x_or_y(int n,int x,int y){
    bool isp=true;
    if (n<2) isp=false;
    for (int i=2;i*i<=n;i++)
    if (n%i==0) isp=false;
    if (isp) return y;
    return x;
}



// Fixed implementation
/*
A simple program which should return the value of x if n is 
a prime number and should return the value of y otherwise.

Examples:
for x_or_y(7, 34, 12) == 34
for x_or_y(15, 8, 5) == 5

*/
#include<stdio.h>
using namespace std;
int x_or_y(int n,int x,int y){
    bool isp=true;
    if (n<2) isp=false;
    for (int i=2;i*i<=n;i++)
    if (n%i==0) isp=false;
    if (isp) return x;
    return y;
}
// Buggy implementation
/*
prime_fib returns n-th number that is a Fibonacci number and it's also prime.
>>> prime_fib(1)
2
>>> prime_fib(2)
3
>>> prime_fib(3)
5
>>> prime_fib(4)
13
>>> prime_fib(5)
89
*/
#include<stdio.h>
using namespace std;
int prime_fib(int n){
    int f1,f2,m;
    f1=1;f2=2;
    int count=0;
    while (count<n)
    {
        f1=f1+f2;
        m=f1;f1=f2;f2=m;
        bool isprime=true;
        for (int w=2;w*w<=f2;w++)
            if (f2%w==0)
            {
             isprime=false; break;
            }
        if (isprime) count+=1;
        if (count==n) return f1;
    }

}


// Fixed implementation
/*
prime_fib returns n-th number that is a Fibonacci number and it's also prime.
>>> prime_fib(1)
2
>>> prime_fib(2)
3
>>> prime_fib(3)
5
>>> prime_fib(4)
13
>>> prime_fib(5)
89
*/
#include<stdio.h>
using namespace std;
int prime_fib(int n){
    int f1,f2,m;
    f1=1;f2=2;
    int count=0;
    while (count<n)
    {
        f1=f1+f2;
        m=f1;f1=f2;f2=m;
        bool isprime=true;
        for (int w=2;w*w<=f1;w++)
            if (f1%w==0)
            {
             isprime=false; break;
            }
        if (isprime) count+=1;
        if (count==n) return f1;
    }

}