instance_id
stringlengths 13
15
| number_spans
int64 1
3
| prompt
stringlengths 133
1.35k
| declaration
stringlengths 111
515
| splits
listlengths 2
4
| removed_spans
listlengths 1
3
| canonical_solution
stringlengths 18
1.4k
| test
stringlengths 148
1.76k
|
|---|---|---|---|---|---|---|---|
CPP/139_spans_2
| 2
|
/*
The Brazilian factorial is defined as:
brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1!
where n > 0
For example:
>>> special_factorial(4)
288
The function will receive an integer as input and should return the special
factorial of this integer.
*/
#include<stdio.h>
using namespace std;
long long special_factorial(int n){
|
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
#include<stdlib.h>
long long special_factorial(int n){
|
[
" long long fact=1,bfact=1;",
"t;\n ",
"\n}\n"
] |
[
"\n for (int i=1;i<=n;i++)\n {\n fact=fact*i;\n bfact=bfact*fac",
" }\n return bfact;"
] |
long long fact=1,bfact=1;
for (int i=1;i<=n;i++)
{
fact=fact*i;
bfact=bfact*fact;
}
return bfact;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (special_factorial(4) == 288);
assert (special_factorial(5) == 34560);
assert (special_factorial(7) == 125411328000);
assert (special_factorial(1) == 1);
}
|
CPP/140_spans_2
| 2
|
/*
Given a string text, replace all spaces in it with underscores,
and if a string has more than 2 consecutive spaces,
then replace all consecutive spaces with -
fix_spaces("Example") == "Example"
fix_spaces("Example 1") == "Example_1"
fix_spaces(" Example 2") == "_Example_2"
fix_spaces(" Example 3") == "_Example-3"
*/
#include<stdio.h>
#include<string>
using namespace std;
string fix_spaces(string text){
|
#include<stdio.h>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
string fix_spaces(string text){
|
[
" strin",
"1;\n ",
"\n if (spacelen==2) out=out+\"__\";\n if (spacelen>2) out=out+'-';\n spacelen=0;\n out=out+text[i];\n }\n if (spacelen==1) out=out+'_';\n if (spacelen==2) out=out+\"__\";\n if (spacelen>2) out=out+'-';\n return out;\n}\n"
] |
[
"g out=\"\";\n int spacelen=0;\n for (int i=0;i<text.length();i++)\n if (text[i]==' ') spacelen+=",
" else\n {\n if (spacelen==1) out=out+'_';"
] |
string out="";
int spacelen=0;
for (int i=0;i<text.length();i++)
if (text[i]==' ') spacelen+=1;
else
{
if (spacelen==1) out=out+'_';
if (spacelen==2) out=out+"__";
if (spacelen>2) out=out+'-';
spacelen=0;
out=out+text[i];
}
if (spacelen==1) out=out+'_';
if (spacelen==2) out=out+"__";
if (spacelen>2) out=out+'-';
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (fix_spaces("Example") == "Example");
assert (fix_spaces("Mudasir Hanif ") == "Mudasir_Hanif_");
assert (fix_spaces("Yellow Yellow Dirty Fellow") == "Yellow_Yellow__Dirty__Fellow");
assert (fix_spaces("Exa mple") == "Exa-mple");
assert (fix_spaces(" Exa 1 2 2 mple") == "-Exa_1_2_2_mple");
}
|
CPP/141_spans_2
| 2
|
/*
Create a function which takes a string representing a file's name, and returns
"Yes" if the the file's name is valid, and returns "No" otherwise.
A file's name is considered to be valid if and only if all the following conditions
are met:
- There should not be more than three digits ('0'-'9') in the file's name.
- The file's name contains exactly one dot "."
- The substring before the dot should not be empty, and it starts with a letter from
the latin alphapet ('a'-'z' and 'A'-'Z').
- The substring after the dot should be one of these: {'txt", "exe", "dll"}
Examples:
file_name_check("example.txt") => "Yes"
file_name_check("1example.dll") => "No" // (the name should start with a latin alphapet letter)
*/
#include<stdio.h>
#include<string>
using namespace std;
string file_name_check(string file_name){
|
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
#include<math.h>
#include<stdlib.h>
string file_name_check(string file_name){
|
[
" int numdigit=0,numdot=0;\n if (f",
">122",
",4);\n if (last!=\".txt\" and last!=\".exe\" and last!=\".dll\") return \"No\";\n for (int i=0;i<file_name.length();i++)\n {\n if (file_name[i]>=48 and file_name[i]<=57) numdigit+=1;\n if (file_name[i]=='.') numdot+=1;\n }\n if (numdigit>3 or numdot!=1) return \"No\";\n return \"Yes\"; \n}\n"
] |
[
"ile_name.length()<5) return \"No\";\n char w=file_name[0];\n if (w<65 or (w>90 and w<97) or w",
") return \"No\";\n string last=file_name.substr(file_name.length()-4"
] |
int numdigit=0,numdot=0;
if (file_name.length()<5) return "No";
char w=file_name[0];
if (w<65 or (w>90 and w<97) or w>122) return "No";
string last=file_name.substr(file_name.length()-4,4);
if (last!=".txt" and last!=".exe" and last!=".dll") return "No";
for (int i=0;i<file_name.length();i++)
{
if (file_name[i]>=48 and file_name[i]<=57) numdigit+=1;
if (file_name[i]=='.') numdot+=1;
}
if (numdigit>3 or numdot!=1) return "No";
return "Yes";
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (file_name_check("example.txt") == "Yes");
assert (file_name_check("1example.dll") == "No");
assert (file_name_check("s1sdf3.asd") == "No");
assert (file_name_check("K.dll") == "Yes");
assert (file_name_check("MY16FILE3.exe") == "Yes");
assert (file_name_check("His12FILE94.exe") == "No");
assert (file_name_check("_Y.txt") == "No");
assert (file_name_check("?aREYA.exe") == "No");
assert (file_name_check("/this_is_valid.dll") == "No");
assert (file_name_check("this_is_valid.wow") == "No");
assert (file_name_check("this_is_valid.txt") == "Yes");
assert (file_name_check("this_is_valid.txtexe") == "No");
assert (file_name_check("#this2_i4s_5valid.ten") == "No");
assert (file_name_check("@this1_is6_valid.exe") == "No");
assert (file_name_check("this_is_12valid.6exe4.txt") == "No");
assert (file_name_check("all.exe.txt") == "No");
assert (file_name_check("I563_No.exe") == "Yes");
assert (file_name_check("Is3youfault.txt") == "Yes");
assert (file_name_check("no_one#knows.dll") == "Yes");
assert (file_name_check("1I563_Yes3.exe") == "No");
assert (file_name_check("I563_Yes3.txtt") == "No");
assert (file_name_check("final..txt") == "No");
assert (file_name_check("final132") == "No");
assert (file_name_check("_f4indsartal132.") == "No");
assert (file_name_check(".txt") == "No");
assert (file_name_check("s.") == "No");
}
|
CPP/142_spans_2
| 2
|
/*
"
This function will take a vector of integers. For all entries in the vector, the function shall square the integer entry if its index is a
multiple of 3 and will cube the integer entry if its index is a multiple of 4 and not a multiple of 3. The function will not
change the entries in the vector whose indexes are not a multiple of 3 or 4. The function shall then return the sum of all entries.
Examples:
For lst = {1,2,3} the output should be 6
For lst = {} the output should be 0
For lst = {-1,-5,2,-1,-5} the output should be -126
*/
#include<stdio.h>
#include<vector>
using namespace std;
int sum_squares(vector<int> lst){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int sum_squares(vector<int> lst){
|
[
" int sum=0;\n for (int i=0;i<lst.size();i++)\n if (i%3==0) sum+=lst[i]*lst[i];\n ",
"lse if (",
"lst[i];\n return sum;\n}\n"
] |
[
" e",
"i%4==0) sum+=lst[i]*lst[i]*lst[i];\n else sum+="
] |
int sum=0;
for (int i=0;i<lst.size();i++)
if (i%3==0) sum+=lst[i]*lst[i];
else if (i%4==0) sum+=lst[i]*lst[i]*lst[i];
else sum+=lst[i];
return sum;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (sum_squares({1,2,3}) == 6);
assert (sum_squares({1,4,9}) == 14);
assert (sum_squares({}) == 0);
assert (sum_squares({1,1,1,1,1,1,1,1,1}) == 9);
assert (sum_squares({-1,-1,-1,-1,-1,-1,-1,-1,-1}) == -3);
assert (sum_squares({0}) == 0);
assert (sum_squares({-1,-5,2,-1,-5}) == -126);
assert (sum_squares({-56,-99,1,0,-2}) == 3030);
assert (sum_squares({-1,0,0,0,0,0,0,0,-1}) == 0);
assert (sum_squares({-16, -9, -2, 36, 36, 26, -20, 25, -40, 20, -4, 12, -26, 35, 37}) == -14196);
assert (sum_squares({-1, -3, 17, -1, -15, 13, -1, 14, -14, -12, -5, 14, -14, 6, 13, 11, 16, 16, 4, 10}) == -1448);
}
|
CPP/143_spans_2
| 2
|
/*
You are given a string representing a sentence,
the sentence contains some words separated by a space,
and you have to return a string that contains the words from the original sentence,
whose lengths are prime numbers,
the order of the words in the new string should be the same as the original one.
Example 1:
Input: sentence = "This is a test"
Output: "is"
Example 2:
Input: sentence = "lets go for swimming"
Output: "go for"
Constraints:
* 1 <= len(sentence) <= 100
* sentence contains only letters
*/
#include<stdio.h>
#include<string>
using namespace std;
string words_in_sentence(string sentence){
|
#include<stdio.h>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
string words_in_sentence(string sentence){
|
[
" string out=\"\";\n string current=\"\";\n sentence=sentence+' ';\n\n for (int i=0;i<sentence.size();i++)\n if (sentence[i]!=' ') current=current+sentence[i];\n else\n {\n bool isp=true;\n int l=cu",
"for (int j=2;j*j<=l;j++)\n if (l%j==0) isp=false;\n if (isp) out=out+current+' ';\n ",
"\n}\n"
] |
[
"rrent.length();\n if (l<2) isp=false;\n ",
" current=\"\"; \n }\n if (out.length()>0)\n out.pop_back();\n return out;"
] |
string out="";
string current="";
sentence=sentence+' ';
for (int i=0;i<sentence.size();i++)
if (sentence[i]!=' ') current=current+sentence[i];
else
{
bool isp=true;
int l=current.length();
if (l<2) isp=false;
for (int j=2;j*j<=l;j++)
if (l%j==0) isp=false;
if (isp) out=out+current+' ';
current="";
}
if (out.length()>0)
out.pop_back();
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (words_in_sentence("This is a test") == "is");
assert (words_in_sentence("lets go for swimming") == "go for");
assert (words_in_sentence("there is no place available here") == "there is no place");
assert (words_in_sentence("Hi I am Hussein") == "Hi am Hussein");
assert (words_in_sentence("go for it") == "go for it");
assert (words_in_sentence("here") == "");
assert (words_in_sentence("here is") == "is");
}
|
CPP/144_spans_2
| 2
|
/*
Your task is to implement a function that will simplify the expression
x * n. The function returns true if x * n evaluates to a whole number and false
otherwise. Both x and n, are string representation of a fraction, and have the following format,
<numerator>/<denominator> where both numerator and denominator are positive whole numbers.
You can assume that x, and n are valid fractions, and do not have zero as denominator.
simplify("1/5", "5/1") = true
simplify("1/6", "2/1") = false
simplify("7/10", "10/2") = false
*/
#include<stdio.h>
#include<string>
using namespace std;
bool simplify(string x,string n){
|
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
#include<math.h>
#include<stdlib.h>
bool simplify(string x,string n){
|
[
" int a,b,c,d,i;\n for (i=0;i<x.size();i++)\n if (x[i]=='/') \n {\n a=atoi(x.substr(0,i).c_str());\n b=atoi(x.su",
"]=='/') \n {\n c=atoi(n.substr(0,i).c_str());\n d=atoi(n.substr(i+1).c_str());\n }\n if ((a*c)%(b*d)==0) return true;\n ",
"turn false;\n}\n"
] |
[
"bstr(i+1).c_str());\n }\n for (i=0;i<n.size();i++)\n if (n[i",
" re"
] |
int a,b,c,d,i;
for (i=0;i<x.size();i++)
if (x[i]=='/')
{
a=atoi(x.substr(0,i).c_str());
b=atoi(x.substr(i+1).c_str());
}
for (i=0;i<n.size();i++)
if (n[i]=='/')
{
c=atoi(n.substr(0,i).c_str());
d=atoi(n.substr(i+1).c_str());
}
if ((a*c)%(b*d)==0) return true;
return false;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (simplify("1/5", "5/1") == true);
assert (simplify("1/6", "2/1") == false);
assert (simplify("5/1", "3/1") == true);
assert (simplify("7/10", "10/2") == false);
assert (simplify("2/10", "50/10") == true);
assert (simplify("7/2", "4/2") == true);
assert (simplify("11/6", "6/1") == true);
assert (simplify("2/3", "5/2") == false);
assert (simplify("5/2", "3/5") == false);
assert (simplify("2/4", "8/4") == true);
assert (simplify("2/4", "4/2") == true);
assert (simplify("1/5", "5/1") == true);
assert (simplify("1/5", "1/5") == false);
}
|
CPP/145_spans_2
| 2
|
/*
Write a function which sorts the given vector of integers
in ascending order according to the sum of their digits.
Note: if there are several items with similar sum of their digits,
order them based on their index in original vector.
For example:
>>> order_by_points({1, 11, -1, -11, -12}) == {-1, -11, 1, -12, 11}
>>> order_by_points({}) == {}
*/
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
vector<int> order_by_points(vector<int> nums){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> order_by_points(vector<int> nums){
|
[
" vector<int> sumdigit={};\n for (int i=0;i<nums.size();i++",
"ngth();j++)\n sum+=w[j]-48;\n if (nums[i]>0) sum+=w[0]-48;\n else sum-=w[0]-48;\n sumdigit.push_back(sum);\n }\n int m;\n for (int i=0;i<nums.size();i++)\n for (int j=",
"])\n {\n m=sumdigit[j];sumdigit[j]=sumdigit[j-1];sumdigit[j-1]=m;\n m=nums[j];nums[j]=nums[j-1];nums[j-1]=m;\n }\n \n return nums;\n}\n"
] |
[
")\n {\n string w=to_string(abs(nums[i]));\n int sum=0;\n for (int j=1;j<w.le",
"1;j<nums.size();j++)\n if (sumdigit[j-1]>sumdigit[j"
] |
vector<int> sumdigit={};
for (int i=0;i<nums.size();i++)
{
string w=to_string(abs(nums[i]));
int sum=0;
for (int j=1;j<w.length();j++)
sum+=w[j]-48;
if (nums[i]>0) sum+=w[0]-48;
else sum-=w[0]-48;
sumdigit.push_back(sum);
}
int m;
for (int i=0;i<nums.size();i++)
for (int j=1;j<nums.size();j++)
if (sumdigit[j-1]>sumdigit[j])
{
m=sumdigit[j];sumdigit[j]=sumdigit[j-1];sumdigit[j-1]=m;
m=nums[j];nums[j]=nums[j-1];nums[j-1]=m;
}
return nums;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(order_by_points({1, 11, -1, -11, -12}) , {-1, -11, 1, -12, 11}));
assert (issame(order_by_points({1234,423,463,145,2,423,423,53,6,37,3457,3,56,0,46}) , {0, 2, 3, 6, 53, 423, 423, 423, 1234, 145, 37, 46, 56, 463, 3457}));
assert (issame(order_by_points({}) , {}));
assert (issame(order_by_points({1, -11, -32, 43, 54, -98, 2, -3}) , {-3, -32, -98, -11, 1, 2, 43, 54}));
assert (issame(order_by_points({1,2,3,4,5,6,7,8,9,10,11}) , {1, 10, 2, 11, 3, 4, 5, 6, 7, 8, 9}));
assert (issame(order_by_points({0,6,6,-76,-21,23,4}) , {-76, -21, 0, 4, 23, 6, 6}));
}
|
CPP/146_spans_2
| 2
|
/*
Write a function that takes a vector of numbers as input and returns
the number of elements in the vector that are greater than 10 and both
first and last digits of a number are odd (1, 3, 5, 7, 9).
For example:
specialFilter({15, -73, 14, -15}) => 1
specialFilter({33, -2, -3, 45, 21, 109}) => 2
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
int specialFilter(vector<int> nums){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int specialFilter(vector<int> nums){
|
[
" int num=0;\n for (int i=0;i<nums.s",
"i]>10)\n {\n string w",
"1) num+=1;\n }\n return num;\n}\n"
] |
[
"ize();i++)\n if (nums[",
"=to_string(nums[i]);\n if (w[0]%2==1 and w[w.length()-1]%2=="
] |
int num=0;
for (int i=0;i<nums.size();i++)
if (nums[i]>10)
{
string w=to_string(nums[i]);
if (w[0]%2==1 and w[w.length()-1]%2==1) num+=1;
}
return num;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (specialFilter({5, -2, 1, -5}) == 0 );
assert (specialFilter({15, -73, 14, -15}) == 1);
assert (specialFilter({33, -2, -3, 45, 21, 109}) == 2);
assert (specialFilter({43, -12, 93, 125, 121, 109}) == 4);
assert (specialFilter({71, -2, -33, 75, 21, 19}) == 3);
assert (specialFilter({1}) == 0 );
assert (specialFilter({}) == 0 );
}
|
CPP/147_spans_2
| 2
|
/*
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){
|
#include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
int get_matrix_triples(int n){
|
[
" vector<int> a;\n vector<vector<int>> sum={{0,0,0}};\n vector<vector<int>> sum2={{0,0,0}};\n for (int i=1;i<=n;i++)\n",
";\n sum[i][a[i-1]]+=1;\n ",
" {\n for (int i=1;i<=n;i++)\n {\n sum2.push_back(sum2[sum2.size()-1]);\n if (i>=1)\n for (int j=0;j<=2;j++)\n sum2[i][(a[i-1]+j)%3]+=sum[i-1][j];\n }\n sum=sum2;\n sum2={{0,0,0}};\n }\n\n return sum[n][0];\n}\n"
] |
[
" {\n a.push_back((i*i-i+1)%3);\n sum.push_back(sum[sum.size()-1])",
"}\n for (int times=1;times<3;times++)\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];
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (get_matrix_triples(5) == 1);
assert (get_matrix_triples(6) == 4);
assert (get_matrix_triples(10) == 36);
assert (get_matrix_triples(100) == 53361);
}
|
CPP/148_spans_2
| 2
|
/*
There are eight planets in our solar system: the closerst to the Sun
is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn,
Uranus, Neptune.
Write a function that takes two planet names as strings planet1 and planet2.
The function should return a vector containing all planets whose orbits are
located between the orbit of planet1 and the orbit of planet2, sorted by
the proximity to the sun.
The function should return an empty vector if planet1 or planet2
are not correct planet names.
Examples
bf("Jupiter", "Neptune") ==> {"Saturn", "Uranus"}
bf("Earth", "Mercury") ==> {"Venus"}
bf("Mercury", "Uranus") ==> {"Venus", "Earth", "Mars", "Jupiter", "Saturn"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> bf(string planet1,string planet2){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<string> bf(string planet1,string planet2){
|
[
" vector<string> planets={\"Mercury\",\"Venus\",\"Earth\",\"Mars\",\"Jupiter\",\"Saturn\",\"Uranus\",\"Neptune\"};\n int pos1=-1,pos2=-1,m;\n for (m=0;m<planets.siz",
"==planet1) pos1=m;\n if (planets[m]==planet2) pos2=m;\n }\n if (pos1==-1",
") return {};\n if (pos1>pos2) {m=pos1;pos1=pos2;pos2=m;}\n vector<string> out={};\n for (m=pos1+1;m<pos2;m++)\n out.push_back(planets[m]);\n return out;\n}\n"
] |
[
"e();m++)\n {\n if (planets[m]",
" or pos2==-1"
] |
vector<string> planets={"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"};
int pos1=-1,pos2=-1,m;
for (m=0;m<planets.size();m++)
{
if (planets[m]==planet1) pos1=m;
if (planets[m]==planet2) pos2=m;
}
if (pos1==-1 or pos2==-1) return {};
if (pos1>pos2) {m=pos1;pos1=pos2;pos2=m;}
vector<string> out={};
for (m=pos1+1;m<pos2;m++)
out.push_back(planets[m]);
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(bf("Jupiter", "Neptune") , {"Saturn", "Uranus"}));
assert (issame(bf("Earth", "Mercury") , {"Venus",}));
assert (issame(bf("Mercury", "Uranus") , {"Venus", "Earth", "Mars", "Jupiter", "Saturn"}));
assert (issame(bf("Neptune", "Venus") , {"Earth", "Mars", "Jupiter", "Saturn", "Uranus"}));
assert (issame(bf("Earth", "Earth") , {}));
assert (issame(bf("Mars", "Earth") , {}));
assert (issame(bf("Jupiter", "Makemake") , {}));
}
|
CPP/149_spans_2
| 2
|
/*
Write a function that accepts a vector of strings as a parameter,
deletes the strings that have odd lengths from it,
and returns the resulted vector with a sorted order,
The vector is always a vector of strings and never a vector of numbers,
and it may contain duplicates.
The order of the vector should be ascending by length of each word, and you
should return the vector sorted by that rule.
If two words have the same length, sort the vector alphabetically.
The function should return a vector of strings in sorted order.
You may assume that all words will have the same length.
For example:
assert vector_sort({"aa", "a", "aaa"}) => {"aa"}
assert vector_sort({"ab", "a", "aaa", "cd"}) => {"ab", "cd"}
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
vector<string> sorted_list_sum(vector<string> lst){
|
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<math.h>
#include<stdlib.h>
vector<string> sorted_list_sum(vector<string> lst){
|
[
" vector<string> out={};\n for (int i=0;i<lst.size();i++)\n if (lst[i].length()%2==0) out.push_back(lst[i]);\n string mid;\n sort(out.begin(),out.end());\n for (int i=0",
"[j-1].length())\n {\n ",
" return out;\n}\n"
] |
[
";i<out.size();i++)\n for (int j=1;j<out.size();j++)\n if (out[j].length()<out",
"mid=out[j];out[j]=out[j-1];out[j-1]=mid;\n }\n"
] |
vector<string> out={};
for (int i=0;i<lst.size();i++)
if (lst[i].length()%2==0) out.push_back(lst[i]);
string mid;
sort(out.begin(),out.end());
for (int i=0;i<out.size();i++)
for (int j=1;j<out.size();j++)
if (out[j].length()<out[j-1].length())
{
mid=out[j];out[j]=out[j-1];out[j-1]=mid;
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(sorted_list_sum({"aa", "a", "aaa"}) , {"aa"}));
assert (issame(sorted_list_sum({"school", "AI", "asdf", "b"}) , {"AI", "asdf", "school"}));
assert (issame(sorted_list_sum({"d", "b", "c", "a"}) , {}));
assert (issame(sorted_list_sum({"d", "dcba", "abcd", "a"}) , {"abcd", "dcba"}));
assert (issame(sorted_list_sum({"AI", "ai", "au"}) , {"AI", "ai", "au"}));
assert (issame(sorted_list_sum({"a", "b", "b", "c", "c", "a"}) , {}));
assert (issame(sorted_list_sum({"aaaa", "bbbb", "dd", "cc"}) , {"cc", "dd", "aaaa", "bbbb"}));
}
|
CPP/150_spans_2
| 2
|
/*
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){
|
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int x_or_y(int n,int x,int y){
|
[
" bool isp=true;\n if (n<2) isp=false;\n for",
"n",
" if (isp) return x;\n return y;\n}\n"
] |
[
" (int i=2;i*i<=n;i++)\n if (",
"%i==0) isp=false;\n "
] |
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;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (x_or_y(7, 34, 12) == 34);
assert (x_or_y(15, 8, 5) == 5);
assert (x_or_y(3, 33, 5212) == 33);
assert (x_or_y(1259, 3, 52) == 3);
assert (x_or_y(7919, -1, 12) == -1);
assert (x_or_y(3609, 1245, 583) == 583);
assert (x_or_y(91, 56, 129) == 129);
assert (x_or_y(6, 34, 1234) == 1234);
assert (x_or_y(1, 2, 0) == 0);
assert (x_or_y(2, 2, 0) == 2);
}
|
CPP/151_spans_2
| 2
|
/*
Given a vector of numbers, return the sum of squares of the numbers
in the vector that are odd. Ignore numbers that are negative or not integers.
double_the_difference({1, 3, 2, 0}) == 1 + 9 + 0 + 0 = 10
double_the_difference({-1, -2, 0}) == 0
double_the_difference({9, -2}) == 81
double_the_difference({0}) == 0
If the input vector is empty, return 0.
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
long long double_the_difference(vector<float> lst){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
long long double_the_difference(vector<float> lst){
|
[
" long long s",
"d(lst[i])<",
"ound(lst[i]));\n return sum;\n}\n"
] |
[
"um=0;\n for (int i=0;i<lst.size();i++)\n if (lst[i]-roun",
"1e-4)\n if (lst[i]>0 and (int)(round(lst[i]))%2==1) sum+=(int)(round(lst[i]))*(int)(r"
] |
long long sum=0;
for (int i=0;i<lst.size();i++)
if (lst[i]-round(lst[i])<1e-4)
if (lst[i]>0 and (int)(round(lst[i]))%2==1) sum+=(int)(round(lst[i]))*(int)(round(lst[i]));
return sum;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (double_the_difference({}) == 0);
assert (double_the_difference({5, 4}) == 25);
assert (double_the_difference({0.1, 0.2, 0.3}) == 0 );
assert (double_the_difference({-10, -20, -30}) == 0 );
assert (double_the_difference({-1, -2, 8}) == 0);
assert (double_the_difference({0.2, 3, 5}) == 34);
long long odd_sum=0;
vector<float> lst={};
for (int i=-99;i<100;i+=2)
{
lst.push_back(i+0.0);
if (i>0 and i%2==1) odd_sum+=i*i;
}
assert (double_the_difference(lst) == odd_sum );
}
|
CPP/152_spans_2
| 2
|
/*
I think we all remember that feeling when the result of some long-awaited
event is finally known. The feelings and thoughts you have at that moment are
definitely worth noting down and comparing.
Your task is to determine if a person correctly guessed the results of a number of matches.
You are given two vectors of scores and guesses of equal length, where each index shows a match.
Return a vector of the same length denoting how far off each guess was. If they have guessed correctly,
the value is 0, and if not, the value is the absolute difference between the guess and the score.
example:
compare({1,2,3,4,5,1},{1,2,3,4,2,-2}) -> {0,0,0,0,3,3}
compare({0,5,0,0,0,4},{4,1,1,0,0,-2}) -> {4,4,1,0,0,6}
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
vector<int> compare(vector<int> game,vector<int> guess){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> compare(vector<int> game,vector<int> guess){
|
[
" vector<int> ",
"for (int i=0;i<game.size();i++)",
"game[i]-guess[i]));\n return out;\n}\n"
] |
[
"out;\n ",
"\n out.push_back(abs("
] |
vector<int> out;
for (int i=0;i<game.size();i++)
out.push_back(abs(game[i]-guess[i]));
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(compare({1,2,3,4,5,1},{1,2,3,4,2,-2}),{0,0,0,0,3,3}));
assert (issame(compare({0,5,0,0,0,4},{4,1,1,0,0,-2}),{4,4,1,0,0,6}));
assert (issame(compare({1,2,3,4,5,1},{1,2,3,4,2,-2}),{0,0,0,0,3,3}));
assert (issame(compare({0,0,0,0,0,0},{0,0,0,0,0,0}),{0,0,0,0,0,0}));
assert (issame(compare({1,2,3},{-1,-2,-3}),{2,4,6}));
assert (issame(compare({1,2,3,5},{-1,2,3,4}),{2,0,0,1}));
}
|
CPP/153_spans_2
| 2
|
/*
You will be given the name of a class (a string) and a vector of extensions.
The extensions are to be used to load additional classes to the class. The
strength of the extension is as follows: Let CAP be the number of the uppercase
letters in the extension's name, and let SM be the number of lowercase letters
in the extension's name, the strength is given by the fraction CAP - SM.
You should find the strongest extension and return a string in this
format: ClassName.StrongestExtensionName.
If there are two or more extensions with the same strength, you should
choose the one that comes first in the vector.
For example, if you are given "Slices" as the class and a vector of the
extensions: {"SErviNGSliCes", "Cheese", "StuFfed"} then you should
return "Slices.SErviNGSliCes" since "SErviNGSliCes" is the strongest extension
(its strength is -1).
Example:
for Strongest_Extension("my_class", {"AA", "Be", "CC"}) == "my_class.AA"
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
string Strongest_Extension(string class_name,vector<string> extensions){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
string Strongest_Extension(string class_name,vector<string> extensions){
|
[
" string strongest=\"\";\n int max=-1000;\n for (int i=0;i<extensions.size();i++)\n {\n int strength=0;\n for (int j=0;j<extensions[i].length();j++)\n {\n char chr=extensions[i][j];\n if (chr>=65 and chr<=90",
"r>=97 and chr<=122) strength-=1;\n }\n if (strength>max) \n {\n max=strength;\n strongest=extensions[i];\n }\n ",
"n class_name+'.'+strongest;\n}\n"
] |
[
") strength+=1;\n if (ch",
" }\n retur"
] |
string strongest="";
int max=-1000;
for (int i=0;i<extensions.size();i++)
{
int strength=0;
for (int j=0;j<extensions[i].length();j++)
{
char chr=extensions[i][j];
if (chr>=65 and chr<=90) strength+=1;
if (chr>=97 and chr<=122) strength-=1;
}
if (strength>max)
{
max=strength;
strongest=extensions[i];
}
}
return class_name+'.'+strongest;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (Strongest_Extension("Watashi", {"tEN", "niNE", "eIGHt8OKe"}) == "Watashi.eIGHt8OKe");
assert (Strongest_Extension("Boku123", {"nani", "NazeDa", "YEs.WeCaNe", "32145tggg"}) == "Boku123.YEs.WeCaNe");
assert (Strongest_Extension("__YESIMHERE", {"t", "eMptY", "(nothing", "zeR00", "NuLl__", "123NoooneB321"}) == "__YESIMHERE.NuLl__");
assert (Strongest_Extension("K", {"Ta", "TAR", "t234An", "cosSo"}) == "K.TAR");
assert (Strongest_Extension("__HAHA", {"Tab", "123", "781345", "-_-"}) == "__HAHA.123");
assert (Strongest_Extension("YameRore", {"HhAas", "okIWILL123", "WorkOut", "Fails", "-_-"}) == "YameRore.okIWILL123");
assert (Strongest_Extension("finNNalLLly", {"Die", "NowW", "Wow", "WoW"}) == "finNNalLLly.WoW");
assert (Strongest_Extension("_", {"Bb", "91245"}) == "_.Bb");
assert (Strongest_Extension("Sp", {"671235", "Bb"}) == "Sp.671235");
}
|
CPP/154_spans_2
| 2
|
/*
You are given 2 words. You need to return true if the second word or any of its rotations is a substring in the first word
cycpattern_check("abcd","abd") => false
cycpattern_check("hello","ell") => true
cycpattern_check("whassup","psus") => false
cycpattern_check("abab","baa") => true
cycpattern_check("efef","eeff") => false
cycpattern_check("himenss",'simen") => true
*/
#include<stdio.h>
#include<string>
using namespace std;
bool cycpattern_check(string a,string b){
|
#include<stdio.h>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
bool cycpattern_check(string a,string b){
|
[
" ",
"ubstr(0,i);\n if (a.find(rotate)!=string::npos) return tr",
" }\n return false;\n\n}\n"
] |
[
" for (int i=0;i<b.size();i++)\n {\n string rotate=b.substr(i)+b.s",
"ue;\n "
] |
for (int i=0;i<b.size();i++)
{
string rotate=b.substr(i)+b.substr(0,i);
if (a.find(rotate)!=string::npos) return true;
}
return false;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (cycpattern_check("xyzw","xyw") == false );
assert (cycpattern_check("yello","ell") == true );
assert (cycpattern_check("whattup","ptut") == false );
assert (cycpattern_check("efef","fee") == true );
assert (cycpattern_check("abab","aabb") == false );
assert (cycpattern_check("winemtt","tinem") == true );
}
|
CPP/155_spans_2
| 2
|
/*
Given an integer. return a vector that has the number of even and odd digits respectively.
Example:
even_odd_count(-12) ==> {1, 1}
even_odd_count(123) ==> {1, 2}
*/
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
using namespace std;
vector<int> even_odd_count(int num){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> even_odd_count(int num){
|
[
" string w=to_string(abs(num));\n int n1=0,n2=0;\n ",
" i=0;i<w.length();i++)\n ",
") n1+=1;\n else n2+=1;\n return {n2,n1};\n}\n"
] |
[
" for (int",
"if (w[i]%2==1"
] |
string w=to_string(abs(num));
int n1=0,n2=0;
for (int i=0;i<w.length();i++)
if (w[i]%2==1) n1+=1;
else n2+=1;
return {n2,n1};
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(even_odd_count(7) , {0, 1}));
assert (issame(even_odd_count(-78) , {1, 1}));
assert (issame(even_odd_count(3452) , {2, 2}));
assert (issame(even_odd_count(346211) , {3, 3}));
assert (issame(even_odd_count(-345821) , {3, 3}));
assert (issame(even_odd_count(-2) , {1, 0}));
assert (issame(even_odd_count(-45347) , {2, 3}));
assert (issame(even_odd_count(0) , {1, 0}));
}
|
CPP/156_spans_2
| 2
|
/*
Given a positive integer, obtain its roman numeral equivalent as a string,
and return it in lowercase.
Restrictions: 1 <= num <= 1000
Examples:
>>> int_to_mini_roman(19) == "xix"
>>> int_to_mini_roman(152) == "clii"
>>> int_to_mini_roman(426) == "cdxxvi"
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
string int_to_mini_romank(int number){
|
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
string int_to_mini_romank(int number){
|
[
" string current=\"\";\n vector<string> rep={\"m\",\"cm\",\"d\",\"cd\",\"c\",\"xc\",\"l\",\"xl\",\"x\",\"ix\",\"v\",\"iv\",\"i\"};\n vector<int> num={1000,900,500,400,100,90,50,40,",
" while(number>0)\n {\n while (number>=num[pos])\n {\n current=current+rep[pos];\n ",
"r-=num[pos];\n }\n if (number>0) pos+=1;\n }\n return current;\n}\n"
] |
[
"10,9,5,4,1};\n int pos=0;\n ",
" numbe"
] |
string current="";
vector<string> rep={"m","cm","d","cd","c","xc","l","xl","x","ix","v","iv","i"};
vector<int> num={1000,900,500,400,100,90,50,40,10,9,5,4,1};
int pos=0;
while(number>0)
{
while (number>=num[pos])
{
current=current+rep[pos];
number-=num[pos];
}
if (number>0) pos+=1;
}
return current;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (int_to_mini_romank(19) == "xix");
assert (int_to_mini_romank(152) == "clii");
assert (int_to_mini_romank(251) == "ccli");
assert (int_to_mini_romank(426) == "cdxxvi");
assert (int_to_mini_romank(500) == "d");
assert (int_to_mini_romank(1) == "i");
assert (int_to_mini_romank(4) == "iv");
assert (int_to_mini_romank(43) == "xliii");
assert (int_to_mini_romank(90) == "xc");
assert (int_to_mini_romank(94) == "xciv");
assert (int_to_mini_romank(532) == "dxxxii");
assert (int_to_mini_romank(900) == "cm");
assert (int_to_mini_romank(994) == "cmxciv");
assert (int_to_mini_romank(1000) == "m");
}
|
CPP/157_spans_2
| 2
|
/*
Given the lengths of the three sides of a triangle. Return true if the three
sides form a right-angled triangle, false otherwise.
A right-angled triangle is a triangle in which one angle is right angle or
90 degree.
Example:
right_angle_triangle(3, 4, 5) == true
right_angle_triangle(1, 2, 3) == false
*/
#include<stdio.h>
#include<math.h>
using namespace std;
bool right_angle_triangle(float a,float b,float c){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool right_angle_triangle(float a,float b,float c){
|
[
" i",
"+b*b-c*c)<1e-4 or abs(a*a+c*c-b*b)<1e-4 or abs(",
" return false;\n}\n"
] |
[
"f (abs(a*a",
"b*b+c*c-a*a)<1e-4) return true;\n "
] |
if (abs(a*a+b*b-c*c)<1e-4 or abs(a*a+c*c-b*b)<1e-4 or abs(b*b+c*c-a*a)<1e-4) return true;
return false;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (right_angle_triangle(3, 4, 5) == true);
assert (right_angle_triangle(1, 2, 3) == false);
assert (right_angle_triangle(10, 6, 8) == true);
assert (right_angle_triangle(2, 2, 2) == false);
assert (right_angle_triangle(7, 24, 25) == true);
assert (right_angle_triangle(10, 5, 7) == false);
assert (right_angle_triangle(5, 12, 13) == true);
assert (right_angle_triangle(15, 8, 17) == true);
assert (right_angle_triangle(48, 55, 73) == true);
assert (right_angle_triangle(1, 1, 1) == false);
assert (right_angle_triangle(2, 2, 10) == false);
}
|
CPP/158_spans_2
| 2
|
/*
Write a function that accepts a vector of strings.
The vector contains different words. Return the word with maximum number
of unique characters. If multiple strings have maximum number of unique
characters, return the one which comes first in lexicographical order.
find_max({"name", "of", 'string"}) == 'string"
find_max({"name", "enam", "game"}) == "enam"
find_max({"aaaaaaa", "bb" ,"cc"}) == "aaaaaaa"
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
string find_max(vector<string> words){
|
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<math.h>
#include<stdlib.h>
string find_max(vector<string> words){
|
[
" string max=\"\";\n int maxu=0;\n for (int i=0;i<words.size();i++)\n {\n string unique=\"\";\n for (int j=",
"ength();j++)\n if (find(unique.begin(),unique.end(),words[i][j])==unique.end())\n unique=unique+words[i][j];\n if (",
"que.length()==maxu and words[i]<max))\n {\n max=words[i];\n maxu=unique.length();\n }\n }\n return max;\n}\n"
] |
[
"0;j<words[i].l",
"unique.length()>maxu or (uni"
] |
string max="";
int maxu=0;
for (int i=0;i<words.size();i++)
{
string unique="";
for (int j=0;j<words[i].length();j++)
if (find(unique.begin(),unique.end(),words[i][j])==unique.end())
unique=unique+words[i][j];
if (unique.length()>maxu or (unique.length()==maxu and words[i]<max))
{
max=words[i];
maxu=unique.length();
}
}
return max;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert ((find_max({"name", "of", "string"}) == "string"));
assert ((find_max({"name", "enam", "game"}) == "enam"));
assert ((find_max({"aaaaaaa", "bb", "cc"}) == "aaaaaaa"));
assert ((find_max({"abc", "cba"}) == "abc"));
assert ((find_max({"play", "this", "game", "of","footbott"}) == "footbott"));
assert ((find_max({"we", "are", "gonna", "rock"}) == "gonna"));
assert ((find_max({"we", "are", "a", "mad", "nation"}) == "nation"));
assert ((find_max({"this", "is", "a", "prrk"}) == "this"));
assert ((find_max({"b"}) == "b"));
assert ((find_max({"play", "play", "play"}) == "play"));
}
|
CPP/159_spans_2
| 2
|
/*
You"re a hungry rabbit, and you already have eaten a certain number of carrots,
but now you need to eat more carrots to complete the day's meals.
you should return a vector of { total number of eaten carrots after your meals,
the number of carrots left after your meals }
if there are not enough remaining carrots, you will eat all remaining carrots, but will still be hungry.
Example:
* eat(5, 6, 10) -> {11, 4}
* eat(4, 8, 9) -> {12, 1}
* eat(1, 10, 10) -> {11, 0}
* eat(2, 11, 5) -> {7, 0}
Variables:
@number : integer
the number of carrots that you have eaten.
@need : integer
the number of carrots that you need to eat.
@remaining : integer
the number of remaining carrots thet exist in stock
Constrain:
* 0 <= number <= 1000
* 0 <= need <= 1000
* 0 <= remaining <= 1000
Have fun :)
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> eat(int number,int need,int remaining){
|
#include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<int> eat(int number,int need,int remaining){
|
[
" ",
"eed>rema",
"};\n}\n"
] |
[
" if (n",
"ining) return {number+remaining, 0};\n return {number+need,remaining-need"
] |
if (need>remaining) return {number+remaining, 0};
return {number+need,remaining-need};
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(eat(5, 6, 10) , {11, 4}));
assert (issame(eat(4, 8, 9) , {12, 1}));
assert (issame(eat(1, 10, 10) , {11, 0}));
assert (issame(eat(2, 11, 5) , {7, 0}));
assert (issame(eat(4, 5, 7) , {9, 2}));
assert (issame(eat(4, 5, 1) , {5, 0}));
}
|
CPP/160_spans_2
| 2
|
/*
Given two vectors operator, and operand. The first vector has basic algebra operations, and
the second vector is a vector of integers. Use the two given vectors to build the algebric
expression and return the evaluation of this expression.
The basic algebra operations:
Addition ( + )
Subtraction ( - )
Multiplication ( * )
Floor division ( // )
Exponentiation ( ** )
Example:
operator{"+", "*", "-"}
vector = {2, 3, 4, 5}
result = 2 + 3 * 4 - 5
=> result = 9
Note:
The length of operator vector is equal to the length of operand vector minus one.
Operand is a vector of of non-negative integers.
Operator vector has at least one operator, and operand vector has at least two operands.
*/
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int do_algebra(vector<string> operato, vector<int> operand){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int do_algebra(vector<string> operato, vector<int> operand){
|
[
" vector<int> num={};\n vector<int> posto={};\n for (int i=0;i<operand.size();i++)\n posto.push_back(i);\n for (int i=0;i<operato.size();i++)\n if (operato[i]==\"**\") \n {\n while (posto[posto[i]]!=posto[i]) posto[i]=posto[posto[i]];\n while (posto[posto[i+1]]!=posto[i+1]) posto[i+1]=posto[posto[i+1]];\n operand[posto[i]]=pow(operand[posto[i]],operand[posto[i+1]]);\n ",
"=0;i<operato.size();i++)\n if (operato[i]==\"*\" or operato[i]==\"//\") \n {\n while (posto[posto[i]]!=posto[i]) posto[i]=posto[posto[i]];\n while (posto[posto[i+1]]!=posto[i+1]) posto[i+1]=posto[posto[i+1]];\n if (operato[i]==\"*\")\n operand[posto[i]]=operand[posto[i]]*operand[posto[i+1]];\n else\n operand[posto[i]]=operand[posto[i]]/operand[posto[i+1]];\n posto[i+1]=posto[i];\n }\n for (int i=0;i<operato.size();i++)\n if (operato[i]==\"+\" or operato[i]==\"-\") \n {\n while (posto[posto[i]]!=posto[i]) posto[i]=posto[posto[i]];\n while (posto[posto[i+1]]!=posto[i+1]) posto[i+1]=posto[posto[i+1]];\n if (operato[i]==\"+\")\n operand[posto[i]]=opera",
"sto[i]]-operand[posto[i+1]];\n posto[i+1]=posto[i];\n }\n return operand[0];\n\n}\n"
] |
[
" posto[i+1]=posto[i];\n }\n for (int i",
"nd[posto[i]]+operand[posto[i+1]];\n else\n operand[posto[i]]=operand[po"
] |
vector<int> num={};
vector<int> posto={};
for (int i=0;i<operand.size();i++)
posto.push_back(i);
for (int i=0;i<operato.size();i++)
if (operato[i]=="**")
{
while (posto[posto[i]]!=posto[i]) posto[i]=posto[posto[i]];
while (posto[posto[i+1]]!=posto[i+1]) posto[i+1]=posto[posto[i+1]];
operand[posto[i]]=pow(operand[posto[i]],operand[posto[i+1]]);
posto[i+1]=posto[i];
}
for (int i=0;i<operato.size();i++)
if (operato[i]=="*" or operato[i]=="//")
{
while (posto[posto[i]]!=posto[i]) posto[i]=posto[posto[i]];
while (posto[posto[i+1]]!=posto[i+1]) posto[i+1]=posto[posto[i+1]];
if (operato[i]=="*")
operand[posto[i]]=operand[posto[i]]*operand[posto[i+1]];
else
operand[posto[i]]=operand[posto[i]]/operand[posto[i+1]];
posto[i+1]=posto[i];
}
for (int i=0;i<operato.size();i++)
if (operato[i]=="+" or operato[i]=="-")
{
while (posto[posto[i]]!=posto[i]) posto[i]=posto[posto[i]];
while (posto[posto[i+1]]!=posto[i+1]) posto[i+1]=posto[posto[i+1]];
if (operato[i]=="+")
operand[posto[i]]=operand[posto[i]]+operand[posto[i+1]];
else
operand[posto[i]]=operand[posto[i]]-operand[posto[i+1]];
posto[i+1]=posto[i];
}
return operand[0];
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (do_algebra({"**", "*", "+"}, {2, 3, 4, 5}) == 37);
assert (do_algebra({"+", "*", "-"}, {2, 3, 4, 5}) == 9);
assert (do_algebra({"//", "*"}, {7, 3, 4}) == 8);
}
|
CPP/161_spans_2
| 2
|
/*
You are given a string s.
if s[i] is a letter, reverse its case from lower to upper or vise versa,
otherwise keep it as it is.
If the string contains no letters, reverse the string.
The function should return the resulted string.
Examples
solve("1234") = "4321"
solve("ab") = "AB"
solve("#a@C") = "#A@c"
*/
#include<stdio.h>
#include<string>
using namespace std;
string solve(string s){
|
#include<stdio.h>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
string solve(string s){
|
[
" int nletter=0;\n string out=\"\";\n for (int i=0;i<s.length();i++)\n {\n char w=s[i];\n if (w>=65 and w<=90) w=w+32;\n ",
"=out+w;\n }\n if (nletter==s.length())\n {\n string p",
" else return out;\n}\n"
] |
[
" else if (w>=97 and w<=122) w=w-32;\n else nletter+=1;\n out",
"(s.rbegin(),s.rend());\n return p;\n }\n "
] |
int nletter=0;
string out="";
for (int i=0;i<s.length();i++)
{
char w=s[i];
if (w>=65 and w<=90) w=w+32;
else if (w>=97 and w<=122) w=w-32;
else nletter+=1;
out=out+w;
}
if (nletter==s.length())
{
string p(s.rbegin(),s.rend());
return p;
}
else return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (solve("AsDf") == "aSdF");
assert (solve("1234") == "4321");
assert (solve("ab") == "AB");
assert (solve("#a@C") == "#A@c");
assert (solve("#AsdfW^45") == "#aSDFw^45");
assert (solve("#6@2") == "2@6#");
assert (solve("#$a^D") == "#$A^d");
assert (solve("#ccc") == "#CCC");
}
|
CPP/162_spans_2
| 2
|
/*
Given a string 'text", return its md5 hash equivalent string.
If 'text" is an empty string, return None.
>>> string_to_md5("Hello world") == "3e25960a79dbc69b674cd4ec67a72c62"
*/
#include<stdio.h>
#include<string>
#include<openssl/md5.h>
using namespace std;
string string_to_md5(string text){
|
#include<stdio.h>
#include<string>
#include<openssl/md5.h>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
string string_to_md5(string text){
|
[
" unsigned char md[16];\n if (text.length()==0) return \"None\";\n MD5_CTX c;\n int i;\n MD5_Init(&c);\n MD5_Update(&c, (unsigned char*)text.c_str(), text.length());\n MD5_Final(md, &c);\n string out_str=\"\";\n for (int i=0;i<16;i++)\n {\n char w;\n if (md[i]<160) w=48+md[i]/16;\n else w=87+md[i]/16;\n ",
"t_str+w;\n if (md[i]%16<10) w=48+md[i]%16;\n else w=87+",
"r;\n}\n"
] |
[
" out_str=ou",
"md[i]%16;\n out_str=out_str+w;\n }\n return out_st"
] |
unsigned char md[16];
if (text.length()==0) return "None";
MD5_CTX c;
int i;
MD5_Init(&c);
MD5_Update(&c, (unsigned char*)text.c_str(), text.length());
MD5_Final(md, &c);
string out_str="";
for (int i=0;i<16;i++)
{
char w;
if (md[i]<160) w=48+md[i]/16;
else w=87+md[i]/16;
out_str=out_str+w;
if (md[i]%16<10) w=48+md[i]%16;
else w=87+md[i]%16;
out_str=out_str+w;
}
return out_str;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (string_to_md5("Hello world") == "3e25960a79dbc69b674cd4ec67a72c62");
assert (string_to_md5("") == "None");
assert (string_to_md5("A B C") == "0ef78513b0cb8cef12743f5aeb35f888");
assert (string_to_md5("password") == "5f4dcc3b5aa765d61d8327deb882cf99");
}
|
CPP/163_spans_2
| 2
|
/*
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){
|
#include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<int> generate_integers(int a,int b){
|
[
"",
"=m;\n }\n\n vector<int> out",
"t.push_back(i);\n return out;\n}\n"
] |
[
" int m;\n if (b<a)\n {\n m=a;a=b;b",
"={};\n for (int i=a;i<=b;i++)\n if (i<10 and i%2==0) ou"
] |
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;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(generate_integers(2, 10) , {2, 4, 6, 8}));
assert (issame(generate_integers(10, 2) , {2, 4, 6, 8}));
assert (issame(generate_integers(132, 2) , {2, 4, 6, 8}));
assert (issame(generate_integers(17,89) , {}));
}
|
CPP/0_spans_3
| 3
|
/*
Check if in given vector of numbers, are any two numbers closer to each other than
given threshold.
>>> has_close_elements({1.0, 2.0, 3.0}, 0.5)
false
>>> has_close_elements({1.0, 2.8, 3.0, 4.0, 5.0, 2.0}, 0.3)
true
*/
#include<stdio.h>
#include<vector>
#include<math.h>
using namespace std;
bool has_close_elements(vector<float> numbers, float threshold){
|
#include<stdio.h>
#include<vector>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool has_close_elements(vector<float> numbers, float threshold){
|
[
" int i,j;\n \n fo",
");i++",
"rs[j",
"false;\n}\n\n"
] |
[
"r (i=0;i<numbers.size(",
")\n for (j=i+1;j<numbers.size();j++)\n if (abs(numbers[i]-numbe",
"])<threshold)\n return true;\n\n return "
] |
int i,j;
for (i=0;i<numbers.size();i++)
for (j=i+1;j<numbers.size();j++)
if (abs(numbers[i]-numbers[j])<threshold)
return true;
return false;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
vector<float> a={1.0, 2.0, 3.9, 4.0, 5.0, 2.2};
assert (has_close_elements(a, 0.3)==true);
assert (has_close_elements(a, 0.05) == false);
assert (has_close_elements({1.0, 2.0, 5.9, 4.0, 5.0}, 0.95) == true);
assert (has_close_elements({1.0, 2.0, 5.9, 4.0, 5.0}, 0.8) ==false);
assert (has_close_elements({1.0, 2.0, 3.0, 4.0, 5.0}, 2.0) == true);
assert (has_close_elements({1.1, 2.2, 3.1, 4.1, 5.1}, 1.0) == true);
assert (has_close_elements({1.1, 2.2, 3.1, 4.1, 5.1}, 0.5) == false);
}
|
CPP/1_spans_3
| 3
|
/*
Input to this function is a string containing multiple groups of nested parentheses. Your goal is to
separate those group into separate strings and return the vector of those.
Separate groups are balanced (each open brace is properly closed) and not nested within each other
Ignore any spaces in the input string.
>>> separate_paren_groups("( ) (( )) (( )( ))")
{"()", "(())", "(()())"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> separate_paren_groups(string paren_string){
|
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<string> separate_paren_groups(string paren_string){
|
[
" vector<string> all_parens;\n string current_paren;\n int level=0;\n ",
"r=paren_stri",
" }\n if (chr==')')\n",
" if (level==0){\n all_parens.push_back(current_paren);\n current_paren=\"\";\n }\n }\n }\n return all_parens;\n}\n"
] |
[
" char chr;\n int i;\n for (i=0;i<paren_string.length();i++)\n {\n ch",
"ng[i];\n if (chr=='(')\n {\n level+=1;\n current_paren+=chr;\n ",
" {\n level-=1;\n current_paren+=chr;\n "
] |
vector<string> all_parens;
string current_paren;
int level=0;
char chr;
int i;
for (i=0;i<paren_string.length();i++)
{
chr=paren_string[i];
if (chr=='(')
{
level+=1;
current_paren+=chr;
}
if (chr==')')
{
level-=1;
current_paren+=chr;
if (level==0){
all_parens.push_back(current_paren);
current_paren="";
}
}
}
return all_parens;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(separate_paren_groups("(()()) ((())) () ((())()())"),{"(()())", "((()))", "()", "((())()())"}));
assert (issame(separate_paren_groups("() (()) ((())) (((())))"), {"()", "(())", "((()))", "(((())))" }));
assert (issame(separate_paren_groups("(()(())((())))") ,{ "(()(())((())))" }));
assert (issame(separate_paren_groups("( ) (( )) (( )( ))") ,{"()", "(())", "(()())"}));
}
|
CPP/3_spans_3
| 3
|
/*
You"re given a vector of deposit and withdrawal operations on a bank account that starts with
zero balance. Your task is to detect if at any point the balance of account falls below zero, and
at that point function should return true. Otherwise it should return false.
>>> below_zero({1, 2, 3})
false
>>> below_zero({1, 2, -4, 5})
true
*/
#include<stdio.h>
#include<vector>
using namespace std;
bool below_zero(vector<int> operations){
|
#include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
bool below_zero(vector<int> operations){
|
[
"",
"(int i=0;i<op",
"e;\n }\n ",
"e;\n}\n"
] |
[
" int num=0;\n for ",
"erations.size();i++)\n {\n num+=operations[i];\n if (num<0) return tru",
" return fals"
] |
int num=0;
for (int i=0;i<operations.size();i++)
{
num+=operations[i];
if (num<0) return true;
}
return false;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (below_zero({}) == false);
assert (below_zero({1, 2, -3, 1, 2, -3}) == false);
assert (below_zero({1, 2, -4, 5, 6}) == true);
assert (below_zero({1, -1, 2, -2, 5, -5, 4, -4}) == false);
assert (below_zero({1, -1, 2, -2, 5, -5, 4, -5}) == true);
assert (below_zero({1, -2, 2, -2, 5, -5, 4, -4}) == true);
}
|
CPP/4_spans_3
| 3
|
/*
For a given vector of input numbers, calculate Mean Absolute Deviation
around the mean of this dataset.
Mean Absolute Deviation is the average absolute difference between each
element and a centerpoint (mean in this case):
MAD = average | x - x_mean |
>>> mean_absolute_deviation({1.0, 2.0, 3.0, 4.0})
1.0
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
float mean_absolute_deviation(vector<float> numbers){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
float mean_absolute_deviation(vector<float> numbers){
|
[
" f",
" int i=0;\n for (i=0;i<numbers.size();i++)\n sum+=numbers[i",
" ",
"bs(numbers[i]-avg);\n return msum/numbers.size();\n}\n"
] |
[
"loat sum=0;\n float avg,msum,mavg;\n ",
"];\n avg=sum/numbers.size();\n",
" msum=0;\n for (i=0;i<numbers.size();i++)\n msum+=a"
] |
float sum=0;
float avg,msum,mavg;
int i=0;
for (i=0;i<numbers.size();i++)
sum+=numbers[i];
avg=sum/numbers.size();
msum=0;
for (i=0;i<numbers.size();i++)
msum+=abs(numbers[i]-avg);
return msum/numbers.size();
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (abs(mean_absolute_deviation({1.0, 2.0, 3.0}) - 2.0/3.0) < 1e-4);
assert (abs(mean_absolute_deviation({1.0, 2.0, 3.0, 4.0}) - 1.0) < 1e-4);
assert (abs(mean_absolute_deviation({1.0, 2.0, 3.0, 4.0, 5.0}) - 6.0/5.0) < 1e-4);
}
|
CPP/5_spans_3
| 3
|
/*
Insert a number "delimeter" between every two consecutive elements of input vector `numbers"
>>> intersperse({}, 4)
{}
>>> intersperse({1, 2, 3}, 4)
{1, 4, 2, 4, 3}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> intersperse(vector<int> numbers, int delimeter){
|
#include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<int> intersperse(vector<int> numbers, int delimeter){
|
[
" vect",
"t> out={};\n ",
";i++)\n",
"[i]);\n\n }\n return out;\n}\n"
] |
[
"or<in",
" if (numbers.size()>0) out.push_back(numbers[0]);\n for (int i=1;i<numbers.size()",
" {\n out.push_back(delimeter);\n out.push_back(numbers"
] |
vector<int> out={};
if (numbers.size()>0) out.push_back(numbers[0]);
for (int i=1;i<numbers.size();i++)
{
out.push_back(delimeter);
out.push_back(numbers[i]);
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(intersperse({}, 7), {}));
assert (issame(intersperse({5, 6, 3, 2}, 8),{5, 8, 6, 8, 3, 8, 2}));
assert (issame(intersperse({2, 2, 2}, 2),{2, 2, 2, 2, 2}));
}
|
CPP/6_spans_3
| 3
|
/*
Input to this function is a string represented multiple groups for nested parentheses separated by spaces.
For each of the group, output the deepest level of nesting of parentheses.
E.g. (()()) has maximum two levels of nesting while ((())) has three.
>>> parse_nested_parens("(()()) ((())) () ((())()())")
{2, 3, 1, 3}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<int> parse_nested_parens(string paren_string){
|
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<int> parse_nested_parens(string paren_string){
|
[
" ",
"r;\n ",
"+)\n {\n chr=paren_string[i];\n if (chr=='(')\n {\n level+=1;\n if (level>max_level) max_level=level;\n current_paren+=chr;\n }\n if (chr==')')\n {\n ",
"\n all_levels.push_back(max_level);\n current_paren=\"\";\n max_level=0;\n }\n }\n }\n return all_levels;\n}\n"
] |
[
" vector<int> all_levels;\n string current_paren;\n int level=0,max_level=0;\n char ch",
" int i;\n for (i=0;i<paren_string.length();i+",
" level-=1;\n current_paren+=chr;\n if (level==0){"
] |
vector<int> all_levels;
string current_paren;
int level=0,max_level=0;
char chr;
int i;
for (i=0;i<paren_string.length();i++)
{
chr=paren_string[i];
if (chr=='(')
{
level+=1;
if (level>max_level) max_level=level;
current_paren+=chr;
}
if (chr==')')
{
level-=1;
current_paren+=chr;
if (level==0){
all_levels.push_back(max_level);
current_paren="";
max_level=0;
}
}
}
return all_levels;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(parse_nested_parens("(()()) ((())) () ((())()())"),{2, 3, 1, 3}));
assert (issame(parse_nested_parens("() (()) ((())) (((())))") , {1, 2, 3, 4}));
assert (issame(parse_nested_parens("(()(())((())))") ,{4}));
}
|
CPP/7_spans_3
| 3
|
/*
Filter an input vector of strings only for ones that contain given substring
>>> filter_by_substring({}, "a")
{}
>>> filter_by_substring({"abc", "bacd", "cde", "vector"}, "a")
{"abc", "bacd", "vector"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> filter_by_substring(vector<string> strings, string substring){
|
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<string> filter_by_substring(vector<string> strings, string substring){
|
[
" vector<string> ",
"t i=0;i<st",
"ngs[i].npo",
"gs[i]);\n }\n return out;\n}\n"
] |
[
"out;\n for (in",
"rings.size();i++)\n {\n if (strings[i].find(substring)!=stri",
"s)\n out.push_back(strin"
] |
vector<string> out;
for (int i=0;i<strings.size();i++)
{
if (strings[i].find(substring)!=strings[i].npos)
out.push_back(strings[i]);
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(filter_by_substring({}, "john"),{}));
assert (issame(filter_by_substring({"xxx", "asd", "xxy", "john doe", "xxxAAA", "xxx"}, "xxx"), {"xxx", "xxxAAA", "xxx"}));
assert (issame(filter_by_substring({"xxx", "asd", "aaaxxy", "john doe", "xxxAAA", "xxx"}, "xx"),{"xxx", "aaaxxy", "xxxAAA", "xxx"}));
assert (issame(filter_by_substring({"grunt", "trumpet", "prune", "gruesome"}, "run") ,{"grunt", "prune"}));
}
|
CPP/8_spans_3
| 3
|
/*
For a given vector of integers, return a vector consisting of a sum and a product of all the integers in a vector.
Empty sum should be equal to 0 and empty product should be equal to 1.
>>> sum_product({})
(0, 1)
>>> sum_product({1, 2, 3, 4})
(10, 24)
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> sum_product(vector<int> numbers){
|
#include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<int> sum_product(vector<int> numbers){
|
[
" int sum=0,p",
");i++)\n",
"mbers[i]",
" {sum,product};\n}\n"
] |
[
"roduct=1;\n for (int i=0;i<numbers.size(",
" {\n sum+=numbers[i];\n product*=nu",
";\n }\n return"
] |
int sum=0,product=1;
for (int i=0;i<numbers.size();i++)
{
sum+=numbers[i];
product*=numbers[i];
}
return {sum,product};
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(sum_product({}) ,{0, 1}));
assert (issame(sum_product({1, 1, 1}), {3, 1}));
assert (issame(sum_product({100, 0}),{100, 0}));
assert (issame(sum_product({3, 5, 7}) , {3 + 5 + 7, 3 * 5 * 7}));
assert (issame(sum_product({10}) ,{10, 10}));
}
|
CPP/9_spans_3
| 3
|
/*
From a given vector of integers, generate a vector of rolling maximum element found until given moment
in the sequence.
>>> rolling_max({1, 2, 3, 2, 3, 4, 2})
{1, 2, 3, 3, 3, 4, 4}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> rolling_max(vector<int> numbers){
|
#include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<int> rolling_max(vector<int> numbers){
|
[
" ",
"nt> out;\n int",
".size();i++)\n ",
" }\n return out;\n}\n"
] |
[
" vector<i",
" max=0;\n for (int i=0;i<numbers",
"{\n if (numbers[i]>max) max=numbers[i];\n out.push_back(max);\n "
] |
vector<int> out;
int max=0;
for (int i=0;i<numbers.size();i++)
{
if (numbers[i]>max) max=numbers[i];
out.push_back(max);
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(rolling_max({}),{}));
assert (issame(rolling_max({1, 2, 3, 4}) , {1, 2, 3, 4}));
assert (issame(rolling_max({4, 3, 2, 1}),{4, 4, 4, 4}));
assert (issame(rolling_max({3, 2, 3, 100, 3}) ,{3, 3, 3, 100, 100}));
}
|
CPP/10_spans_3
| 3
|
#include<stdio.h>
#include<string>
using namespace std;
bool is_palindrome(string str){
//Test if given string is a palindrome
string s(str.rbegin(),str.rend());
return s==str;
}
string make_palindrome(string str){
/*
Find the shortest palindrome that begins with a supplied string.
Algorithm idea is simple: - Find the longest postfix of supplied string that is a palindrome.
- Append to the end of the string reverse of a string prefix that comes before the palindromic suffix.
>>> make_palindrome("")
""
>>> make_palindrome("cat")
"catac"
>>> make_palindrome("cata")
"catac"
*/
|
#include<stdio.h>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
bool is_palindrome(string str){
string s(str.rbegin(),str.rend());
return s==str;
}
string make_palindrome(string str){
|
[
"",
"ngth();i++)\n {\n string rstr=str.substr(i);\n if (is_palin",
" string n2str(nstr.rbegin(),nstr.rend());\n ",
"rn str+n2str;\n }\n }\n string n2str(str.rbegin(),str.rend());\n return str+n2str;\n}\n"
] |
[
" int i;\n for (i=0;i<str.le",
"drome(rstr))\n {\n string nstr;\n nstr=str.substr(0,i);\n",
" retu"
] |
int i;
for (i=0;i<str.length();i++)
{
string rstr=str.substr(i);
if (is_palindrome(rstr))
{
string nstr;
nstr=str.substr(0,i);
string n2str(nstr.rbegin(),nstr.rend());
return str+n2str;
}
}
string n2str(str.rbegin(),str.rend());
return str+n2str;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (make_palindrome("") == "");
assert (make_palindrome("x") == "x");
assert (make_palindrome("xyz") == "xyzyx");
assert (make_palindrome("xyx") == "xyx") ;
assert (make_palindrome("jerry") == "jerryrrej");
}
|
CPP/11_spans_3
| 3
|
/*
Input are two strings a and b consisting only of 1s and 0s.
Perform binary XOR on these inputs and return result also as a string.
>>> string_xor("010", "110")
"100"
*/
#include<stdio.h>
#include<string>
using namespace std;
string string_xor(string a,string b){
|
#include<stdio.h>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
string string_xor(string a,string b){
|
[
"",
"th());i++)\n {\n if (i<a.length() ",
" {\n ",
"\n }\n else\n {\n if (i>=a.length()) \n {\n output+=b[i];\n }\n else output+=a[i];\n }\n }\n return output;\n}\n"
] |
[
" string output=\"\";\n for (int i=0;(i<a.length() and i<b.leng",
"and i<b.length())\n {\n if (a[i]== b[i]) \n ",
"output+='0';\n } \n else output+='1';"
] |
string output="";
for (int i=0;(i<a.length() and i<b.length());i++)
{
if (i<a.length() and i<b.length())
{
if (a[i]== b[i])
{
output+='0';
}
else output+='1';
}
else
{
if (i>=a.length())
{
output+=b[i];
}
else output+=a[i];
}
}
return output;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (string_xor("111000", "101010") == "010010");
assert (string_xor("1", "1") == "0");
assert (string_xor("0101", "0000") == "0101");
}
|
CPP/12_spans_3
| 3
|
/*
Out of vector of strings, return the longest one. Return the first one in case of multiple
strings of the same length. Return None in case the input vector is empty.
>>> longest({})
>>> longest({"a", "b", "c"})
"a"
>>> longest({"a", "bb", "ccc"})
"ccc"
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
string longest(vector<string> strings){
|
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
string longest(vector<string> strings){
|
[
" string out;\n for (int i=0;i",
"e();i++)\n ",
">out.length()) out=str",
"}\n"
] |
[
"<strings.siz",
" {\n if (strings[i].length()",
"ings[i];\n }\n return out;\n"
] |
string out;
for (int i=0;i<strings.size();i++)
{
if (strings[i].length()>out.length()) out=strings[i];
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (longest({}) == "");
assert (longest({"x", "y", "z"}) == "x");
assert (longest({"x", "yyy", "zzzz", "www", "kkkk", "abc"}) == "zzzz");
}
|
CPP/13_spans_3
| 3
|
/*
Return a greatest common divisor of two integers a and b
>>> greatest_common_divisor(3, 5)
1
>>> greatest_common_divisor(25, 15)
5
*/
#include<stdio.h>
using namespace std;
int greatest_common_divisor(int a, int b){
|
#include<stdio.h>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
int greatest_common_divisor(int a, int b){
|
[
" i",
"hile (",
" a",
" (a==0) return b;\n }\n}\n"
] |
[
"nt out,m;\n w",
"true){\n if (a<b) \n {\n m=a;a=b;b=m;\n }\n ",
"=a%b;\n if"
] |
int out,m;
while (true){
if (a<b)
{
m=a;a=b;b=m;
}
a=a%b;
if (a==0) return b;
}
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (greatest_common_divisor(3, 7) == 1);
assert (greatest_common_divisor(10, 15) == 5);
assert (greatest_common_divisor(49, 14) == 7);
assert (greatest_common_divisor(144, 60) == 12);
}
|
CPP/14_spans_3
| 3
|
/*
Return vector of all prefixes from shortest to longest of the input string
>>> all_prefixes("abc")
{"a", "ab", "abc"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> all_prefixes(string str){
|
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<string> all_prefixes(string str){
|
[
" vector<",
" string ",
" current=current+str[i];\n out.",
"rent);\n }\n return out;\n}\n"
] |
[
"string> out;\n ",
"current=\"\";\n for (int i=0;i<str.length();i++)\n {\n ",
"push_back(cur"
] |
vector<string> out;
string current="";
for (int i=0;i<str.length();i++)
{
current=current+str[i];
out.push_back(current);
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(all_prefixes(""),{}));
assert (issame(all_prefixes("asdfgh") ,{"a", "as", "asd", "asdf", "asdfg", "asdfgh"}));
assert (issame(all_prefixes("WWW") ,{"W", "WW", "WWW"}));
}
|
CPP/15_spans_3
| 3
|
/*
Return a string containing space-delimited numbers starting from 0 upto n inclusive.
>>> string_sequence(0)
"0"
>>> string_sequence(5)
"0 1 2 3 4 5"
*/
#include<stdio.h>
#include<string>
using namespace std;
string string_sequence(int n){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string string_sequence(int n){
|
[
" stri",
"ring(i);",
"",
"out;\n}\n"
] |
[
"ng out=\"0\";\n for (int i=1;i<=n;i++)\n out=out+\" \"+to_st",
"\n r",
"return "
] |
string out="0";
for (int i=1;i<=n;i++)
out=out+" "+to_string(i);
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (string_sequence(0) == "0");
assert (string_sequence(3) == "0 1 2 3");
assert (string_sequence(10) == "0 1 2 3 4 5 6 7 8 9 10");
}
|
CPP/16_spans_3
| 3
|
/*
Given a string, find out how many distinct characters (regardless of case) does it consist of
>>> count_distinct_characters("xyzXYZ")
3
>>> count_distinct_characters("Jerry")
4
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int count_distinct_characters(string str){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int count_distinct_characters(string str){
|
[
" vector<char> distinct={};\n transform(str.begin(),str.end(),str.begin(),::tolower);\n for (int",
"(int j=0;j<distinct.size();j++)\n ",
"se) distinc",
"\n}\n"
] |
[
" i=0;i<str.size();i++)\n {\n bool isin=false;\n for ",
" if (distinct[j]==str[i])\n isin=true;\n if (isin==fal",
"t.push_back(str[i]);\n\n }\n return distinct.size();"
] |
vector<char> distinct={};
transform(str.begin(),str.end(),str.begin(),::tolower);
for (int i=0;i<str.size();i++)
{
bool isin=false;
for (int j=0;j<distinct.size();j++)
if (distinct[j]==str[i])
isin=true;
if (isin==false) distinct.push_back(str[i]);
}
return distinct.size();
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (count_distinct_characters("") == 0);
assert (count_distinct_characters("abcde") == 5);
assert (count_distinct_characters("abcdecadeCADE") == 5);
assert (count_distinct_characters("aaaaAAAAaaaa") == 1);
assert (count_distinct_characters("Jerry jERRY JeRRRY") == 5);
}
|
CPP/17_spans_3
| 3
|
/*
Input to this function is a string representing musical notes in a special ASCII format.
Your task is to parse this string and return vector of integers corresponding to how many beats does each
not last.
Here is a legend:
"o" - whole note, lasts four beats
"o|" - half note, lasts two beats
".|" - quater note, lasts one beat
>>> parse_music("o o| .| o| o| .| .| .| .| o o")
{4, 2, 1, 2, 2, 1, 1, 1, 1, 4, 4}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<int> parse_music(string music_string){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> parse_music(string music_string){
|
[
" string current=\"\";\n vector<int> out={};\n if (music_string.length()>0)\n music_st",
" i=0;i<music_string.length();i++)\n {\n if (music_string[i]==' ')\n {\n if (current==\"o\") out.push_back(4);\n",
"h_back(2);\n if (current==\".|\") out.push_back(1);\n current=\"\";\n }\n else curre",
" }\n return out;\n}\n"
] |
[
"ring=music_string+' ';\n for (int",
" if (current==\"o|\") out.pus",
"nt+=music_string[i];\n "
] |
string current="";
vector<int> out={};
if (music_string.length()>0)
music_string=music_string+' ';
for (int i=0;i<music_string.length();i++)
{
if (music_string[i]==' ')
{
if (current=="o") out.push_back(4);
if (current=="o|") out.push_back(2);
if (current==".|") out.push_back(1);
current="";
}
else current+=music_string[i];
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(parse_music("") , {}));
assert (issame(parse_music("o o o o") ,{4, 4, 4, 4}));
assert (issame(parse_music(".| .| .| .|") , {1, 1, 1, 1}));
assert (issame(parse_music("o| o| .| .| o o o o") , {2, 2, 1, 1, 4, 4, 4, 4}));
assert (issame(parse_music("o| .| o| .| o o| o o|") , {2, 1, 2, 1, 4, 2, 4, 2}));
}
|
CPP/18_spans_3
| 3
|
/*
Find how many times a given substring can be found in the original string. Count overlaping cases.
>>> how_many_times("", "a")
0
>>> how_many_times("aaa", "a")
3
>>> how_many_times("aaaa", "aa")
3
*/
#include<stdio.h>
#include<string>
using namespace std;
int how_many_times(string str,string substring){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int how_many_times(string str,string substring){
|
[
" i",
"0;\n if (s",
"str(i",
"turn out;\n}\n"
] |
[
"nt out=",
"tr.length()==0) return 0;\n for (int i=0;i<=str.length()-substring.length();i++)\n if (str.sub",
",substring.length())==substring)\n out+=1;\n re"
] |
int out=0;
if (str.length()==0) return 0;
for (int i=0;i<=str.length()-substring.length();i++)
if (str.substr(i,substring.length())==substring)
out+=1;
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (how_many_times("", "x") == 0);
assert (how_many_times("xyxyxyx", "x") == 4);
assert (how_many_times("cacacacac", "cac") == 4);
assert (how_many_times("john doe", "john") == 1);
}
|
CPP/19_spans_3
| 3
|
/*
Input is a space-delimited string of numberals from "zero" to "nine".
Valid choices are "zero", "one", 'two", 'three", "four", "five", 'six", 'seven", "eight" and "nine".
Return the string with numbers sorted from smallest to largest
>>> sort_numbers('three one five")
"one three five"
*/
#include<stdio.h>
#include<string>
#include<map>
using namespace std;
string sort_numbers(string numbers){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<map>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string sort_numbers(string numbers){
|
[
" map<string,int> tonum={{\"zero\",0},{\"one\",1},{\"two\",2},{\"three\",3},{\"four\",4},{\"five\",5},{\"six\",6},{\"seven\",7},{\"eight\",8},{\"nine\",9}};\n map<int,string> numto={{0,\"zero\"}",
"\"},{2,\"two\"},{3,\"three\"},{4,\"four\"},{5,\"five\"},{6,\"six\"},{7,\"seven\"},{8,\"eight\"},{9,\"nine\"}};\n int count[10];\n for (int i=0;i<10;i++)\n count[i]=0;\n string out=\"\",current=\"\";\n if (numbers.length()>0) numbers=numbers+' ';\n for (int i=0;i<numbers.length();i++)\n if (numbers[i]==' ')\n {\n count[tonum[current]]+=1;\n current=\"\";\n }\n else current+=numbers[i];",
"or (int i=0;i<10;i++)\n for (int j=0;j<count[i];j++)\n out=out+numto[i]+' ';\n if (o",
"pop_back();\n return out;\n}\n"
] |
[
",{1,\"one",
"\n f",
"ut.length()>0) out."
] |
map<string,int> tonum={{"zero",0},{"one",1},{"two",2},{"three",3},{"four",4},{"five",5},{"six",6},{"seven",7},{"eight",8},{"nine",9}};
map<int,string> numto={{0,"zero"},{1,"one"},{2,"two"},{3,"three"},{4,"four"},{5,"five"},{6,"six"},{7,"seven"},{8,"eight"},{9,"nine"}};
int count[10];
for (int i=0;i<10;i++)
count[i]=0;
string out="",current="";
if (numbers.length()>0) numbers=numbers+' ';
for (int i=0;i<numbers.length();i++)
if (numbers[i]==' ')
{
count[tonum[current]]+=1;
current="";
}
else current+=numbers[i];
for (int i=0;i<10;i++)
for (int j=0;j<count[i];j++)
out=out+numto[i]+' ';
if (out.length()>0) out.pop_back();
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (sort_numbers("") == "");
assert (sort_numbers("three") == "three");
assert (sort_numbers("three five nine") == "three five nine");
assert (sort_numbers("five zero four seven nine eight") == "zero four five seven eight nine");
assert (sort_numbers("six five four three two one zero") == "zero one two three four five six");
}
|
CPP/20_spans_3
| 3
|
/*
From a supplied vector of numbers (of length at least two) select and return two that are the closest to each
other and return them in order (smaller number, larger number).
>>> find_closest_elements({1.0, 2.0, 3.0, 4.0, 5.0, 2.2})
(2.0, 2.2)
>>> find_closest_elements({1.0, 2.0, 3.0, 4.0, 5.0, 2.0})
(2.0, 2.0)
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
vector<float> find_closest_elements(vector<float> numbers){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<float> find_closest_elements(vector<float> numbers){
|
[
" vector<float> out={};\n for (int i=0;i<numbers.size(",
"ers[i]-numbers[j])<abs(ou",
"]>out[1])\n out=",
"urn out;\n}\n"
] |
[
");i++)\n for (int j=i+1;j<numbers.size();j++)\n if (out.size()==0 or abs(numb",
"t[0]-out[1]))\n out={numbers[i],numbers[j]};\n if (out[0",
"{out[1],out[0]};\n ret"
] |
vector<float> out={};
for (int i=0;i<numbers.size();i++)
for (int j=i+1;j<numbers.size();j++)
if (out.size()==0 or abs(numbers[i]-numbers[j])<abs(out[0]-out[1]))
out={numbers[i],numbers[j]};
if (out[0]>out[1])
out={out[1],out[0]};
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<float> a,vector<float>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (abs(a[i]-b[i])>1e-4) return false;
}
return true;
}
int main(){
assert (issame(find_closest_elements({1.0, 2.0, 3.9, 4.0, 5.0, 2.2}) , {3.9, 4.0}));
assert (issame(find_closest_elements({1.0, 2.0, 5.9, 4.0, 5.0}) , {5.0, 5.9} ));
assert (issame(find_closest_elements({1.0, 2.0, 3.0, 4.0, 5.0, 2.2}) ,{2.0, 2.2}));
assert (issame(find_closest_elements({1.0, 2.0, 3.0, 4.0, 5.0, 2.0}) ,{2.0, 2.0}));
assert (issame(find_closest_elements({1.1, 2.2, 3.1, 4.1, 5.1}) , {2.2, 3.1}));
}
|
CPP/21_spans_3
| 3
|
/*
Given vector of numbers (of at least two elements), apply a linear transform to that vector,
such that the smallest number will become 0 and the largest will become 1
>>> rescale_to_unit({1.0, 2.0, 3.0, 4.0, 5.0})
{0.0, 0.25, 0.5, 0.75, 1.0}
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
vector<float> rescale_to_unit(vector<float> numbers){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<float> rescale_to_unit(vector<float> numbers){
|
[
" ",
"00,max=-100000;\n",
"mbers[i];\n if (numbers[i]>max) ma",
"ers[i]-min)/(max-min);\n return numbers;\n}\n"
] |
[
"float min=1000",
" for (int i=0;i<numbers.size();i++)\n {\n if (numbers[i]<min) min=nu",
"x=numbers[i];\n }\n for (int i=0;i<numbers.size();i++)\n numbers[i]=(numb"
] |
float min=100000,max=-100000;
for (int i=0;i<numbers.size();i++)
{
if (numbers[i]<min) min=numbers[i];
if (numbers[i]>max) max=numbers[i];
}
for (int i=0;i<numbers.size();i++)
numbers[i]=(numbers[i]-min)/(max-min);
return numbers;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<float> a,vector<float>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (abs(a[i]-b[i])>1e-4) return false;
}
return true;
}
int main(){
assert (issame(rescale_to_unit({2.0, 49.9}) , {0.0, 1.0}));
assert (issame(rescale_to_unit({100.0, 49.9}) ,{1.0, 0.0}));
assert (issame(rescale_to_unit({1.0, 2.0, 3.0, 4.0, 5.0}) , {0.0, 0.25, 0.5, 0.75, 1.0}));
assert (issame(rescale_to_unit({2.0, 1.0, 5.0, 3.0, 4.0}) , {0.25, 0.0, 1.0, 0.5, 0.75}));
assert (issame(rescale_to_unit({12.0, 11.0, 15.0, 13.0, 14.0}) ,{0.25, 0.0, 1.0, 0.5, 0.75}));
}
|
CPP/22_spans_3
| 3
|
/*
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){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<boost/any.hpp>
#include<list>
typedef std::list<boost::any> list_any;
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> filter_integers(list_any values){
|
[
" li",
" for (it=values.begin();it",
" ",
"return out;\n}\n"
] |
[
"st_any::iterator it;\n boost::any anyone;\n vector<int> out;\n ",
"!=values.end();it++)\n {\n anyone=*it;\n if( anyone.type() == typeid(int) )\n ",
" out.push_back(boost::any_cast<int>(*it));\n }\n "
] |
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;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(filter_integers({}),{}));
assert (issame(filter_integers({4, {},23.2, 9, string("adasd")}) ,{4, 9}));
assert (issame(filter_integers({3, 'c', 3, 3, 'a', 'b'}) ,{3, 3, 3}));
}
|
CPP/25_spans_3
| 3
|
/*
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){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> factorize(int n){
|
[
" ",
"0)\n ",
" }\n",
"\n"
] |
[
" vector<int> out={};\n for (int i=2;i*i<=n;i++)\n if (n%i==",
" {\n n=n/i;\n out.push_back(i);\n i-=1;\n ",
" out.push_back(n);\n return out;\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;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(factorize(2) , {2}));
assert (issame(factorize(4) , {2, 2}));
assert (issame(factorize(8) , {2, 2, 2}));
assert (issame(factorize(3 * 19) , {3, 19}));
assert (issame(factorize(3 * 19 * 3 * 19) , {3, 3, 19, 19}));
assert (issame(factorize(3 * 19 * 3 * 19 * 3 * 19) , {3, 3, 3, 19, 19, 19}));
assert (issame(factorize(3 * 19 * 19 * 19) , {3, 19, 19, 19}));
assert (issame(factorize(3 * 2 * 3) , {2, 3, 3}));
}
|
CPP/26_spans_3
| 3
|
/*
From a vector of integers, remove all elements that occur more than once.
Keep order of elements left the same as in the input.
>>> remove_duplicates({1, 2, 3, 2, 4})
{1, 3, 4}
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> remove_duplicates(vector<int> numbers){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> remove_duplicates(vector<int> numbers){
|
[
" vector<int> out={};\n vector<int> has1={}",
"if (find(has2.begin(),has2.end(),numbers[i])!=has2.end()) continue;\n if (find(has1.begin(),has1.end(),numbers[i])!=has1.end())\n {\n\n ",
"(numb",
"i=0;i<numbers.size();i++)\n if (find(has2.begin(),has2.end(),numbers[i])==has2.end())\n out.push_back(numbers[i]);\n return out;\n\n\n}\n"
] |
[
";\n vector<int> has2={};\n for (int i=0;i<numbers.size();i++)\n {\n ",
" has2.push_back(numbers[i]);\n }\n else has1.push_back",
"ers[i]);\n }\n for (int "
] |
vector<int> out={};
vector<int> has1={};
vector<int> has2={};
for (int i=0;i<numbers.size();i++)
{
if (find(has2.begin(),has2.end(),numbers[i])!=has2.end()) continue;
if (find(has1.begin(),has1.end(),numbers[i])!=has1.end())
{
has2.push_back(numbers[i]);
}
else has1.push_back(numbers[i]);
}
for (int i=0;i<numbers.size();i++)
if (find(has2.begin(),has2.end(),numbers[i])==has2.end())
out.push_back(numbers[i]);
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(remove_duplicates({}) , {}));
assert (issame(remove_duplicates({1, 2, 3, 4}) , {1, 2, 3, 4}));
assert (issame(remove_duplicates({1, 2, 3, 2, 4, 3, 5}) , {1, 4, 5}));
}
|
CPP/27_spans_3
| 3
|
/*
For a given string, flip lowercase characters to uppercase and uppercase to lowercase.
>>> flip_case("Hello")
"hELLO"
*/
#include<stdio.h>
#include<string>
using namespace std;
string filp_case(string str){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string filp_case(string str){
|
[
" string out=\"\";\n for (int i=",
" char w=",
" if (w>=65 and w<=90){ w+=32;}\n ",
"}\n"
] |
[
"0;i<str.length();i++)\n {\n ",
"str[i];\n if (w>=97 and w<=122) {w-=32;}\n else\n",
" out=out+w;\n }\n return out;\n"
] |
string out="";
for (int i=0;i<str.length();i++)
{
char w=str[i];
if (w>=97 and w<=122) {w-=32;}
else
if (w>=65 and w<=90){ w+=32;}
out=out+w;
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (filp_case("") == "");
assert (filp_case("Hello!") == "hELLO!");
assert (filp_case("These violent delights have violent ends") == "tHESE VIOLENT DELIGHTS HAVE VIOLENT ENDS");
}
|
CPP/28_spans_3
| 3
|
/*
Concatenate vector of strings into a single string
>>> concatenate({})
""
>>> concatenate({"a", "b", "c"})
"abc"
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
string concatenate(vector<string> strings){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string concatenate(vector<string> strings){
|
[
" str",
"t",
"",
"\n"
] |
[
"ing ou",
"=\"\";\n ",
" for (int i=0;i<strings.size();i++)\n out=out+strings[i];\n return out;\n}"
] |
string out="";
for (int i=0;i<strings.size();i++)
out=out+strings[i];
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (concatenate({}) == "");
assert (concatenate({"x", "y", "z"}) == "xyz");
assert (concatenate({"x", "y", "z", "w", "k"}) == "xyzwk");
}
|
CPP/29_spans_3
| 3
|
/*
Filter an input vector of strings only for ones that start with a given prefix.
>>> filter_by_prefix({}, "a")
{}
>>> filter_by_prefix({"abc", "bcd", "cde", "vector"}, "a")
{"abc", "vector"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> filter_by_prefix(vector<string> strings, string prefix){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<string> filter_by_prefix(vector<string> strings, string prefix){
|
[
" vector<string> out={};",
"int i=0;i<strings.size();i++)\n if (",
"(0,prefix.",
""
] |
[
"\n for (",
"strings[i].substr",
"length())==prefix) out.push_back(strings[i]);\n return out;\n}\n"
] |
vector<string> out={};
for (int i=0;i<strings.size();i++)
if (strings[i].substr(0,prefix.length())==prefix) out.push_back(strings[i]);
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(filter_by_prefix({}, "john") , {}));
assert (issame(filter_by_prefix({"xxx", "asd", "xxy", "john doe", "xxxAAA", "xxx"}, "xxx") , {"xxx", "xxxAAA", "xxx"}));
}
|
CPP/30_spans_3
| 3
|
/*
Return only positive numbers in the vector.
>>> get_positive({-1, 2, -4, 5, 6})
{2, 5, 6}
>>> get_positive({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10})
{5, 3, 2, 3, 9, 123, 1}
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
vector<float> get_positive(vector<float> l){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<float> get_positive(vector<float> l){
|
[
"",
"",
"t.pu",
"ut;\n}\n"
] |
[
" vector<float> o",
"out={};\n for (int i=0;i<l.size();i++)\n if (l[i]>0) ou",
"sh_back(l[i]);\n return o"
] |
vector<float> out={};
for (int i=0;i<l.size();i++)
if (l[i]>0) out.push_back(l[i]);
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<float> a,vector<float>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (abs(a[i]-b[i])>1e-4) return false;
}
return true;
}
int main(){
assert (issame(get_positive({-1, -2, 4, 5, 6}) , {4, 5, 6} ));
assert (issame(get_positive({5, 3, -5, 2, 3, 3, 9, 0, 123, 1, -10}) , {5, 3, 2, 3, 3, 9, 123, 1}));
assert (issame(get_positive({-1, -2}) , {} ));
assert (issame(get_positive({}) , {}));
}
|
CPP/31_spans_3
| 3
|
/*
Return true if a given number is prime, and false otherwise.
>>> is_prime(6)
false
>>> is_prime(101)
true
>>> is_prime(11)
true
>>> is_prime(13441)
true
>>> is_prime(61)
true
>>> is_prime(4)
false
>>> is_prime(1)
false
*/
#include<stdio.h>
using namespace std;
bool is_prime(long long n){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_prime(long long n){
|
[
" if (n<2)",
"ong i=2;i*",
"i==0) return ",
"turn true;\n}\n"
] |
[
" return false;\n for (long l",
"i<=n;i++)\n if (n%",
"false;\n re"
] |
if (n<2) return false;
for (long long i=2;i*i<=n;i++)
if (n%i==0) return false;
return true;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (is_prime(6) == false);
assert (is_prime(101) == true);
assert (is_prime(11) == true);
assert (is_prime(13441) == true);
assert (is_prime(61) == true);
assert (is_prime(4) == false);
assert (is_prime(1) == false);
assert (is_prime(5) == true);
assert (is_prime(11) == true);
assert (is_prime(17) == true);
assert (is_prime(5 * 17) == false);
assert (is_prime(11 * 7) == false);
assert (is_prime(13441 * 19) == false);
}
|
CPP/32_spans_3
| 3
|
#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
*/
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
double poly(vector<double> xs, double x){
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){
|
[
" double ans=0;\n double value;\n value=poly(xs,ans);\n while (abs(value)>1e-6)\n {\n ",
"0;\n for (int i=1;i<xs.size();i++)\n",
"\n driv+=xs[i]*pow(ans,i-1)*i;\n }\n ans=ans-value/driv;\n value=poly",
"\n return ans;\n\n}\n"
] |
[
" double driv=",
" {",
"(xs,ans);\n }"
] |
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;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
double solution;
int ncoeff;
for (int i=0;i<100;i++)
{
ncoeff = 2 * (1+rand()%4);
vector<double> coeffs = {};
for (int j=0;j<ncoeff;j++)
{
double coeff = -10+rand()%21;
if (coeff == 0) coeff = 1;
coeffs.push_back(coeff);
}
solution = find_zero(coeffs);
assert (abs(poly(coeffs, solution))< 1e-3);
}
}
|
CPP/33_spans_3
| 3
|
/*
This function takes a vector l and returns a vector l' such that
l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal
to the values of the corresponding indicies of l, but sorted.
>>> sort_third({1, 2, 3})
{1, 2, 3}
>>> sort_third({5, 6, 3, 4, 8, 9, 2})
{2, 6, 3, 4, 8, 9, 5}
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> sort_third(vector<int> l){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> sort_third(vector<int> l){
|
[
" vector<int> third={};\n int i;\n for (i=0;i*3<l.size();i++)\n third.push_back(l[i*3]);\n ",
"d.begin(),third.end());\n\n vector<int> out={};\n for (i=",
" {\n if (i%3==0) {out.push_back(thir",
"ck(l[i]);\n }\n return out;\n\n}\n"
] |
[
"\n sort(thir",
"0;i<l.size();i++)\n ",
"d[i/3]);}\n else out.push_ba"
] |
vector<int> third={};
int i;
for (i=0;i*3<l.size();i++)
third.push_back(l[i*3]);
sort(third.begin(),third.end());
vector<int> out={};
for (i=0;i<l.size();i++)
{
if (i%3==0) {out.push_back(third[i/3]);}
else out.push_back(l[i]);
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(sort_third({1, 2, 3}) , sort_third({1, 2, 3})));
assert (issame(sort_third({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10}) , sort_third({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10})));
assert (issame(sort_third({5, 8, -12, 4, 23, 2, 3, 11, 12, -10}) , sort_third({5, 8, -12, 4, 23, 2, 3, 11, 12, -10})));
assert (issame(sort_third({5, 6, 3, 4, 8, 9, 2}) , {2, 6, 3, 4, 8, 9, 5}));
assert (issame(sort_third({5, 8, 3, 4, 6, 9, 2}) , {2, 8, 3, 4, 6, 9, 5}));
assert (issame(sort_third({5, 6, 9, 4, 8, 3, 2}) , {2, 6, 9, 4, 8, 3, 5}));
assert (issame(sort_third({5, 6, 3, 4, 8, 9, 2, 1}) , {2, 6, 3, 4, 8, 9, 5, 1}));
}
|
CPP/34_spans_3
| 3
|
/*
Return sorted unique elements in a vector
>>> unique({5, 3, 5, 2, 3, 3, 9, 0, 123})
{0, 2, 3, 5, 9, 123}
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> unique(vector<int> l){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> unique(vector<int> l){
|
[
" vector<int> out={};\n for (in",
"nd(out.beg",
"nd()",
"t.begin(),out.end());\n return out;\n}\n"
] |
[
"t i=0;i<l.size();i++)\n if (fi",
"in(),out.e",
",l[i])==out.end())\n out.push_back(l[i]);\n sort(ou"
] |
vector<int> out={};
for (int i=0;i<l.size();i++)
if (find(out.begin(),out.end(),l[i])==out.end())
out.push_back(l[i]);
sort(out.begin(),out.end());
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(unique({5, 3, 5, 2, 3, 3, 9, 0, 123}) , {0, 2, 3, 5, 9, 123}));
}
|
CPP/35_spans_3
| 3
|
/*
Return maximum element in the vector.
>>> max_element({1, 2, 3})
3
>>> max_element({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10})
123
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
float max_element(vector<float> l){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
float max_element(vector<float> l){
|
[
" ",
"",
"r",
"ax;\n\n}\n"
] |
[
" float max=-1000",
"00;\n fo",
" (int i=0;i<l.size();i++)\n if (max<l[i]) max=l[i];\n return m"
] |
float max=-10000;
for (int i=0;i<l.size();i++)
if (max<l[i]) max=l[i];
return max;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (abs(max_element({1, 2, 3})- 3)<1e-4);
assert (abs(max_element({5, 3, -5, 2, -3, 3, 9, 0, 124, 1, -10})- 124)<1e-4);
}
|
CPP/36_spans_3
| 3
|
/*
Return the number of times the digit 7 appears in integers less than n which are divisible by 11 or 13.
>>> fizz_buzz(50)
0
>>> fizz_buzz(78)
2
>>> fizz_buzz(79)
3
*/
#include<stdio.h>
using namespace std;
int fizz_buzz(int n){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int fizz_buzz(int n){
|
[
" int ",
";\n for (int i=0;i<n;i++)\n if (",
" int q=i;\n while (q>0)\n {\n ",
" \n return count;\n}\n"
] |
[
"count=0",
"i%11==0 or i%13==0)\n {\n",
" if (q%10==7) count+=1;\n q=q/10;\n }\n }"
] |
int count=0;
for (int i=0;i<n;i++)
if (i%11==0 or i%13==0)
{
int q=i;
while (q>0)
{
if (q%10==7) count+=1;
q=q/10;
}
}
return count;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (fizz_buzz(50) == 0);
assert (fizz_buzz(78) == 2);
assert (fizz_buzz(79) == 3);
assert (fizz_buzz(100) == 3);
assert (fizz_buzz(200) == 6);
assert (fizz_buzz(4000) == 192);
assert (fizz_buzz(10000) == 639);
assert (fizz_buzz(100000) == 8026);
}
|
CPP/37_spans_3
| 3
|
/*
This function takes a vector l and returns a vector l' such that
l' is identical to l in the odd indicies, while its values at the even indicies are equal
to the values of the even indicies of l, but sorted.
>>> sort_even({1, 2, 3})
{1, 2, 3}
>>> sort_even({5, 6, 3, 4})
{3, 6, 5, 4}
*/
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<float> sort_even(vector<float> l){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<float> sort_even(vector<float> l){
|
[
" vector<",
" vector<float> even={};\n for (int i=0;i*2<l.size();i++)\n even.push_back(",
"\n for (int i=0;i<l.size();i++)\n {\n if (i%2==0) out.push_back(even[i/2]);\n if (i%2==1) out.push_bac",
"}\n return out;\n}\n"
] |
[
"float> out={};\n",
"l[i*2]);\n sort(even.begin(),even.end());",
"k(l[i]);\n "
] |
vector<float> out={};
vector<float> even={};
for (int i=0;i*2<l.size();i++)
even.push_back(l[i*2]);
sort(even.begin(),even.end());
for (int i=0;i<l.size();i++)
{
if (i%2==0) out.push_back(even[i/2]);
if (i%2==1) out.push_back(l[i]);
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<float> a,vector<float>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (abs(a[i]-b[i])>1e-4) return false;
}
return true;
}
int main(){
assert (issame(sort_even({1, 2, 3}), {1, 2, 3}));
assert (issame(sort_even({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10}) , {-10, 3, -5, 2, -3, 3, 5, 0, 9, 1, 123}));
assert (issame(sort_even({5, 8, -12, 4, 23, 2, 3, 11, 12, -10}) , {-12, 8, 3, 4, 5, 2, 12, 11, 23, -10}));
}
|
CPP/38_spans_3
| 3
|
#include<stdio.h>
#include<string>
using namespace std;
string encode_cyclic(string s){
// returns encoded string by cycling groups of three characters.
// split string to groups. Each of length 3.
int l=s.length();
int num=(l+2)/3;
string x,output;
int i;
for (i=0;i*3<l;i++)
{
//cycle elements in each group. Unless group has fewer elements than 3.
x=s.substr(i*3,3);
if (x.length()==3) x=x.substr(1)+x[0];
output=output+x;
}
return output;
}
string decode_cyclic(string s){
/*
takes as input string encoded with encode_cyclic function. Returns decoded string.
*/
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string encode_cyclic(string s){
int l=s.length();
int num=(l+2)/3;
string x,output;
int i;
for (i=0;i*3<l;i++)
{
x=s.substr(i*3,3);
if (x.length()==3) x=x.substr(1)+x[0];
output=output+x;
}
return output;
}
string decode_cyclic(string s){
int l=s.length();
int num=(l+2)/3;
string x,output;
int i;
for (i=0;i*3<l;i++)
{
|
[
" int l=s.length();\n i",
" ",
"st",
" output=output+x;\n }\n return output;\n\n\n}\n"
] |
[
"nt num=(l+2)/3;\n string x,output;\n int i;\n for (i=0;i*3<l;i++)\n {\n ",
" x=s.sub",
"r(i*3,3);\n if (x.length()==3) x=x[2]+x.substr(0,2);\n "
] |
int l=s.length();
int num=(l+2)/3;
string x,output;
int i;
for (i=0;i*3<l;i++)
{
x=s.substr(i*3,3);
if (x.length()==3) x=x[2]+x.substr(0,2);
output=output+x;
}
return output;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
for (int i=0;i<100;i++)
{
int l=10+rand()%11;
string str="";
for (int j=0;j<l;j++)
{
char chr=97+rand()%26;
str+=chr;
}
string encoded_str = encode_cyclic(str);
assert (decode_cyclic(encoded_str) == str);
}
}
|
CPP/39_spans_3
| 3
|
/*
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){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int prime_fib(int n){
|
[
" int f1,f2,m;\n f1=1;f2=2;\n int count=",
" bool isprime=true;\n ",
"e; break;\n",
"me) count+=1;\n if (count==n) return f1;\n }\n\n}\n"
] |
[
"0;\n while (count<n)\n {\n f1=f1+f2;\n m=f1;f1=f2;f2=m;\n ",
" for (int w=2;w*w<=f1;w++)\n if (f1%w==0)\n {\n isprime=fals",
" }\n if (ispri"
] |
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;
}
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (prime_fib(1) == 2);
assert (prime_fib(2) == 3);
assert (prime_fib(3) == 5);
assert (prime_fib(4) == 13);
assert (prime_fib(5) == 89);
assert (prime_fib(6) == 233);
assert (prime_fib(7) == 1597);
assert (prime_fib(8) == 28657);
assert (prime_fib(9) == 514229);
assert (prime_fib(10) == 433494437);
}
|
CPP/40_spans_3
| 3
|
/*
triples_sum_to_zero takes a vector of integers as an input.
it returns true if there are three distinct elements in the vector that
sum to zero, and false otherwise.
>>> triples_sum_to_zero({1, 3, 5, 0})
false
>>> triples_sum_to_zero({1, 3, -2, 1})
true
>>> triples_sum_to_zero({1, 2, 3, 7})
false
>>> triples_sum_to_zero({2, 4, -5, 3, 9, 7})
true
>>> triples_sum_to_zero({1})
false
*/
#include<stdio.h>
#include<vector>
using namespace std;
bool triples_sum_to_zero(vector<int> l){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
bool triples_sum_to_zero(vector<int> l){
|
[
" for",
" for (int j=i+1;j<l.size",
" ",
"=0) return true;\n return false;\n}\n"
] |
[
" (int i=0;i<l.size();i++)\n ",
"();j++)\n",
" for (int k=j+1;k<l.size();k++)\n if (l[i]+l[j]+l[k]="
] |
for (int i=0;i<l.size();i++)
for (int j=i+1;j<l.size();j++)
for (int k=j+1;k<l.size();k++)
if (l[i]+l[j]+l[k]==0) return true;
return false;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (triples_sum_to_zero({1, 3, 5, 0}) == false);
assert (triples_sum_to_zero({1, 3, 5, -1}) == false);
assert (triples_sum_to_zero({1, 3, -2, 1}) == true);
assert (triples_sum_to_zero({1, 2, 3, 7}) == false);
assert (triples_sum_to_zero({1, 2, 5, 7}) == false);
assert (triples_sum_to_zero({2, 4, -5, 3, 9, 7}) == true);
assert (triples_sum_to_zero({1}) == false);
assert (triples_sum_to_zero({1, 3, 5, -100}) == false);
assert (triples_sum_to_zero({100, 3, 5, -100}) == false);
}
|
CPP/44_spans_3
| 3
|
/*
Change numerical base of input number x to base.
return string representation after the conversion.
base numbers are less than 10.
>>> change_base(8, 3)
"22"
>>> change_base(8, 2)
"1000"
>>> change_base(7, 2)
"111"
*/
#include<stdio.h>
#include<string>
using namespace std;
string change_base(int x,int base){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string change_base(int x,int base){
|
[
" string ",
" whil",
"",
"return out;\n}\n"
] |
[
"out=\"\";\n ",
"e (x>",
"0)\n {\n out=to_string(x%base)+out;\n x=x/base;\n }\n "
] |
string out="";
while (x>0)
{
out=to_string(x%base)+out;
x=x/base;
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (change_base(8, 3) == "22");
assert (change_base(9, 3) == "100");
assert (change_base(234, 2) == "11101010");
assert (change_base(16, 2) == "10000");
assert (change_base(8, 2) == "1000");
assert (change_base(7, 2) == "111");
for (int x=2;x<8;x++)
assert (change_base(x, x + 1) == to_string(x));
}
|
CPP/46_spans_3
| 3
|
/*
The Fib4 number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows:
fib4(0) -> 0
fib4(1) -> 0
fib4(2) -> 2
fib4(3) -> 0
fib4(n) -> fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4).
Please write a function to efficiently compute the n-th element of the fib4 number sequence. Do not use recursion.
>>> fib4(5)
4
>>> fib4(6)
8
>>> fib4(7)
14
*/
#include<stdio.h>
using namespace std;
int fib4(int n){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int fib4(int n){
|
[
" int f[100];\n f[0]=0;\n f[1]=0",
" i=4;i<=n;i++",
"-2]+f[i-3]+f[i",
"\n}\n"
] |
[
";\n f[2]=2;\n f[3]=0;\n for (int",
")\n {\n f[i]=f[i-1]+f[i",
"-4];\n }\n return f[n];"
] |
int f[100];
f[0]=0;
f[1]=0;
f[2]=2;
f[3]=0;
for (int i=4;i<=n;i++)
{
f[i]=f[i-1]+f[i-2]+f[i-3]+f[i-4];
}
return f[n];
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (fib4(5) == 4);
assert (fib4(8) == 28);
assert (fib4(10) == 104);
assert (fib4(12) == 386);
}
|
CPP/47_spans_3
| 3
|
/*
Return median of elements in the vector l.
>>> median({3, 1, 2, 4, 5})
3
>>> median({-10, 4, 6, 1000, 10, 20})
15.0
*/
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
float median(vector<float> l){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
float median(vector<float> l){
|
[
"",
"l.",
"",
"l.size()/2-1]);\n}\n"
] |
[
" sort(",
"begin(),l.end());\n if (l.size()%2==1) return l[l.size()/2];\n re",
"eturn 0.5*(l[l.size()/2]+l["
] |
sort(l.begin(),l.end());
if (l.size()%2==1) return l[l.size()/2];
return 0.5*(l[l.size()/2]+l[l.size()/2-1]);
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (abs(median({3, 1, 2, 4, 5}) - 3)<1e-4);
assert (abs(median({-10, 4, 6, 1000, 10, 20}) -8.0)<1e-4);
assert (abs(median({5}) - 5)<1e-4);
assert (abs(median({6, 5}) - 5.5)<1e-4);
assert (abs(median({8, 1, 3, 9, 9, 2, 7}) - 7)<1e-4 );
}
|
CPP/50_spans_3
| 3
|
#include<stdio.h>
#include<string>
using namespace std;
string encode_shift(string s){
// returns encoded string by shifting every character by 5 in the alphabet.
string out;
int i;
for (i=0;i<s.length();i++)
{
int w=((int)s[i]+5-(int)'a')%26+(int)'a';
out=out+(char)w;
}
return out;
}
string decode_shift(string s){
// takes as input string encoded with encode_shift function. Returns decoded string.
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string encode_shift(string s){
string out;
int i;
for (i=0;i<s.length();i++)
{
int w=((int)s[i]+5-(int)'a')%26+(int)'a';
out=out+(char)w;
}
return out;
}
string decode_shift(string s){
|
[
" string out;\n int",
"t w=((int)s[i]+21-(int)",
" ",
"t;\n}\n"
] |
[
" i;\n for (i=0;i<s.length();i++)\n {\n in",
"'a')%26+(int)'a'; \n",
" out=out+(char)w;\n }\n return ou"
] |
string out;
int i;
for (i=0;i<s.length();i++)
{
int w=((int)s[i]+21-(int)'a')%26+(int)'a';
out=out+(char)w;
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
for (int i=0;i<100;i++)
{
int l=10+rand()%11;
string str="";
for (int j=0;j<l;j++)
{
char chr=97+rand()%26;
str+=chr;
}
string encoded_str = encode_shift(str);
assert (decode_shift(encoded_str) == str);
}
}
|
CPP/51_spans_3
| 3
|
/*
remove_vowels is a function that takes string and returns string without vowels.
>>> remove_vowels("")
""
>>> remove_vowels("abcdef\nghijklm")
"bcdf\nghjklm"
>>> remove_vowels("abcdef")
"bcdf"
>>> remove_vowels("aaaaa")
""
>>> remove_vowels("aaBAA")
"B"
>>> remove_vowels("zbcd")
"zbcd"
*/
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
string remove_vowels(string text){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
string remove_vowels(string text){
|
[
" string out=\"\";\n s",
"tex",
" ",
"n out;\n\n}\n"
] |
[
"tring vowels=\"AEIOUaeiou\";\n for (int i=0;i<",
"t.length();i++)\n if (find(vowels.begin(),vowels.end(),text[i])==vowels.end())\n ",
" out=out+text[i];\n retur"
] |
string out="";
string vowels="AEIOUaeiou";
for (int i=0;i<text.length();i++)
if (find(vowels.begin(),vowels.end(),text[i])==vowels.end())
out=out+text[i];
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (remove_vowels("") == "");
assert (remove_vowels("abcdef\nghijklm") == "bcdf\nghjklm");
assert (remove_vowels("fedcba") == "fdcb");
assert (remove_vowels("eeeee") == "");
assert (remove_vowels("acBAA") == "cB");
assert (remove_vowels("EcBOO") == "cB");
assert (remove_vowels("ybcd") == "ybcd");
}
|
CPP/52_spans_3
| 3
|
/*
Return true if all numbers in the vector l are below threshold t.
>>> below_threshold({1, 2, 4, 10}, 100)
true
>>> below_threshold({1, 20, 4, 10}, 5)
false
*/
#include<stdio.h>
#include<vector>
using namespace std;
bool below_threshold(vector<int>l, int t){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool below_threshold(vector<int>l, int t){
|
[
" for (int",
"",
"e;",
" true;\n}\n"
] |
[
" i=0;i",
"i<l.size();i++)\n if (l[i]>=t) return fals",
"\n return"
] |
for (int i=0;i<l.size();i++)
if (l[i]>=t) return false;
return true;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (below_threshold({1, 2, 4, 10}, 100));
assert (not(below_threshold({1, 20, 4, 10}, 5)));
assert (below_threshold({1, 20, 4, 10}, 21));
assert (below_threshold({1, 20, 4, 10}, 22));
assert (below_threshold({1, 8, 4, 10}, 11));
assert (not(below_threshold({1, 8, 4, 10}, 10)));
}
|
CPP/54_spans_3
| 3
|
/*
Check if two words have the same characters.
>>> same_chars("eabcdzzzz", "dddzzzzzzzddeddabc")
true
>>> same_chars("abcd", "dddddddabc")
true
>>> same_chars("dddddddabc", "abcd")
true
>>> same_chars("eabcd", "dddddddabc")
false
>>> same_chars("abcd", "dddddddabce")
false
>>> same_chars("eabcdzzzz", "dddzzzzzzzddddabc")
false
*/
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
bool same_chars(string s0,string s1){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
bool same_chars(string s0,string s1){
|
[
" for (int i=0;i<s0.length();i++)\n if (find(s1.beg",
"=0;i<s1.l",
"ind(s0.begin(),s0.en",
" \n}\n"
] |
[
"in(),s1.end(),s0[i])==s1.end())\n return false;\n for (int i",
"ength();i++)\n if (f",
"d(),s1[i])==s0.end())\n return false;\n return true; "
] |
for (int i=0;i<s0.length();i++)
if (find(s1.begin(),s1.end(),s0[i])==s1.end())
return false;
for (int i=0;i<s1.length();i++)
if (find(s0.begin(),s0.end(),s1[i])==s0.end())
return false;
return true;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (same_chars("eabcdzzzz", "dddzzzzzzzddeddabc") == true);
assert (same_chars("abcd", "dddddddabc") == true);
assert (same_chars("dddddddabc", "abcd") == true);
assert (same_chars("eabcd", "dddddddabc") == false);
assert (same_chars("abcd", "dddddddabcf") == false);
assert (same_chars("eabcdzzzz", "dddzzzzzzzddddabc") == false);
assert (same_chars("aabb", "aaccc") == false);
}
|
CPP/55_spans_3
| 3
|
/*
Return n-th Fibonacci number.
>>> fib(10)
55
>>> fib(1)
1
>>> fib(8)
21
*/
#include<stdio.h>
using namespace std;
int fib(int n){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int fib(int n){
|
[
" ",
"",
"f[",
""
] |
[
" in",
"t f[1000];\n f[0]=0;f[1]=1;\n for (int i=2;i<=n; i++)\n f[i]=f[i-1]+f[i-2];\n return ",
"n];\n}\n"
] |
int f[1000];
f[0]=0;f[1]=1;
for (int i=2;i<=n; i++)
f[i]=f[i-1]+f[i-2];
return f[n];
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (fib(10) == 55);
assert (fib(1) == 1);
assert (fib(8) == 21);
assert (fib(11) == 89);
assert (fib(12) == 144);
}
|
CPP/56_spans_3
| 3
|
/*
brackets is a string of '<' and '>'.
return true if every opening bracket has a corresponding closing bracket.
>>> correct_bracketing("<")
false
>>> correct_bracketing("<>")
true
>>> correct_bracketing("<<><>>")
true
>>> correct_bracketing("><<>")
false
*/
#include<stdio.h>
#include<string>
using namespace std;
bool correct_bracketing(string brackets){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool correct_bracketing(string brackets){
|
[
" int ",
"",
"ts.length();i++)\n {\n if (brackets[i]=='<') level+=1;\n if (brackets[i]=='>') level-=1;\n if (level<0) return false;\n }\n if (level!=0) return",
" return true;\n}\n"
] |
[
"level=0",
";\n for (int i=0;i<bracke",
" false;\n "
] |
int level=0;
for (int i=0;i<brackets.length();i++)
{
if (brackets[i]=='<') level+=1;
if (brackets[i]=='>') level-=1;
if (level<0) return false;
}
if (level!=0) return false;
return true;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (correct_bracketing("<>"));
assert (correct_bracketing("<<><>>"));
assert (correct_bracketing("<><><<><>><>"));
assert (correct_bracketing("<><><<<><><>><>><<><><<>>>"));
assert (not (correct_bracketing("<<<><>>>>")));
assert (not (correct_bracketing("><<>")));
assert (not (correct_bracketing("<")));
assert (not (correct_bracketing("<<<<")));
assert (not (correct_bracketing(">")));
assert (not (correct_bracketing("<<>")));
assert (not (correct_bracketing("<><><<><>><>><<>")));
assert (not (correct_bracketing("<><><<><>><>>><>")));
}
|
CPP/57_spans_3
| 3
|
/*
Return true is vector elements are monotonically increasing or decreasing.
>>> monotonic({1, 2, 4, 20})
true
>>> monotonic({1, 20, 4, 10})
false
>>> monotonic({4, 1, 0, -10})
true
*/
#include<stdio.h>
#include<vector>
using namespace std;
bool monotonic(vector<float> l){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool monotonic(vector<float> l){
|
[
" int incr,decr;\n incr=0;",
"l[i-1]",
" (incr+decr=",
"n true;\n}\n"
] |
[
"decr=0;\n for (int i=1;i<l.size();i++)\n {\n if (l[i]>l[i-1]) incr=1;\n if (l[i]<",
") decr=1;\n }\n if",
"=2) return false;\n retur"
] |
int incr,decr;
incr=0;decr=0;
for (int i=1;i<l.size();i++)
{
if (l[i]>l[i-1]) incr=1;
if (l[i]<l[i-1]) decr=1;
}
if (incr+decr==2) return false;
return true;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (monotonic({1, 2, 4, 10}) == true);
assert (monotonic({1, 2, 4, 20}) == true);
assert (monotonic({1, 20, 4, 10}) == false);
assert (monotonic({4, 1, 0, -10}) == true);
assert (monotonic({4, 1, 1, 0}) == true);
assert (monotonic({1, 2, 3, 2, 5, 60}) == false);
assert (monotonic({1, 2, 3, 4, 5, 60}) == true);
assert (monotonic({9, 9, 9, 9}) == true);
}
|
CPP/58_spans_3
| 3
|
/*
Return sorted unique common elements for two vectors.
>>> common({1, 4, 3, 34, 653, 2, 5}, {5, 7, 1, 5, 9, 653, 121})
{1, 5, 653}
>>> common({5, 3, 2, 8}, {3, 2})
{2, 3}
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> common(vector<int> l1,vector<int> l2){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> common(vector<int> l1,vector<int> l2){
|
[
" ",
"l1[i])==o",
"(),l1[i])!=l2.end())\n ",
"sh_back(l1[i]);\n sort(out.begin(),out.end());\n return out;\n}\n"
] |
[
"vector<int> out={};\n for (int i=0;i<l1.size();i++)\n if (find(out.begin(),out.end(),",
"ut.end())\n if (find(l2.begin(),l2.end",
" out.pu"
] |
vector<int> out={};
for (int i=0;i<l1.size();i++)
if (find(out.begin(),out.end(),l1[i])==out.end())
if (find(l2.begin(),l2.end(),l1[i])!=l2.end())
out.push_back(l1[i]);
sort(out.begin(),out.end());
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(common({1, 4, 3, 34, 653, 2, 5}, {5, 7, 1, 5, 9, 653, 121}) , {1, 5, 653}));
assert (issame(common({5, 3, 2, 8}, {3, 2}) , {2, 3}));
assert (issame(common({4, 3, 2, 8}, {3, 2, 4}) , {2, 3, 4}));
assert (issame(common({4, 3, 2, 8}, {}) , {}));
}
|
CPP/61_spans_3
| 3
|
/*
brackets is a string of '(' and ')'.
return true if every opening bracket has a corresponding closing bracket.
>>> correct_bracketing("(")
false
>>> correct_bracketing("()")
true
>>> correct_bracketing("(()())")
true
>>> correct_bracketing(")(()")
false
*/
#include<stdio.h>
#include<string>
using namespace std;
bool correct_bracketing(string brackets){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool correct_bracketing(string brackets){
|
[
" int level=0;\n ",
") level+=1;\n if (brackets[",
" ",
""
] |
[
" for (int i=0;i<brackets.length();i++)\n {\n if (brackets[i]=='('",
"i]==')') level-=1;\n ",
" if (level<0) return false;\n }\n if (level!=0) return false;\n return true;\n}\n"
] |
int level=0;
for (int i=0;i<brackets.length();i++)
{
if (brackets[i]=='(') level+=1;
if (brackets[i]==')') level-=1;
if (level<0) return false;
}
if (level!=0) return false;
return true;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (correct_bracketing("()"));
assert (correct_bracketing("(()())"));
assert (correct_bracketing("()()(()())()"));
assert (correct_bracketing("()()((()()())())(()()(()))"));
assert (not (correct_bracketing("((()())))")));
assert (not (correct_bracketing(")(()")));
assert (not (correct_bracketing("(")));
assert (not (correct_bracketing("((((")));
assert (not (correct_bracketing(")")));
assert (not (correct_bracketing("(()")));
assert (not (correct_bracketing("()()(()())())(()")));
assert (not (correct_bracketing("()()(()())()))()")));
}
|
CPP/62_spans_3
| 3
|
/*
xs represent coefficients of a polynomial.
xs{0} + xs{1} * x + xs{2} * x^2 + ....
Return derivative of this polynomial in the same form.
>>> derivative({3, 1, 2, 4, 5})
{1, 4, 12, 20}
>>> derivative({1, 2, 3})
{2, 6}
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
vector<float> derivative(vector<float> xs){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<float> derivative(vector<float> xs){
|
[
" v",
"",
" retu",
"}\n"
] |
[
"ector<float>",
" out={};\n for (int i=1;i<xs.size();i++)\n out.push_back(i*xs[i]);\n ",
"rn out;\n"
] |
vector<float> out={};
for (int i=1;i<xs.size();i++)
out.push_back(i*xs[i]);
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<float> a,vector<float>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (abs(a[i]-b[i])>1e-4) return false;
}
return true;
}
int main(){
assert (issame(derivative({3, 1, 2, 4, 5}) , {1, 4, 12, 20}));
assert (issame(derivative({1, 2, 3}) , {2, 6}));
assert (issame(derivative({3, 2, 1}) , {2, 2}));
assert (issame(derivative({3, 2, 1, 0, 4}) , {2, 2, 0, 16}));
assert (issame(derivative({1}) , {}));
}
|
CPP/63_spans_3
| 3
|
/*
The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows:
fibfib(0) == 0
fibfib(1) == 0
fibfib(2) == 1
fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3).
Please write a function to efficiently compute the n-th element of the fibfib number sequence.
>>> fibfib(1)
0
>>> fibfib(5)
4
>>> fibfib(8)
24
*/
#include<stdio.h>
using namespace std;
int fibfib(int n){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int fibfib(int n){
|
[
" int",
"]=0;\n ",
"n f",
"\n\n}\n"
] |
[
" ff[100];\n ff[0",
" ff[1]=0;\n ff[2]=1;\n for (int i=3;i<=n;i++)\n ff[i]=ff[i-1]+ff[i-2]+ff[i-3];\n retur",
"f[n];"
] |
int ff[100];
ff[0]=0;
ff[1]=0;
ff[2]=1;
for (int i=3;i<=n;i++)
ff[i]=ff[i-1]+ff[i-2]+ff[i-3];
return ff[n];
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (fibfib(2) == 1);
assert (fibfib(1) == 0);
assert (fibfib(5) == 4);
assert (fibfib(8) == 24);
assert (fibfib(10) == 81);
assert (fibfib(12) == 274);
assert (fibfib(14) == 927);
}
|
CPP/64_spans_3
| 3
|
/*
Write a function vowels_count which takes a string representing a word as input and returns the number of vowels in the string. Vowels in this case are 'a', 'e', 'i', 'o', 'u'.
Here, 'y' is also a vowel, but only when it is at the end of the given word.
Example:
>>> vowels_count("abcde")
2
>>> vowels_count("ACEDY")
3
*/
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
int vowels_count(string s){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int vowels_count(string s){
|
[
" string vowels=\"aeiouAEIOU\";\n int count=0;\n for (int i=0;i<s",
",s",
" if (s[s.length()-1]=='y'",
"t;\n}\n"
] |
[
".length();i++)\n if (find(vowels.begin(),vowels.end()",
"[i])!=vowels.end())\n count+=1;\n ",
" or s[s.length()-1]=='Y') count+=1;\n return coun"
] |
string vowels="aeiouAEIOU";
int count=0;
for (int i=0;i<s.length();i++)
if (find(vowels.begin(),vowels.end(),s[i])!=vowels.end())
count+=1;
if (s[s.length()-1]=='y' or s[s.length()-1]=='Y') count+=1;
return count;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (vowels_count("abcde") == 2);
assert (vowels_count("Alone") == 3);
assert (vowels_count("key") == 2);
assert (vowels_count("bye") == 1);
assert (vowels_count("keY") == 2);
assert (vowels_count("bYe") == 1);
assert (vowels_count("ACEDY") == 3);
}
|
CPP/65_spans_3
| 3
|
/*
Circular shift the digits of the integer x, shift the digits right by shift
and return the result as a string.
If shift > number of digits, return digits reversed.
>>> circular_shift(12, 1)
"21"
>>> circular_shift(12, 2)
"12"
*/
#include<stdio.h>
#include<string>
using namespace std;
string circular_shift(int x,int shift){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string circular_shift(int x,int shift){
|
[
" string xs;\n xs=to_string(x);\n if (xs.length(",
" {",
" }\n xs=xs.",
"str(0,xs.length()-shift);\n return xs;\n}\n"
] |
[
")<shift)\n",
"\n string s(xs.rbegin(),xs.rend());\n return s;\n ",
"substr(xs.length()-shift)+xs.sub"
] |
string xs;
xs=to_string(x);
if (xs.length()<shift)
{
string s(xs.rbegin(),xs.rend());
return s;
}
xs=xs.substr(xs.length()-shift)+xs.substr(0,xs.length()-shift);
return xs;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (circular_shift(100, 2) == "001");
assert (circular_shift(12, 2) == "12");
assert (circular_shift(97, 8) == "79");
assert (circular_shift(12, 1) == "21");
assert (circular_shift(11, 101) == "11");
}
|
CPP/66_spans_3
| 3
|
/*
Task
Write a function that takes a string as input and returns the sum of the upper characters only's
ASCII codes.
Examples:
digitSum("") => 0
digitSum("abAB") => 131
digitSum("abcCd") => 67
digitSum("helloE") => 69
digitSum("woArBld") => 131
digitSum("aAaaaXa") => 153
*/
#include<stdio.h>
#include<string>
using namespace std;
int digitSum(string s){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int digitSum(string s){
|
[
" int sum=0;\n",
"i<s.length();i++)\n",
"[",
"\n"
] |
[
" for (int i=0;",
" if (s[i]>=65 and s",
"i]<=90)\n sum+=s[i];\n return sum;\n}"
] |
int sum=0;
for (int i=0;i<s.length();i++)
if (s[i]>=65 and s[i]<=90)
sum+=s[i];
return sum;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (digitSum("") == 0);
assert (digitSum("abAB") == 131);
assert (digitSum("abcCd") == 67);
assert (digitSum("helloE") == 69);
assert (digitSum("woArBld") == 131);
assert (digitSum("aAaaaXa") == 153);
assert (digitSum(" How are yOu?") == 151);
assert (digitSum("You arE Very Smart") == 327);
}
|
CPP/67_spans_3
| 3
|
/*
In this task, you will be given a string that represents a number of apples and oranges
that are distributed in a basket of fruit this basket contains
apples, oranges, and mango fruits. Given the string that represents the total number of
the oranges and apples and an integer that represent the total number of the fruits
in the basket return the number of the mango fruits in the basket.
for example:
fruit_distribution("5 apples and 6 oranges", 19) ->19 - 5 - 6 = 8
fruit_distribution("0 apples and 1 oranges",3) -> 3 - 0 - 1 = 2
fruit_distribution("2 apples and 3 oranges", 100) -> 100 - 2 - 3 = 95
fruit_distribution("100 apples and 1 oranges",120) -> 120 - 100 - 1 = 19
*/
#include<stdio.h>
#include<string>
using namespace std;
int fruit_distribution(string s,int n){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int fruit_distribution(string s,int n){
|
[
" string num1=\"\",num2=\"\";\n ",
"or (int i=0;i<s.size();i++)\n \n ",
" if (is12==0) num1=num1+s[i];\n if (is",
"2==0 and num1.length()>0) is12=1;\n return n-atoi(num1.c_str())-atoi(num2.c_str());\n\n}\n"
] |
[
" int is12;\n is12=0;\n f",
" if (s[i]>=48 and s[i]<=57)\n {\n ",
"12==1) num2=num2+s[i];\n }\n else\n if (is1"
] |
string num1="",num2="";
int is12;
is12=0;
for (int i=0;i<s.size();i++)
if (s[i]>=48 and s[i]<=57)
{
if (is12==0) num1=num1+s[i];
if (is12==1) num2=num2+s[i];
}
else
if (is12==0 and num1.length()>0) is12=1;
return n-atoi(num1.c_str())-atoi(num2.c_str());
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (fruit_distribution("5 apples and 6 oranges",19) == 8);
assert (fruit_distribution("5 apples and 6 oranges",21) == 10);
assert (fruit_distribution("0 apples and 1 oranges",3) == 2);
assert (fruit_distribution("1 apples and 0 oranges",3) == 2);
assert (fruit_distribution("2 apples and 3 oranges",100) == 95);
assert (fruit_distribution("2 apples and 3 oranges",5) == 0);
assert (fruit_distribution("1 apples and 100 oranges",120) == 19);
}
|
CPP/68_spans_3
| 3
|
/*
Given a vector representing a branch of a tree that has non-negative integer nodes
your task is to pluck one of the nodes and return it.
The plucked node should be the node with the smallest even value.
If multiple nodes with the same smallest even value are found return the node that has smallest index.
The plucked node should be returned in a vector, { smalest_value, its index },
If there are no even values or the given vector is empty, return {}.
Example 1:
Input: {4,2,3}
Output: {2, 1}
Explanation: 2 has the smallest even value, and 2 has the smallest index.
Example 2:
Input: {1,2,3}
Output: {2, 1}
Explanation: 2 has the smallest even value, and 2 has the smallest index.
Example 3:
Input: {}
Output: {}
Example 4:
Input: {5, 0, 3, 0, 4, 2}
Output: {0, 1}
Explanation: 0 is the smallest value, but there are two zeros,
so we will choose the first zero, which has the smallest index.
Constraints:
* 1 <= nodes.length <= 10000
* 0 <= node.value
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> pluck(vector<int> arr){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> pluck(vector<int> arr){
|
[
" ",
"t={",
" ",
" return out;\n}\n"
] |
[
" vector<int> ou",
"};\n for (int i=0;i<arr.size();i++)\n if (arr[i]%2==0 and (out.size()==0 or arr[i]<out[0]))\n ",
" out={arr[i],i};\n"
] |
vector<int> out={};
for (int i=0;i<arr.size();i++)
if (arr[i]%2==0 and (out.size()==0 or arr[i]<out[0]))
out={arr[i],i};
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(pluck({4,2,3}) , {2, 1}));
assert (issame(pluck({1,2,3}) , {2, 1}));
assert (issame(pluck({}) , {}));
assert (issame(pluck({5, 0, 3, 0, 4, 2}) , {0, 1}));
assert (issame(pluck({1, 2, 3, 0, 5, 3}) , {0, 3}));
assert (issame(pluck({5, 4, 8, 4 ,8}) , {4, 1}));
assert (issame(pluck({7, 6, 7, 1}) , {6, 1}));
assert (issame(pluck({7, 9, 7, 1}) , {}));
}
|
CPP/69_spans_3
| 3
|
/*
You are given a non-empty vector of positive integers. Return the greatest integer that is greater than
zero, and has a frequency greater than or equal to the value of the integer itself.
The frequency of an integer is the number of times it appears in the vector.
If no such a value exist, return -1.
Examples:
search({4, 1, 2, 2, 3, 1}) == 2
search({1, 2, 2, 3, 3, 3, 4, 4, 4}) == 3
search({5, 5, 4, 4, 4}) == -1
*/
#include<stdio.h>
#include<vector>
using namespace std;
int search(vector<int> lst){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int search(vector<int> lst){
|
[
" vector<vector<int>> freq={};\n int max=-1;\n for (int i=0;i<lst.size();i++",
" for (int j=0;j<freq.size();j++)\n if (lst[i]==freq[j][0]) \n {\n freq[j][1]+=1;\n has=true;\n if (freq[j][1]>=freq[j][0] and freq[j][0]>max) max=freq[j][0];\n }\n if (not(has)) ",
"sh_bac",
"\n if (max==-1 and lst[i]==1) max=1;\n }\n }\n return max;\n}\n"
] |
[
")\n {\n bool has=false;\n ",
"\n {\n freq.pu",
"k({lst[i],1});"
] |
vector<vector<int>> freq={};
int max=-1;
for (int i=0;i<lst.size();i++)
{
bool has=false;
for (int j=0;j<freq.size();j++)
if (lst[i]==freq[j][0])
{
freq[j][1]+=1;
has=true;
if (freq[j][1]>=freq[j][0] and freq[j][0]>max) max=freq[j][0];
}
if (not(has))
{
freq.push_back({lst[i],1});
if (max==-1 and lst[i]==1) max=1;
}
}
return max;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (search({5, 5, 5, 5, 1}) == 1);
assert (search({4, 1, 4, 1, 4, 4}) == 4);
assert (search({3, 3}) == -1);
assert (search({8, 8, 8, 8, 8, 8, 8, 8}) == 8);
assert (search({2, 3, 3, 2, 2}) == 2);
assert (search({2, 7, 8, 8, 4, 8, 7, 3, 9, 6, 5, 10, 4, 3, 6, 7, 1, 7, 4, 10, 8, 1}) == 1);
assert (search({3, 2, 8, 2}) == 2);
assert (search({6, 7, 1, 8, 8, 10, 5, 8, 5, 3, 10}) == 1);
assert (search({8, 8, 3, 6, 5, 6, 4}) == -1);
assert (search({6, 9, 6, 7, 1, 4, 7, 1, 8, 8, 9, 8, 10, 10, 8, 4, 10, 4, 10, 1, 2, 9, 5, 7, 9}) == 1);
assert (search({1, 9, 10, 1, 3}) == 1);
assert (search({6, 9, 7, 5, 8, 7, 5, 3, 7, 5, 10, 10, 3, 6, 10, 2, 8, 6, 5, 4, 9, 5, 3, 10}) == 5);
assert (search({1}) == 1);
assert (search({8, 8, 10, 6, 4, 3, 5, 8, 2, 4, 2, 8, 4, 6, 10, 4, 2, 1, 10, 2, 1, 1, 5}) == 4);
assert (search({2, 10, 4, 8, 2, 10, 5, 1, 2, 9, 5, 5, 6, 3, 8, 6, 4, 10}) == 2);
assert (search({1, 6, 10, 1, 6, 9, 10, 8, 6, 8, 7, 3}) == 1);
assert (search({9, 2, 4, 1, 5, 1, 5, 2, 5, 7, 7, 7, 3, 10, 1, 5, 4, 2, 8, 4, 1, 9, 10, 7, 10, 2, 8, 10, 9, 4}) == 4);
assert (search({2, 6, 4, 2, 8, 7, 5, 6, 4, 10, 4, 6, 3, 7, 8, 8, 3, 1, 4, 2, 2, 10, 7}) == 4);
assert (search({9, 8, 6, 10, 2, 6, 10, 2, 7, 8, 10, 3, 8, 2, 6, 2, 3, 1}) == 2);
assert (search({5, 5, 3, 9, 5, 6, 3, 2, 8, 5, 6, 10, 10, 6, 8, 4, 10, 7, 7, 10, 8}) == -1);
assert (search({10}) == -1);
assert (search({9, 7, 7, 2, 4, 7, 2, 10, 9, 7, 5, 7, 2}) == 2);
assert (search({5, 4, 10, 2, 1, 1, 10, 3, 6, 1, 8}) == 1);
assert (search({7, 9, 9, 9, 3, 4, 1, 5, 9, 1, 2, 1, 1, 10, 7, 5, 6, 7, 6, 7, 7, 6}) == 1);
assert (search({3, 10, 10, 9, 2}) == -1);
}
|
CPP/70_spans_3
| 3
|
/*
Given vector of integers, return vector in strange order.
Strange sorting, is when you start with the minimum value,
then maximum of the remaining integers, then minimum and so on.
Examples:
strange_sort_vector({1, 2, 3, 4}) == {1, 4, 2, 3}
strange_sort_vector({5, 5, 5, 5}) == {5, 5, 5, 5}
strange_sort_vector({}) == {}
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> strange_sort_list(vector<int> lst){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> strange_sort_list(vector<int> lst){
|
[
" vector<in",
"hile (l<r)\n {\n out.push_back(lst[l]);\n ",
"",
"back(lst[r]);\n r-=1;\n }\n if (l==r) out.push_back(lst[l]);\n return out;\n\n}\n"
] |
[
"t> out={};\n sort(lst.begin(),lst.end());\n int l=0,r=lst.size()-1;\n w",
" l+=1;\n ou",
"t.push_"
] |
vector<int> out={};
sort(lst.begin(),lst.end());
int l=0,r=lst.size()-1;
while (l<r)
{
out.push_back(lst[l]);
l+=1;
out.push_back(lst[r]);
r-=1;
}
if (l==r) out.push_back(lst[l]);
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(strange_sort_list({1, 2, 3, 4}) , {1, 4, 2, 3}));
assert (issame(strange_sort_list({5, 6, 7, 8, 9}) , {5, 9, 6, 8, 7}));
assert (issame(strange_sort_list({1, 2, 3, 4, 5}) , {1, 5, 2, 4, 3}));
assert (issame(strange_sort_list({5, 6, 7, 8, 9, 1}) , {1, 9, 5, 8, 6, 7}));
assert (issame(strange_sort_list({5, 5, 5, 5}) , {5, 5, 5, 5}));
assert (issame(strange_sort_list({}) , {}));
assert (issame(strange_sort_list({1,2,3,4,5,6,7,8}) , {1, 8, 2, 7, 3, 6, 4, 5}));
assert (issame(strange_sort_list({0,2,2,2,5,5,-5,-5}) , {-5, 5, -5, 5, 0, 2, 2, 2}));
assert (issame(strange_sort_list({111111}) , {111111}));
}
|
CPP/71_spans_3
| 3
|
/*
Given the lengths of the three sides of a triangle. Return the area of
the triangle rounded to 2 decimal points if the three sides form a valid triangle.
Otherwise return -1
Three sides make a valid triangle when the sum of any two sides is greater
than the third side.
Example:
triangle_area(3, 4, 5) == 6.00
triangle_area(1, 2, 10) == -1
*/
#include<stdio.h>
#include<math.h>
using namespace std;
float triangle_area(float a,float b,float c){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
float triangle_area(float a,float b,float c){
|
[
" ",
"or a+c",
"eturn -",
" return area;\n}\n"
] |
[
" if (a+b<=c ",
"<=b or b+c<=a) r",
"1;\n float h=(a+b+c)/2;\n float area;\n area=pow(h*(h-a)*(h-b)*(h-c),0.5);\n "
] |
if (a+b<=c or a+c<=b or b+c<=a) return -1;
float h=(a+b+c)/2;
float area;
area=pow(h*(h-a)*(h-b)*(h-c),0.5);
return area;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (abs(triangle_area(3, 4, 5)-6.00)<0.01);
assert (abs(triangle_area(1, 2, 10) +1)<0.01);
assert (abs(triangle_area(4, 8, 5) -8.18)<0.01);
assert (abs(triangle_area(2, 2, 2) -1.73)<0.01);
assert (abs(triangle_area(1, 2, 3) +1)<0.01);
assert (abs(triangle_area(10, 5, 7) - 16.25)<0.01);
assert (abs(triangle_area(2, 6, 3) +1)<0.01);
assert (abs(triangle_area(1, 1, 1) -0.43)<0.01);
assert (abs(triangle_area(2, 2, 10) +1)<0.01);
}
|
CPP/72_spans_3
| 3
|
/*
Write a function that returns true if the object q will fly, and false otherwise.
The object q will fly if it's balanced (it is a palindromic vector) and the sum of its elements is less than or equal the maximum possible weight w.
Example:
will_it_fly({1, 2}, 5) ➞ false
// 1+2 is less than the maximum possible weight, but it's unbalanced.
will_it_fly({3, 2, 3}, 1) ➞ false
// it's balanced, but 3+2+3 is more than the maximum possible weight.
will_it_fly({3, 2, 3}, 9) ➞ true
// 3+2+3 is less than the maximum possible weight, and it's balanced.
will_it_fly({3}, 5) ➞ true
// 3 is less than the maximum possible weight, and it's balanced.
*/
#include<stdio.h>
#include<vector>
using namespace std;
bool will_it_fly(vector<int> q,int w){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool will_it_fly(vector<int> q,int w){
|
[
" int sum=0;\n for (int i=0;i<q.siz",
"e;",
" ",
" true;\n}\n"
] |
[
"e();i++)\n {\n if (q[i]!=q[q.size()-1-i]) return fals",
"\n sum+=q[i];\n",
"}\n if (sum>w) return false;\n return"
] |
int sum=0;
for (int i=0;i<q.size();i++)
{
if (q[i]!=q[q.size()-1-i]) return false;
sum+=q[i];
}
if (sum>w) return false;
return true;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (will_it_fly({3, 2, 3}, 9)==true);
assert (will_it_fly({1, 2}, 5) == false);
assert (will_it_fly({3}, 5) == true);
assert (will_it_fly({3, 2, 3}, 1) == false);
assert (will_it_fly({1, 2, 3}, 6) ==false);
assert (will_it_fly({5}, 5) == true);
}
|
CPP/73_spans_3
| 3
|
/*
Given a vector arr of integers, find the minimum number of elements that
need to be changed to make the vector palindromic. A palindromic vector is a vector that
is read the same backwards and forwards. In one change, you can change one element to any other element.
For example:
smallest_change({1,2,3,5,4,7,9,6}) == 4
smallest_change({1, 2, 3, 4, 3, 2, 2}) == 1
smallest_change({1, 2, 3, 2, 1}) == 0
*/
#include<stdio.h>
#include<vector>
using namespace std;
int smallest_change(vector<int> arr){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int smallest_change(vector<int> arr){
|
[
" i",
" o",
"tur",
""
] |
[
"nt out=0;\n for (int i=0;i<arr.size()-1-i;i++)\n if (arr[i]!=arr[arr.size()-1-i])\n ",
"ut+=1;\n re",
"n out;\n}\n"
] |
int out=0;
for (int i=0;i<arr.size()-1-i;i++)
if (arr[i]!=arr[arr.size()-1-i])
out+=1;
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (smallest_change({1,2,3,5,4,7,9,6}) == 4);
assert (smallest_change({1, 2, 3, 4, 3, 2, 2}) == 1);
assert (smallest_change({1, 4, 2}) == 1);
assert (smallest_change({1, 4, 4, 2}) == 1);
assert (smallest_change({1, 2, 3, 2, 1}) == 0);
assert (smallest_change({3, 1, 1, 3}) == 0);
assert (smallest_change({1}) == 0);
assert (smallest_change({0, 1}) == 1);
}
|
CPP/74_spans_3
| 3
|
/*
Write a function that accepts two vectors of strings and returns the vector that has
total number of chars in the all strings of the vector less than the other vector.
if the two vectors have the same number of chars, return the first vector.
Examples
total_match({}, {}) ➞ {}
total_match({"hi", "admin"}, {"hI", "Hi"}) ➞ {"hI", "Hi"}
total_match({"hi", "admin"}, {"hi", "hi", "admin", "project"}) ➞ {"hi", "admin"}
total_match({"hi", "admin"}, {"hI", "hi", "hi"}) ➞ {"hI", "hi", "hi"}
total_match({"4"}, {"1", "2", "3", "4", "5"}) ➞ {"4"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> total_match(vector<string> lst1,vector<string> lst2){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<string> total_match(vector<string> lst1,vector<string> lst2){
|
[
" int num1,num2,i;\n num1=0;num2=0;\n ",
"r (i=0;i<",
"h",
" lst1;\n}\n"
] |
[
" for (i=0;i<lst1.size();i++)\n num1+=lst1[i].length();\n fo",
"lst2.size();i++)\n num2+=lst2[i].lengt",
"();\n if (num1>num2) return lst2;\n return"
] |
int num1,num2,i;
num1=0;num2=0;
for (i=0;i<lst1.size();i++)
num1+=lst1[i].length();
for (i=0;i<lst2.size();i++)
num2+=lst2[i].length();
if (num1>num2) return lst2;
return lst1;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(total_match({}, {}) , {}));
assert (issame(total_match({"hi", "admin"}, {"hi", "hi"}) , {"hi", "hi"}));
assert (issame(total_match({"hi", "admin"}, {"hi", "hi", "admin", "project"}) , {"hi", "admin"}));
assert (issame(total_match({"4"}, {"1", "2", "3", "4", "5"}) , {"4"}));
assert (issame(total_match({"hi", "admin"}, {"hI", "Hi"}) , {"hI", "Hi"}));
assert (issame(total_match({"hi", "admin"}, {"hI", "hi", "hi"}) , {"hI", "hi", "hi"}));
assert (issame(total_match({"hi", "admin"}, {"hI", "hi", "hii"}) , {"hi", "admin"}));
assert (issame(total_match({}, {"this"}) , {}));
assert (issame(total_match({"this"}, {}) , {}));
}
|
CPP/75_spans_3
| 3
|
/*
Write a function that returns true if the given number is the multiplication of 3 prime numbers
and false otherwise.
Knowing that (a) is less then 100.
Example:
is_multiply_prime(30) == true
30 = 2 * 3 * 5
*/
#include<stdio.h>
using namespace std;
bool is_multiply_prime(int a){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_multiply_prime(int a){
|
[
" ",
"",
"m==2) re",
""
] |
[
" int num=0;\n for (int i=2;i*i<",
"<=a;i++)\n while (a%i==0 and a>i)\n {\n a=a/i;\n num+=1;\n }\n if (nu",
"turn true;\n return false; \n}\n"
] |
int num=0;
for (int i=2;i*i<=a;i++)
while (a%i==0 and a>i)
{
a=a/i;
num+=1;
}
if (num==2) return true;
return false;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (is_multiply_prime(5) == false);
assert (is_multiply_prime(30) == true);
assert (is_multiply_prime(8) == true);
assert (is_multiply_prime(10) == false);
assert (is_multiply_prime(125) == true);
assert (is_multiply_prime(3 * 5 * 7) == true);
assert (is_multiply_prime(3 * 6 * 7) == false);
assert (is_multiply_prime(9 * 9 * 9) == false);
assert (is_multiply_prime(11 * 9 * 9) == false);
assert (is_multiply_prime(11 * 13 * 7) == true);
}
|
CPP/76_spans_3
| 3
|
/*
Your task is to write a function that returns true if a number x is a simple
power of n and false in other cases.
x is a simple power of n if n**int=x
For example:
is_simple_power(1, 4) => true
is_simple_power(2, 2) => true
is_simple_power(8, 2) => true
is_simple_power(3, 2) => false
is_simple_power(3, 1) => false
is_simple_power(5, 3) => false
*/
#include<stdio.h>
#include<math.h>
using namespace std;
bool is_simple_power(int x,int n){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_simple_power(int x,int n){
|
[
" i",
"le (p<=x an",
"1",
"n false;\n}\n"
] |
[
"nt p=1,count=0;\n whi",
"d count<100)\n {\n if (p==x) return true;\n p=p*n;count+=",
";\n }\n retur"
] |
int p=1,count=0;
while (p<=x and count<100)
{
if (p==x) return true;
p=p*n;count+=1;
}
return false;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (is_simple_power(1, 4)== true);
assert (is_simple_power(2, 2)==true);
assert (is_simple_power(8, 2)==true);
assert (is_simple_power(3, 2)==false);
assert (is_simple_power(3, 1)==false);
assert (is_simple_power(5, 3)==false);
assert (is_simple_power(16, 2)== true);
assert (is_simple_power(143214, 16)== false);
assert (is_simple_power(4, 2)==true);
assert (is_simple_power(9, 3)==true);
assert (is_simple_power(16, 4)==true);
assert (is_simple_power(24, 2)==false);
assert (is_simple_power(128, 4)==false);
assert (is_simple_power(12, 6)==false);
assert (is_simple_power(1, 1)==true);
assert (is_simple_power(1, 12)==true);
}
|
CPP/77_spans_3
| 3
|
/*
Write a function that takes an integer a and returns true
if this ingeger is a cube of some integer number.
Note: you may assume the input is always valid.
Examples:
iscube(1) ==> true
iscube(2) ==> false
iscube(-1) ==> true
iscube(64) ==> true
iscube(0) ==> true
iscube(180) ==> false
*/
#include<stdio.h>
#include<math.h>
using namespace std;
bool iscuber(int a){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool iscuber(int a){
|
[
" for ",
"i++)\n",
"f (i*i*i==abs(a)) ret",
"n false;\n}\n"
] |
[
"(int i=0;i*i*i<=abs(a);",
" i",
"urn true;\n retur"
] |
for (int i=0;i*i*i<=abs(a);i++)
if (i*i*i==abs(a)) return true;
return false;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (iscuber(1) == true);
assert (iscuber(2) == false);
assert (iscuber(-1) == true);
assert (iscuber(64) == true);
assert (iscuber(180) == false);
assert (iscuber(1000) == true);
assert (iscuber(0) == true);
assert (iscuber(1729) == false);
}
|
CPP/78_spans_3
| 3
|
/*
You have been tasked to write a function that receives
a hexadecimal number as a string and counts the number of hexadecimal
digits that are primes (prime number, or a prime, is a natural number
greater than 1 that is not a product of two smaller natural numbers).
Hexadecimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
Prime numbers are 2, 3, 5, 7, 11, 13, 17,...
So you have to determine a number of the following digits: 2, 3, 5, 7,
B (=decimal 11), D (=decimal 13).
Note: you may assume the input is always correct or empty string,
and symbols A,B,C,D,E,F are always uppercase.
Examples:
For num = "AB" the output should be 1.
For num = "1077E" the output should be 2.
For num = "ABED1A33" the output should be 4.
For num = "123456789ABCDEF0" the output should be 6.
For num = "2020" the output should be 2.
*/
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
int hex_key(string num){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int hex_key(string num){
|
[
" s",
"357BD\";\n int ou",
"(int i=0;i<num.length();",
"(),key.end(),num[i])!=key.end()) out+=1;\n return out;\n}\n"
] |
[
"tring key=\"2",
"t=0;\n for ",
"i++)\n if (find(key.begin"
] |
string key="2357BD";
int out=0;
for (int i=0;i<num.length();i++)
if (find(key.begin(),key.end(),num[i])!=key.end()) out+=1;
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (hex_key("AB") == 1 );
assert (hex_key("1077E") == 2 );
assert (hex_key("ABED1A33") == 4 );
assert (hex_key("2020") == 2 );
assert (hex_key("123456789ABCDEF0") == 6 );
assert (hex_key("112233445566778899AABBCCDDEEFF00") == 12 );
assert (hex_key("") == 0);
}
|
CPP/79_spans_3
| 3
|
/*
You will be given a number in decimal form and your task is to convert it to
binary format. The function should return a string, with each character representing a binary
number. Each character in the string will be '0' or '1'.
There will be an extra couple of characters "db" at the beginning and at the end of the string.
The extra characters are there to help with the format.
Examples:
decimal_to_binary(15) // returns "db1111db"
decimal_to_binary(32) // returns "db100000db"
*/
#include<stdio.h>
#include<string>
using namespace std;
string decimal_to_binary(int decimal){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string decimal_to_binary(int decimal){
|
[
" string out=\"\";\n if ",
"0)",
"g(decimal%2)+out;\n d",
"rn out;\n}\n"
] |
[
"(decimal==",
" return \"db0db\";\n while (decimal>0)\n {\n out=to_strin",
"ecimal=decimal/2;\n }\n out=\"db\"+out+\"db\";\n retu"
] |
string out="";
if (decimal==0) return "db0db";
while (decimal>0)
{
out=to_string(decimal%2)+out;
decimal=decimal/2;
}
out="db"+out+"db";
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (decimal_to_binary(0) == "db0db");
assert (decimal_to_binary(32) == "db100000db");
assert (decimal_to_binary(103) == "db1100111db");
assert (decimal_to_binary(15) == "db1111db");
}
|
CPP/80_spans_3
| 3
|
/*
You are given a string s.
Your task is to check if the string is happy or not.
A string is happy if its length is at least 3 and every 3 consecutive letters are distinct
For example:
is_happy("a") => false
is_happy("aa") => false
is_happy("abcd") => true
is_happy("aabb") => false
is_happy("adb") => true
is_happy("xyy") => false
*/
#include<stdio.h>
#include<string>
using namespace std;
bool is_happy(string s){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_happy(string s){
|
[
" if (s.length()<3) return false;\n ",
"=2;i<s.",
");i++)\n if (s[i]==s[i-1] or s[i]==s[i-2])",
"turn true;\n}\n"
] |
[
"for (int i",
"length(",
" return false;\n re"
] |
if (s.length()<3) return false;
for (int i=2;i<s.length();i++)
if (s[i]==s[i-1] or s[i]==s[i-2]) return false;
return true;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (is_happy("a") == false );
assert (is_happy("aa") == false );
assert (is_happy("abcd") == true );
assert (is_happy("aabb") == false );
assert (is_happy("adb") == true );
assert (is_happy("xyy") == false );
assert (is_happy("iopaxpoi") == true );
assert (is_happy("iopaxioi") == false );
}
|
CPP/81_spans_3
| 3
|
/*
It is the last week of the semester and the teacher has to give the grades
to students. The teacher has been making her own algorithm for grading.
The only problem is, she has lost the code she used for grading.
She has given you a vector of GPAs for some students and you have to write
a function that can output a vector of letter grades using the following table:
GPA | Letter grade
4.0 A+
> 3.7 A
> 3.3 A-
> 3.0 B+
> 2.7 B
> 2.3 B-
> 2.0 C+
> 1.7 C
> 1.3 C-
> 1.0 D+
> 0.7 D
> 0.0 D-
0.0 E
Example:
grade_equation({4.0, 3, 1.7, 2, 3.5}) ==> {"A+", "B", "C-", "C", "A-"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> numerical_letter_grade(vector<float> grades){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<string> numerical_letter_grade(vector<float> grades){
|
[
" vector<string> out={};\n for (int i=0;i<grades.size();i++)\n {\n if (grades",
"rades[i]>3.7001 and grades[i]<3.9999)",
"k(\"A-\");\n if (grades[i]>3.0001 and grades[i]<=3.3001) out.push_back(\"B+\");\n if (grades[i]>2.7001 and grades[i]<=3.0001) out.push_back(\"B\");\n if (grades[i]>2.3001 and grades[i]<=2.7001) out.push_back(\"B-\");\n if (grades[i]>2.0001 and grades[i]<=2.3001) out.push_back(\"C+\");\n if (grades[i]>1.7001 and grades[i]<=2.0",
".push_back(\"C-\");\n if (grades[i]>1.0001 and grades[i]<=1.3001) out.push_back(\"D+\");\n if (grades[i]>0.7001 and grades[i]<=1.0001) out.push_back(\"D\");\n if (grades[i]>0.0001 and grades[i]<=0.7001) out.push_back(\"D-\");\n if (grades[i]<=0.0001) out.push_back(\"E\");\n }\n return out;\n}\n"
] |
[
"[i]>=3.9999) out.push_back(\"A+\");\n if (g",
" out.push_back(\"A\");\n if (grades[i]>3.3001 and grades[i]<=3.7001) out.push_bac",
"001) out.push_back(\"C\");\n if (grades[i]>1.3001 and grades[i]<=1.7001) out"
] |
vector<string> out={};
for (int i=0;i<grades.size();i++)
{
if (grades[i]>=3.9999) out.push_back("A+");
if (grades[i]>3.7001 and grades[i]<3.9999) out.push_back("A");
if (grades[i]>3.3001 and grades[i]<=3.7001) out.push_back("A-");
if (grades[i]>3.0001 and grades[i]<=3.3001) out.push_back("B+");
if (grades[i]>2.7001 and grades[i]<=3.0001) out.push_back("B");
if (grades[i]>2.3001 and grades[i]<=2.7001) out.push_back("B-");
if (grades[i]>2.0001 and grades[i]<=2.3001) out.push_back("C+");
if (grades[i]>1.7001 and grades[i]<=2.0001) out.push_back("C");
if (grades[i]>1.3001 and grades[i]<=1.7001) out.push_back("C-");
if (grades[i]>1.0001 and grades[i]<=1.3001) out.push_back("D+");
if (grades[i]>0.7001 and grades[i]<=1.0001) out.push_back("D");
if (grades[i]>0.0001 and grades[i]<=0.7001) out.push_back("D-");
if (grades[i]<=0.0001) out.push_back("E");
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(numerical_letter_grade({4.0, 3, 1.7, 2, 3.5}) , {"A+", "B", "C-", "C", "A-"}));
assert (issame(numerical_letter_grade({1.2}) , {"D+"}));
assert (issame(numerical_letter_grade({0.5}) , {"D-"}));
assert (issame(numerical_letter_grade({0.0}) , {"E"}));
assert (issame(numerical_letter_grade({1, 0.3, 1.5, 2.8, 3.3}) , {"D", "D-", "C-", "B", "B+"}));
assert (issame(numerical_letter_grade({0, 0.7}) , {"E", "D-"}));
}
|
CPP/82_spans_3
| 3
|
/*
Write a function that takes a string and returns true if the string
length is a prime number or false otherwise
Examples
prime_length("Hello") == true
prime_length("abcdcba") == true
prime_length("kittens") == true
prime_length("orange") == false
*/
#include<stdio.h>
#include<string>
using namespace std;
bool prime_length(string str){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool prime_length(string str){
|
[
" int l,i;\n l=s",
"or",
" ",
"e;\n}\n"
] |
[
"tr.length();\n if (l<2) return false;\n f",
" (i=2;i*i<=l;i++)\n if (l%i==0) return false;\n",
"return tru"
] |
int l,i;
l=str.length();
if (l<2) return false;
for (i=2;i*i<=l;i++)
if (l%i==0) return false;
return true;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (prime_length("Hello") == true);
assert (prime_length("abcdcba") == true);
assert (prime_length("kittens") == true);
assert (prime_length("orange") == false);
assert (prime_length("wow") == true);
assert (prime_length("world") == true);
assert (prime_length("MadaM") == true);
assert (prime_length("Wow") == true);
assert (prime_length("") == false);
assert (prime_length("HI") == true);
assert (prime_length("go") == true);
assert (prime_length("gogo") == false);
assert (prime_length("aaaaaaaaaaaaaaa") == false);
assert (prime_length("Madam") == true);
assert (prime_length("M") == false);
assert (prime_length("0") == false);
}
|
CPP/83_spans_3
| 3
|
/*
Given a positive integer n, return the count of the numbers of n-digit
positive integers that start or end with 1.
*/
#include<stdio.h>
using namespace std;
int starts_one_ends(int n){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int starts_one_ends(int n){
|
[
" if (n<1) return 0",
"==1) return 1;\n int out=18",
"",
")\n out=out*10;\n return out;\n}\n"
] |
[
";\n if (n",
";\n for (int i=",
"=2;i<n;i++"
] |
if (n<1) return 0;
if (n==1) return 1;
int out=18;
for (int i=2;i<n;i++)
out=out*10;
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (starts_one_ends(1) == 1);
assert (starts_one_ends(2) == 18);
assert (starts_one_ends(3) == 180);
assert (starts_one_ends(4) == 1800);
assert (starts_one_ends(5) == 18000);
}
|
CPP/84_spans_3
| 3
|
/*
Given a positive integer N, return the total sum of its digits in binary.
Example
For N = 1000, the sum of digits will be 1 the output should be "1".
For N = 150, the sum of digits will be 6 the output should be "110".
For N = 147, the sum of digits will be 12 the output should be "1100".
Variables:
@N integer
Constraints: 0 ≤ N ≤ 10000.
Output:
a string of binary number
*/
#include<stdio.h>
#include<string>
using namespace std;
string solve(int N){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string solve(int N){
|
[
" string",
"length();i++)\n",
" ",
"}\n return bi;\n}\n"
] |
[
" str,bi=\"\";\n str=to_string(N);\n int i,sum=0;\n for (int i=0;i<str.",
" sum+=str[i]-48;\n while (sum>0)\n {\n bi=to_string(sum%2)+bi;\n",
" sum=sum/2;\n "
] |
string str,bi="";
str=to_string(N);
int i,sum=0;
for (int i=0;i<str.length();i++)
sum+=str[i]-48;
while (sum>0)
{
bi=to_string(sum%2)+bi;
sum=sum/2;
}
return bi;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (solve(1000) == "1");
assert (solve(150) == "110");
assert (solve(147) == "1100");
assert (solve(333) == "1001");
assert (solve(963) == "10010");
}
|
CPP/85_spans_3
| 3
|
/*
Given a non-empty vector of integers lst. add the even elements that are at odd indices..
Examples:
add({4, 2, 6, 7}) ==> 2
*/
#include<stdio.h>
#include<vector>
using namespace std;
int add(vector<int> lst){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int add(vector<int> lst){
|
[
"",
"t",
"1];\n return",
""
] |
[
" in",
" sum=0;\n for (int i=0;i*2+1<lst.size();i++)\n if (lst[i*2+1]%2==0) sum+=lst[i*2+",
" sum;\n}\n"
] |
int sum=0;
for (int i=0;i*2+1<lst.size();i++)
if (lst[i*2+1]%2==0) sum+=lst[i*2+1];
return sum;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (add({4, 88}) == 88);
assert (add({4, 5, 6, 7, 2, 122}) == 122);
assert (add({4, 0, 6, 7}) == 0);
assert (add({4, 4, 6, 8}) == 12);
}
|
CPP/86_spans_3
| 3
|
/*
Write a function that takes a string and returns an ordered version of it.
Ordered version of string, is a string where all words (separated by space)
are replaced by a new word where all the characters arranged in
ascending order based on ascii value.
Note: You should keep the order of words and blank spaces in the sentence.
For example:
anti_shuffle("Hi") returns "Hi"
anti_shuffle("hello") returns "ehllo"
anti_shuffle("Hello World!!!") returns "Hello !!!Wdlor"
*/
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
string anti_shuffle(string s){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
string anti_shuffle(string s){
|
[
" string out=\"\";\n string curre",
"s.length();i++)\n if",
"ent.begin(),current.end());\n if (out.length()>0) out=out+' ';\n out=out+current;\n current=\"\";\n }\n else current=curren",
"turn out;\n}\n"
] |
[
"nt=\"\";\n s=s+' ';\n for (int i=0;i<",
" (s[i]==' ')\n {\n sort(curr",
"t+s[i];\n re"
] |
string out="";
string current="";
s=s+' ';
for (int i=0;i<s.length();i++)
if (s[i]==' ')
{
sort(current.begin(),current.end());
if (out.length()>0) out=out+' ';
out=out+current;
current="";
}
else current=current+s[i];
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (anti_shuffle("Hi") == "Hi");
assert (anti_shuffle("hello") == "ehllo");
assert (anti_shuffle("number") == "bemnru");
assert (anti_shuffle("abcd") == "abcd");
assert (anti_shuffle("Hello World!!!") == "Hello !!!Wdlor");
assert (anti_shuffle("") == "");
assert (anti_shuffle("Hi. My name is Mister Robot. How are you?") == ".Hi My aemn is Meirst .Rboot How aer ?ouy");
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.