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/36_spans_2
| 2
|
/*
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 count=0;\n for (int i=0;i<n;i++)\n if (i%11==0 or i%13==0)\n {\n i",
" q=q",
""
] |
[
"nt q=i;\n while (q>0)\n {\n if (q%10==7) count+=1;\n ",
"/10;\n }\n } \n return count;\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_2
| 2
|
/*
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<float> o",
"={};\n for (int i=0;i*2<l.size();i++)\n even.push_back(l[",
"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_back(l[i]);\n }\n return out;\n}\n"
] |
[
"ut={};\n vector<float> even",
"i*2]);\n sort(even.begin(),even.end());\n for (int "
] |
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_2
| 2
|
#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=",
";\n int num=(l+2)/3;\n string x,output;\n int i;\n for (i=0;i*3",
"==3) x=x[2]+x.substr(0,2);\n output=output+x;\n }\n return output;\n\n\n}\n"
] |
[
"s.length()",
"<l;i++)\n {\n x=s.substr(i*3,3);\n if (x.length()"
] |
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_2
| 2
|
/*
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=0;\n while (count<n)\n {\n f1=f1+f2;\n m=f1;f1=f2;f2=m;\n bool isprime=true;\n",
"ime=false; break;\n }",
" if (isprime) count+=1;\n if (count==n) return f1;\n }\n\n}\n"
] |
[
" for (int w=2;w*w<=f1;w++)\n if (f1%w==0)\n {\n ispr",
"\n "
] |
int f1,f2,m;
f1=1;f2=2;
int count=0;
while (count<n)
{
f1=f1+f2;
m=f1;f1=f2;f2=m;
bool isprime=true;
for (int w=2;w*w<=f1;w++)
if (f1%w==0)
{
isprime=false; break;
}
if (isprime) count+=1;
if (count==n) return f1;
}
}
|
#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_2
| 2
|
/*
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 (int i=0;i<l.si",
"ize();j",
" return false;\n}\n"
] |
[
"ze();i++)\n for (int j=i+1;j<l.s",
"++)\n for (int k=j+1;k<l.size();k++)\n if (l[i]+l[j]+l[k]==0) return true;\n "
] |
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/41_spans_2
| 2
|
/*
Imagine a road that's a perfectly straight infinitely long line.
n cars are driving left to right; simultaneously, a different set of n cars
are driving right to left. The two sets of cars start out being very far from
each other. All cars move in the same speed. Two cars are said to collide
when a car that's moving left to right hits a car that's moving right to left.
However, the cars are infinitely sturdy and strong; as a result, they continue moving
in their trajectory as if they did not collide.
This function outputs the number of such collisions.
*/
#include<stdio.h>
using namespace std;
int car_race_collision(int n){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int car_race_collision(int n){
|
[
"",
"",
"\n}\n"
] |
[
" r",
"return n*n;"
] |
return n*n;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (car_race_collision(2) == 4);
assert (car_race_collision(3) == 9);
assert (car_race_collision(4) == 16);
assert (car_race_collision(8) == 64);
assert (car_race_collision(10) == 100);
}
|
CPP/42_spans_2
| 2
|
/*
Return vector with elements incremented by 1.
>>> incr_vector({1, 2, 3})
{2, 3, 4}
>>> incr_vector({5, 3, 5, 2, 3, 3, 9, 0, 123})
{6, 4, 6, 3, 4, 4, 10, 1, 124}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> incr_list(vector<int> l){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> incr_list(vector<int> l){
|
[
" ",
"i",
"ze();i++)\n l[i]+=1;\n return l;\n}\n"
] |
[
"for (int i=0;",
"<l.si"
] |
for (int i=0;i<l.size();i++)
l[i]+=1;
return l;
}
|
#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(incr_list({}) , {}));
assert (issame(incr_list({3, 2, 1}) , {4, 3, 2}));
assert (issame(incr_list({5, 2, 5, 2, 3, 3, 9, 0, 123}) , {6, 3, 6, 3, 4, 4, 10, 1, 124}));
}
|
CPP/43_spans_2
| 2
|
/*
pairs_sum_to_zero takes a vector of integers as an input.
it returns true if there are two distinct elements in the vector that
sum to zero, and false otherwise.
>>> pairs_sum_to_zero({1, 3, 5, 0})
false
>>> pairs_sum_to_zero({1, 3, -2, 1})
false
>>> pairs_sum_to_zero({1, 2, 3, 7})
false
>>> pairs_sum_to_zero({2, 4, -5, 3, 5, 7})
true
>>> pairs_sum_to_zero({1})
false
*/
#include<stdio.h>
#include<vector>
using namespace std;
bool pairs_sum_to_zero(vector<int> l){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool pairs_sum_to_zero(vector<int> l){
|
[
" ",
"t i=0;i<l.size",
"return false;\n}\n"
] |
[
" for (in",
"();i++)\n for (int j=i+1;j<l.size();j++)\n if (l[i]+l[j]==0) return true;\n "
] |
for (int i=0;i<l.size();i++)
for (int j=i+1;j<l.size();j++)
if (l[i]+l[j]==0) return true;
return false;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (pairs_sum_to_zero({1, 3, 5, 0}) == false);
assert (pairs_sum_to_zero({1, 3, -2, 1}) == false);
assert (pairs_sum_to_zero({1, 2, 3, 7}) == false);
assert (pairs_sum_to_zero({2, 4, -5, 3, 5, 7}) == true);
assert (pairs_sum_to_zero({1}) == false);
assert (pairs_sum_to_zero({-3, 9, -1, 3, 2, 30}) == true);
assert (pairs_sum_to_zero({-3, 9, -1, 3, 2, 31}) == true);
assert (pairs_sum_to_zero({-3, 9, -1, 4, 2, 30}) == false);
assert (pairs_sum_to_zero({-3, 9, -1, 4, 2, 31}) == false);
}
|
CPP/44_spans_2
| 2
|
/*
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){
|
[
" stri",
"o_s",
";\n}\n"
] |
[
"ng out=\"\";\n while (x>0)\n {\n out=t",
"tring(x%base)+out;\n x=x/base;\n }\n return out"
] |
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_2
| 2
|
/*
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){
|
[
" ",
"i<=n;i++)",
"turn f[n];\n}\n"
] |
[
" int f[100];\n f[0]=0;\n f[1]=0;\n f[2]=2;\n f[3]=0;\n for (int i=4;",
"\n {\n f[i]=f[i-1]+f[i-2]+f[i-3]+f[i-4];\n }\n re"
] |
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_2
| 2
|
/*
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){
|
[
" sort(l.begi",
"nd());\n if (l.",
"rn l[l.size()/2];\n return 0.5*(l[l.size()/2]+l[l.size()/2-1]);\n}\n"
] |
[
"n(),l.e",
"size()%2==1) retu"
] |
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/48_spans_2
| 2
|
/*
Checks if given string is a palindrome
>>> is_palindrome("")
true
>>> is_palindrome("aba")
true
>>> is_palindrome("aaaaa")
true
>>> is_palindrome("zbcd")
false
*/
#include<stdio.h>
#include<string>
using namespace std;
bool is_palindrome(string text){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_palindrome(string text){
|
[
"",
"",
"t;\n}\n"
] |
[
" st",
"ring pr(text.rbegin(),text.rend());\n return pr==tex"
] |
string pr(text.rbegin(),text.rend());
return pr==text;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (is_palindrome("") == true);
assert (is_palindrome("aba") == true);
assert (is_palindrome("aaaaa") == true);
assert (is_palindrome("zbcd") == false);
assert (is_palindrome("xywyx") == true);
assert (is_palindrome("xywyz") == false);
assert (is_palindrome("xywzx") == false);
}
|
CPP/49_spans_2
| 2
|
/*
Return 2^n modulo p (be aware of numerics).
>>> modp(3, 5)
3
>>> modp(1101, 101)
2
>>> modp(0, 101)
1
>>> modp(3, 11)
8
>>> modp(100, 101)
1
*/
#include<stdio.h>
using namespace std;
int modp(int n,int p){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int modp(int n,int p){
|
[
" int out",
"<n;i++)\n ",
"}\n"
] |
[
"=1;\n for (int i=0;i",
" out=(out*2)%p;\n return out;\n"
] |
int out=1;
for (int i=0;i<n;i++)
out=(out*2)%p;
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (modp(3, 5) == 3);
assert (modp(1101, 101) == 2);
assert (modp(0, 101) == 1);
assert (modp(3, 11) == 8);
assert (modp(100, 101) == 1);
assert (modp(30, 5) == 4);
assert (modp(31, 5) == 3);
}
|
CPP/50_spans_2
| 2
|
#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",
"or (i=0;i<s.length();i++)\n ",
"out;\n}\n"
] |
[
" out;\n int i;\n f",
" {\n int w=((int)s[i]+21-(int)'a')%26+(int)'a'; \n out=out+(char)w;\n }\n return "
] |
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_2
| 2
|
/*
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 string vowels=\"AEIOUaeiou\";\n for (int i=0;i<text.length(",
"(),text[i])==vowels.end())\n ",
"return out;\n\n}\n"
] |
[
");i++)\n if (find(vowels.begin(),vowels.end",
" out=out+text[i];\n "
] |
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_2
| 2
|
/*
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){
|
[
" ",
"r",
""
] |
[
"for (int i=0;i<l.size();i++)\n if (l[i]>=t) return false;\n ",
"eturn true;\n}\n"
] |
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/53_spans_2
| 2
|
/*
Add two numbers x and y
>>> add(2, 3)
5
>>> add(5, 7)
12
*/
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int add(int x,int y){
|
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#include<algorithm>
#include<math.h>
int add(int x,int y){
|
[
" ",
"",
"\n"
] |
[
" return",
" x+y;\n}"
] |
return x+y;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (add(0, 1) == 1);
assert (add(1, 0) == 1);
assert (add(2, 3) == 5);
assert (add(5, 7) == 12);
assert (add(7, 5) == 12);
for (int i=0;i<100;i+=1)
{
int x=rand()%1000;
int y=rand()%1000;
assert (add(x, y) == x + y);
}
}
|
CPP/54_spans_2
| 2
|
/*
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 (i",
".end(),s0[i])==s1.end())\n return false;\n for (i",
"i++)\n if (find(s0.begin(),s0.end(),s1[i])==s0.end())\n return false;\n return true; \n}\n"
] |
[
"nt i=0;i<s0.length();i++)\n if (find(s1.begin(),s1",
"nt i=0;i<s1.length();"
] |
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_2
| 2
|
/*
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[1]=1;\n for (int i=2;i<=n; i++)\n f[i]=f[i-1]",
"rn f[n];\n}\n"
] |
[
" int f[1000];\n f[0]=0;",
"+f[i-2];\n retu"
] |
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_2
| 2
|
/*
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 for (int i=0;i<brackets.length();i++)\n {\n if (brackets[i]=='<') level+=1",
"vel<0) retur",
"ue;\n}\n"
] |
[
";\n if (brackets[i]=='>') level-=1;\n if (le",
"n false;\n }\n if (level!=0) return false;\n return tr"
] |
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_2
| 2
|
/*
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;decr=0;\n for (int i=1;i<l.size();i+",
"-1]) incr=1;",
"]) decr=1;\n }\n if (incr+decr==2) return false;\n return true;\n}\n"
] |
[
"+)\n {\n if (l[i]>l[i",
"\n if (l[i]<l[i-1"
] |
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_2
| 2
|
/*
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){
|
[
" vector<int> out={};\n for (int i=0;i<l1.size();i++)\n if (find(out.begin(),out.end(),l1[i])==out.end())\n ",
"egin(),l2.end(),l1[i])!=l2.end())\n out.push_back",
" return out;\n}\n"
] |
[
" if (find(l2.b",
"(l1[i]);\n sort(out.begin(),out.end());\n"
] |
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/59_spans_2
| 2
|
/*
Return the largest prime factor of n. Assume n > 1 and is not a prime.
>>> largest_prime_factor(13195)
29
>>> largest_prime_factor(2048)
2
*/
#include<stdio.h>
using namespace std;
int largest_prime_factor(int n){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int largest_prime_factor(int n){
|
[
" for (i",
" and n>i) ",
"n n;\n}\n"
] |
[
"nt i=2;i*i<=n;i++)\n while (n%i==0",
" n=n/i;\n retur"
] |
for (int i=2;i*i<=n;i++)
while (n%i==0 and n>i) n=n/i;
return n;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (largest_prime_factor(15) == 5);
assert (largest_prime_factor(27) == 3);
assert (largest_prime_factor(63) == 7);
assert (largest_prime_factor(330) == 11);
assert (largest_prime_factor(13195) == 29);
}
|
CPP/60_spans_2
| 2
|
/*
sum_to_n is a function that sums numbers from 1 to n.
>>> sum_to_n(30)
465
>>> sum_to_n(100)
5050
>>> sum_to_n(5)
15
>>> sum_to_n(10)
55
>>> sum_to_n(1)
1
*/
#include<stdio.h>
using namespace std;
int sum_to_n(int n){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int sum_to_n(int n){
|
[
" re",
"",
")/2;\n}\n"
] |
[
"turn ",
" n*(n+1"
] |
return n*(n+1)/2;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (sum_to_n(1) == 1);
assert (sum_to_n(6) == 21);
assert (sum_to_n(11) == 66);
assert (sum_to_n(30) == 465);
assert (sum_to_n(100) == 5050);
}
|
CPP/61_spans_2
| 2
|
/*
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 for (int i=0;i<brackets.le",
") level-=",
"turn false;\n }\n if (level!=0) return false;\n return true;\n}\n"
] |
[
"ngth();i++)\n {\n if (brackets[i]=='(') level+=1;\n if (brackets[i]==')'",
"1;\n if (level<0) re"
] |
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_2
| 2
|
/*
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){
|
[
" vector",
"xs.",
"\n out.push_back(i*xs[i]);\n return out;\n}\n"
] |
[
"<float> out={};\n for (int i=1;i<",
"size();i++)"
] |
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_2
| 2
|
/*
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 ff[100];\n ff[0]=0",
"0;\n ff[2]=1;\n ",
"f[i-3];\n return ff[n];\n\n}\n"
] |
[
";\n ff[1]=",
" for (int i=3;i<=n;i++)\n ff[i]=ff[i-1]+ff[i-2]+f"
] |
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_2
| 2
|
/*
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.length();i++)\n if (find(vowels.begin(),vowels.end(),s[i])!=vowels.end())\n c",
"==",
"eturn count;\n}\n"
] |
[
"ount+=1;\n if (s[s.length()-1]=='y' or s[s.length()-1]",
"'Y') count+=1;\n r"
] |
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_2
| 2
|
/*
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_",
"d());\n return s;\n }\n xs=xs.sub",
"-shift)+xs.substr(0,xs.length()-shift);\n return xs;\n}\n"
] |
[
"string(x);\n if (xs.length()<shift)\n {\n string s(xs.rbegin(),xs.ren",
"str(xs.length()"
] |
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_2
| 2
|
/*
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 for",
"]<=90)\n ",
"n sum;\n}\n"
] |
[
" (int i=0;i<s.length();i++)\n if (s[i]>=65 and s[i",
" sum+=s[i];\n retur"
] |
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_2
| 2
|
/*
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 int is12;\n ",
" {\n if (is12==0) num1=num1+s[i];\n if (is12==1) num2=num2+s[i];\n }\n else\n if (is12==",
""
] |
[
"is12=0;\n for (int i=0;i<s.size();i++)\n \n if (s[i]>=48 and s[i]<=57)\n ",
"0 and num1.length()>0) is12=1;\n return n-atoi(num1.c_str())-atoi(num2.c_str());\n\n}\n"
] |
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_2
| 2
|
/*
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){
|
[
" vector<int> out={};\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 return out;\n}\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_2
| 2
|
/*
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",
"<lst.size();i++)\n {",
"req.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)) \n {\n freq.push_back({lst[i],1});\n if (max==-1 and lst[i]==1) max=1;\n }\n }\n return max;\n}\n"
] |
[
" int max=-1;\n for (int i=0;i",
"\n bool has=false;\n for (int j=0;j<f"
] |
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_2
| 2
|
/*
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<int> out={};\n sort(lst.begin(),lst.end());\n int l=0,r=lst.size()-1;\n whi",
" ",
"k(lst[r]);\n r-=1;\n }\n if (l==r) out.push_back(lst[l]);\n return out;\n\n}\n"
] |
[
"le (l<r)\n {\n ",
" out.push_back(lst[l]);\n l+=1;\n out.push_bac"
] |
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_2
| 2
|
/*
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){
|
[
" if (a+b<=c or",
"+c)/2;\n float area",
"return area;\n}\n"
] |
[
" a+c<=b or b+c<=a) return -1;\n float h=(a+b",
";\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_2
| 2
|
/*
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){
|
[
" ",
")\n {\n if (q[i]!=q[q.size(",
"return true;\n}\n"
] |
[
"int sum=0;\n for (int i=0;i<q.size();i++",
")-1-i]) return false;\n sum+=q[i];\n }\n if (sum>w) return false;\n "
] |
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_2
| 2
|
/*
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){
|
[
" int out",
" ou",
" return out;\n}\n"
] |
[
"=0;\n for (int i=0;i<arr.size()-1-i;i++)\n if (arr[i]!=arr[arr.size()-1-i])\n",
"t+=1;\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_2
| 2
|
/*
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,n",
"um2=0;\n for (i=0;i<lst1.size();i++)\n ",
"ize();i++)\n num2+=lst2[i].length();\n if (num1>num2) return lst2;\n return lst1;\n}\n"
] |
[
"um2,i;\n num1=0;n",
" num1+=lst1[i].length();\n for (i=0;i<lst2.s"
] |
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_2
| 2
|
/*
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){
|
[
" int num=",
"nt i=2;i*i<=a;i++)\n ",
" a=a/i;\n num+=1;\n }\n if (num==2) return true;\n return false; \n}\n"
] |
[
"0;\n for (i",
" while (a%i==0 and a>i)\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_2
| 2
|
/*
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){
|
[
" int p",
"\n while (p",
" return false;\n}\n"
] |
[
"=1,count=0;",
"<=x and count<100)\n {\n if (p==x) return true;\n p=p*n;count+=1;\n }\n "
] |
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_2
| 2
|
/*
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 (int",
"a);i++)\n if (i*i*i==abs(a)) return tr",
"\n"
] |
[
" i=0;i*i*i<=abs(",
"ue;\n return false;\n}"
] |
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_2
| 2
|
/*
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){
|
[
" string k",
"length();i++)\n if (find(key.be",
"um[i])!=key.end()) out+=1;\n return out;\n}\n"
] |
[
"ey=\"2357BD\";\n int out=0;\n for (int i=0;i<num.",
"gin(),key.end(),n"
] |
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_2
| 2
|
/*
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 ",
"_string(decimal%2)+out;\n decimal=decimal/2",
"}\n"
] |
[
"(decimal==0) return \"db0db\";\n while (decimal>0)\n {\n out=to",
";\n }\n out=\"db\"+out+\"db\";\n return out;\n"
] |
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_2
| 2
|
/*
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.lengt",
" return false;\n",
"alse;\n return true;\n}\n"
] |
[
"h()<3)",
" for (int i=2;i<s.length();i++)\n if (s[i]==s[i-1] or s[i]==s[i-2]) return f"
] |
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_2
| 2
|
/*
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[i]>=3.9999) out.push_back(\"A+\");\n if (grades[i]>3.7001 and grades[i]<3.9999) out.push_back(\"A\");\n if (grades[i]>3.3001 and grades[i]<=3.7001) out.push_back(\"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.0001) out.push_back(\"C\");\n if (grades[i]>1.3001 and grades[i]<=1.7001) out.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",
"f (grades[i]>0.0001 and grades[i]<=0.7001) out.p",
"(\"E\");\n }\n return out;\n}\n"
] |
[
".0001) out.push_back(\"D\");\n i",
"ush_back(\"D-\");\n if (grades[i]<=0.0001) out.push_back"
] |
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_2
| 2
|
/*
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",
" if (l%i==0) return f",
""
] |
[
" l,i;\n l=str.length();\n if (l<2) return false;\n for (i=2;i*i<=l;i++)\n ",
"alse;\n return true;\n}\n"
] |
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_2
| 2
|
/*
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<",
")",
" return out;\n}\n"
] |
[
"1) return 0;\n if (n==1",
" return 1;\n int out=18;\n for (int i=2;i<n;i++)\n out=out*10;\n "
] |
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_2
| 2
|
/*
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 str,bi=\"\";\n str=to_st",
" while (sum",
" }\n return bi;\n}\n"
] |
[
"ring(N);\n int i,sum=0;\n for (int i=0;i<str.length();i++)\n sum+=str[i]-48;\n",
">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_2
| 2
|
/*
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){
|
[
" int sum=0;\n ",
" (",
"2+1]%2==0) sum+=lst[i*2+1];\n return sum;\n}\n"
] |
[
" for (int i=0;i*2+1<lst.size();i++)\n if",
"lst[i*"
] |
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_2
| 2
|
/*
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 current=\"\";\n s=s+' ';",
"==' ')\n {\n sort(current.begin(),current.end());\n if (out.length",
"rent=\"\";\n }\n else current=current+s[i];\n return out;\n}\n"
] |
[
"\n for (int i=0;i<s.length();i++)\n if (s[i]",
"()>0) out=out+' ';\n out=out+current;\n cur"
] |
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");
}
|
CPP/87_spans_2
| 2
|
/*
You are given a 2 dimensional data, as a nested vectors,
which is similar to matrix, however, unlike matrices,
each row may contain a different number of columns.
Given lst, and integer x, find integers x in the vector,
and return vector of vectors, {{x1, y1}, {x2, y2} ...} such that
each vector is a coordinate - {row, columns}, starting with 0.
Sort coordinates initially by rows in ascending order.
Also, sort coordinates of the row by columns in descending order.
Examples:
get_row({
{1,2,3,4,5,6},
{1,2,3,4,1,6},
{1,2,3,4,5,1}
}, 1) == {{0, 0}, {1, 4}, {1, 0}, {2, 5}, {2, 0}}
get_row({}, 1) == {}
get_row({{}, {1}, {1, 2, 3}}, 3) == {{2, 2}}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<vector<int>> get_row(vector<vector<int>> lst, int x){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<vector<int>> get_row(vector<vector<int>> lst, int x){
|
[
" vector<vector<int>> out={};\n for (",
"st[i].size(",
"({i,j});\n return out;\n}\n"
] |
[
"int i=0;i<lst.size();i++)\n for (int j=l",
")-1;j>=0;j-=1)\n if (lst[i][j]==x) out.push_back"
] |
vector<vector<int>> out={};
for (int i=0;i<lst.size();i++)
for (int j=lst[i].size()-1;j>=0;j-=1)
if (lst[i][j]==x) out.push_back({i,j});
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(vector<vector<int>> a,vector<vector<int>> b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i].size()!=b[i].size()) return false;
for (int j=0;j<a[i].size();j++)
if (a[i][j]!=b[i][j]) return false;
}
return true;
}
int main(){
assert (issame(get_row({
{1,2,3,4,5,6},
{1,2,3,4,1,6},
{1,2,3,4,5,1}}, 1) , {{0, 0}, {1, 4}, {1, 0}, {2, 5}, {2, 0}}));
assert (issame(get_row({
{1,2,3,4,5,6},
{1,2,3,4,5,6},
{1,2,3,4,5,6},
{1,2,3,4,5,6},
{1,2,3,4,5,6},
{1,2,3,4,5,6}}, 2) , {{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}}));
assert (issame(get_row({
{1,2,3,4,5,6},
{1,2,3,4,5,6},
{1,1,3,4,5,6},
{1,2,1,4,5,6},
{1,2,3,1,5,6},
{1,2,3,4,1,6},
{1,2,3,4,5,1}
}, 1) , {{0, 0}, {1, 0}, {2, 1}, {2, 0}, {3, 2}, {3, 0}, {4, 3}, {4, 0}, {5, 4}, {5, 0}, {6, 5}, {6, 0}}));
assert (issame(get_row({}, 1) , {}));
assert (issame(get_row({{1}}, 2) , {}));
assert (issame(get_row({{}, {1}, {1, 2, 3}}, 3) , {{2, 2}}));
}
|
CPP/88_spans_2
| 2
|
/*
Given a vector of non-negative integers, return a copy of the given vector after sorting,
you will sort the given vector in ascending order if the sum( first index value, last index value) is odd,
or sort it in descending order if the sum( first index value, last index value) is even.
Note:
* don't change the given vector.
Examples:
* sort_vector({}) => {}
* sort_vector({5}) => {5}
* sort_vector({2, 4, 3, 0, 1, 5}) => {0, 1, 2, 3, 4, 5}
* sort_vector({2, 4, 3, 0, 1, 5, 6}) => {6, 5, 4, 3, 2, 1, 0}
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> sort_array(vector<int> array){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> sort_array(vector<int> array){
|
[
" if (array.",
"()-1]) %2==1)\n {\n sort(array.begin(),array.end());",
"\n sort(array.begin(),array.end());\n vector<int> out={};\n for (int i=array.size()-1;i>=0;i-=1)\n out.push_back(array[i]);\n return out;\n }\n\n}\n"
] |
[
"size()==0) return {};\n if ((array[0]+array[array.size",
"\n return array;\n }\n else\n {"
] |
if (array.size()==0) return {};
if ((array[0]+array[array.size()-1]) %2==1)
{
sort(array.begin(),array.end());
return array;
}
else
{
sort(array.begin(),array.end());
vector<int> out={};
for (int i=array.size()-1;i>=0;i-=1)
out.push_back(array[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_array({}) , {}));
assert (issame(sort_array({5}) , {5}));
assert (issame(sort_array({2, 4, 3, 0, 1, 5}) , {0, 1, 2, 3, 4, 5}));
assert (issame(sort_array({2, 4, 3, 0, 1, 5, 6}) , {6, 5, 4, 3, 2, 1, 0}));
assert (issame(sort_array({2, 1}) , {1, 2}));
assert (issame(sort_array({15, 42, 87, 32 ,11, 0}) , {0, 11, 15, 32, 42, 87}));
assert (issame(sort_array({21, 14, 23, 11}) , {23, 21, 14, 11}));
}
|
CPP/89_spans_2
| 2
|
/*
Create a function encrypt that takes a string as an argument and
returns a string encrypted with the alphabet being rotated.
The alphabet should be rotated in a manner such that the letters
shift down by two multiplied to two places.
For example:
encrypt("hi") returns "lm"
encrypt("asdfghjkl") returns "ewhjklnop"
encrypt("gf") returns "kj"
encrypt("et") returns "ix"
*/
#include<stdio.h>
#include<string>
using namespace std;
string encrypt(string s){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string encrypt(string s){
|
[
" string",
" int w=((int)s[i]+4-(int)'a'",
"t=out+(char)w;\n }\n return out;\n}\n"
] |
[
" out;\n int i;\n for (i=0;i<s.length();i++)\n {\n ",
")%26+(int)'a'; \n ou"
] |
string out;
int i;
for (i=0;i<s.length();i++)
{
int w=((int)s[i]+4-(int)'a')%26+(int)'a';
out=out+(char)w;
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (encrypt("hi") == "lm");
assert (encrypt("asdfghjkl") == "ewhjklnop");
assert (encrypt("gf") == "kj");
assert (encrypt("et") == "ix");
assert (encrypt("faewfawefaewg")=="jeiajeaijeiak");
assert (encrypt("hellomyfriend")=="lippsqcjvmirh");
assert (encrypt("dxzdlmnilfuhmilufhlihufnmlimnufhlimnufhfucufh")=="hbdhpqrmpjylqmpyjlpmlyjrqpmqryjlpmqryjljygyjl");
assert (encrypt("a")=="e");
}
|
CPP/90_spans_2
| 2
|
/*
You are given a vector of integers.
Write a function next_smallest() that returns the 2nd smallest element of the vector.
Return None if there is no such element.
next_smallest({1, 2, 3, 4, 5}) == 2
next_smallest({5, 1, 4, 3, 2}) == 2
next_smallest({}) == None
next_smallest({1, 1}) == None
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int next_smallest(vector<int> lst){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int next_smallest(vector<int> lst){
|
[
" sort(lst.be",
"i-1]) retur",
"\n"
] |
[
"gin(),lst.end());\n for (int i=1;i<lst.size();i++)\n if (lst[i]!=lst[",
"n lst[i];\n return -1;\n}"
] |
sort(lst.begin(),lst.end());
for (int i=1;i<lst.size();i++)
if (lst[i]!=lst[i-1]) return lst[i];
return -1;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (next_smallest({1, 2, 3, 4, 5}) == 2);
assert (next_smallest({5, 1, 4, 3, 2}) == 2);
assert (next_smallest({}) == -1);
assert (next_smallest({1, 1}) == -1);
assert (next_smallest({1,1,1,1,0}) == 1);
assert (next_smallest({-35, 34, 12, -45}) == -35);
}
|
CPP/91_spans_2
| 2
|
/*
You'll be given a string of words, and your task is to count the number
of boredoms. A boredom is a sentence that starts with the word "I".
Sentences are delimited by '.', '?' or '!'.
For example:
>>> is_bored("Hello world")
0
>>> is_bored("The sky is blue. The sun is shining. I love this weather")
1
*/
#include<stdio.h>
#include<string>
using namespace std;
int is_bored(string S){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int is_bored(string S){
|
[
" bool isstart=true;\n ",
" if (S[i]==' ' and isi) {is",
"e; }\n else isi=false; \n if (S[i]!=' ') { isstart=false;}\n if (S[i]=='.' or S[i]=='?' or S[i]=='!') isstart=true;\n }\n return sum;\n}\n"
] |
[
" bool isi=false;\n int sum=0;\n for (int i=0;i<S.length();i++)\n {\n ",
"i=false; sum+=1;}\n if (S[i]=='I' and isstart) {isi=tru"
] |
bool isstart=true;
bool isi=false;
int sum=0;
for (int i=0;i<S.length();i++)
{
if (S[i]==' ' and isi) {isi=false; sum+=1;}
if (S[i]=='I' and isstart) {isi=true; }
else isi=false;
if (S[i]!=' ') { isstart=false;}
if (S[i]=='.' or S[i]=='?' or S[i]=='!') isstart=true;
}
return sum;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (is_bored("Hello world") == 0);
assert (is_bored("Is the sky blue?") == 0);
assert (is_bored("I love It !") == 1);
assert (is_bored("bIt") == 0);
assert (is_bored("I feel good today. I will be productive. will kill It") == 2);
assert (is_bored("You and I are going for a walk") == 0);
}
|
CPP/92_spans_2
| 2
|
/*
Create a function that takes 3 numbers.
Returns true if one of the numbers is equal to the sum of the other two, and all numbers are integers.
Returns false in any other cases.
Examples
any_int(5, 2, 7) β true
any_int(3, 2, 2) β false
any_int(3, -2, 1) β true
any_int(3.6, -2.2, 2) β false
*/
#include<stdio.h>
#include<math.h>
using namespace std;
bool any_int(float a,float b,float c){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool any_int(float a,float b,float c){
|
[
" ",
"a) return false;\n if (round(b)!=b) return false;\n if (round(c)!=c) return false;\n if (a+b==c or a+c",
"a) return true;\n return false;\n}\n"
] |
[
"if (round(a)!=",
"==b or b+c=="
] |
if (round(a)!=a) return false;
if (round(b)!=b) return false;
if (round(c)!=c) return false;
if (a+b==c or a+c==b or b+c==a) return true;
return false;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (any_int(2, 3, 1)==true);
assert (any_int(2.5, 2, 3)==false);
assert (any_int(1.5, 5, 3.5)==false);
assert (any_int(2, 6, 2)==false);
assert (any_int(4, 2, 2)==true);
assert (any_int(2.2, 2.2, 2.2)==false);
assert (any_int(-4, 6, 2)==true);
assert (any_int(2,1,1)==true);
assert (any_int(3,4,7)==true);
assert (any_int(3.01,4,7)==false);
}
|
CPP/93_spans_2
| 2
|
/*
Write a function that takes a message, and encodes in such a
way that it swaps case of all letters, replaces all vowels in
the message with the letter that appears 2 places ahead of that
vowel in the english alphabet.
Assume only letters.
Examples:
>>> encode('test")
"TGST"
>>> encode("This is a message")
'tHKS KS C MGSSCGG"
*/
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
string encode(string message){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
string encode(string message){
|
[
" string vowels=\"aeiouAEIOU\";\n string out=\"",
" if (w>=97 and w<=122){w=w-32;}\n else if (w>=65 and w<=90) w=w+32;\n if (find(vowels.begin(),vowels.end(),w",
" out=out+w;\n }\n return out;\n}\n"
] |
[
"\";\n for (int i=0;i<message.length();i++)\n {\n char w=message[i];\n ",
")!=vowels.end()) w=w+2;\n "
] |
string vowels="aeiouAEIOU";
string out="";
for (int i=0;i<message.length();i++)
{
char w=message[i];
if (w>=97 and w<=122){w=w-32;}
else if (w>=65 and w<=90) w=w+32;
if (find(vowels.begin(),vowels.end(),w)!=vowels.end()) w=w+2;
out=out+w;
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (encode("TEST") == "tgst");
assert (encode("Mudasir") == "mWDCSKR");
assert (encode("YES") == "ygs");
assert (encode("This is a message") == "tHKS KS C MGSSCGG");
assert (encode("I DoNt KnOw WhAt tO WrItE") == "k dQnT kNqW wHcT Tq wRkTg");
}
|
CPP/94_spans_2
| 2
|
/*
You are given a vector of integers.
You need to find the largest prime value and return the sum of its digits.
Examples:
For lst = {0,3,2,1,3,5,7,4,5,5,5,2,181,32,4,32,3,2,32,324,4,3} the output should be 10
For lst = {1,0,1,8,2,4597,2,1,3,40,1,2,1,2,4,2,5,1} the output should be 25
For lst = {1,3,1,32,5107,34,83278,109,163,23,2323,32,30,1,9,3} the output should be 13
For lst = {0,724,32,71,99,32,6,0,5,91,83,0,5,6} the output should be 11
For lst = {0,81,12,3,1,21} the output should be 3
For lst = {0,8,1,2,1,7} the output should be 7
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
int skjkasdkd(vector<int> lst){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int skjkasdkd(vector<int> lst){
|
[
" int largest=0;\n for (int i=0;i<lst.size();i++)\n if (lst[i]>largest)\n {\n bool prime=true;\n for (int j=2;j*",
" if (prime) largest=lst[i];\n }\n int sum=0;\n string s;\n ",
";\n return sum;\n}\n#undef NDEBUG\n#include<assert.h>\n"
] |
[
"j<=lst[i];j++)\n if (lst[i]%j==0) prime=false;\n ",
" s=to_string(largest);\n for (int i=0;i<s.length();i++)\n sum+=s[i]-48"
] |
int largest=0;
for (int i=0;i<lst.size();i++)
if (lst[i]>largest)
{
bool prime=true;
for (int j=2;j*j<=lst[i];j++)
if (lst[i]%j==0) prime=false;
if (prime) largest=lst[i];
}
int sum=0;
string s;
s=to_string(largest);
for (int i=0;i<s.length();i++)
sum+=s[i]-48;
return sum;
}
#undef NDEBUG
#include<assert.h>
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (skjkasdkd({0,3,2,1,3,5,7,4,5,5,5,2,181,32,4,32,3,2,32,324,4,3}) == 10);
assert (skjkasdkd({1,0,1,8,2,4597,2,1,3,40,1,2,1,2,4,2,5,1}) == 25);
assert (skjkasdkd({1,3,1,32,5107,34,83278,109,163,23,2323,32,30,1,9,3}) == 13);
assert (skjkasdkd({0,724,32,71,99,32,6,0,5,91,83,0,5,6}) == 11);
assert (skjkasdkd({0,81,12,3,1,21}) == 3);
assert (skjkasdkd({0,8,1,2,1,7}) == 7);
assert (skjkasdkd({8191}) == 19);
assert (skjkasdkd({8191, 123456, 127, 7}) == 19);
assert (skjkasdkd({127, 97, 8192}) == 10);
}
|
CPP/95_spans_2
| 2
|
/*
Given a map, return true if all keys are strings in lower
case or all keys are strings in upper case, else return false.
The function should return false is the given map is empty.
Examples:
check_map_case({{"a","apple"}, {"b","banana"}}) should return true.
check_map_case({{"a","apple"}, {"A","banana"}, {"B","banana"}}) should return false.
check_map_case({{"a","apple"}, {"8","banana"}, {"a","apple"}}) should return false.
check_map_case({{"Name","John"}, {"Age","36"}, {"City","Houston"}}) should return false.
check_map_case({{"STATE","NC"}, {"ZIP","12345"} }) should return true.
*/
#include<stdio.h>
#include<string>
#include<map>
using namespace std;
bool check_dict_case(map<string,string> dict){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<map>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool check_dict_case(map<string,string> dict){
|
[
" map<string,string>::iterator it;\n int islower=0,isupper=0;\n if (dict.size()==0) return false;\n for (it=dict.begin();it!=dict.end();it++)\n {\n string key=it->first;\n \n for (int i=0;i<key.length();i++)\n {\n if (key[i]<65 or (key[i]>90 and key[i]<97) or key[i]>122) return false;\n if (",
"]>=65 and key[i]<=90) isupper=1;\n if (key[i]>=97 and key[i]<=122) islower=1",
" }\n\n }\n return true;\n}\n"
] |
[
"key[i",
";\n if (isupper+islower==2) return false;\n"
] |
map<string,string>::iterator it;
int islower=0,isupper=0;
if (dict.size()==0) return false;
for (it=dict.begin();it!=dict.end();it++)
{
string key=it->first;
for (int i=0;i<key.length();i++)
{
if (key[i]<65 or (key[i]>90 and key[i]<97) or key[i]>122) return false;
if (key[i]>=65 and key[i]<=90) isupper=1;
if (key[i]>=97 and key[i]<=122) islower=1;
if (isupper+islower==2) return false;
}
}
return true;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (check_dict_case({{"p","pineapple"}, {"b","banana"}}) == true);
assert (check_dict_case({{"p","pineapple"}, {"A","banana"}, {"B","banana"}}) == false);
assert (check_dict_case({{"p","pineapple"}, {"5","banana"}, {"a","apple"}}) == false);
assert (check_dict_case({{"Name","John"}, {"Age","36"}, {"City","Houston"}}) == false);
assert (check_dict_case({{"STATE","NC"}, {"ZIP","12345"} }) == true );
assert (check_dict_case({{"fruit","Orange"}, {"taste","Sweet"} }) == true );
assert (check_dict_case({}) == false);
}
|
CPP/96_spans_2
| 2
|
/*
Implement a function that takes an non-negative integer and returns a vector of the first n
integers that are prime numbers and less than n.
for example:
count_up_to(5) => {2,3}
count_up_to(11) => {2,3,5,7}
count_up_to(0) => {}
count_up_to(20) => {2,3,5,7,11,13,17,19}
count_up_to(1) => {}
count_up_to(18) => {2,3,5,7,11,13,17}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> count_up_to(int n){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> count_up_to(int n){
|
[
" vector<int> out={};\n int i,j;\n for (i=2;i<n;i++)\n if (out.size()==0) {out.push_back(i);}\n else\n {\n bool isp=true;",
"j]<=i;j++)\n ",
"k(i);\n }\n return out;\n}\n"
] |
[
"\n for (j=0;out[j]*out[",
" if (i%out[j]==0) isp=false;\n if (isp) out.push_bac"
] |
vector<int> out={};
int i,j;
for (i=2;i<n;i++)
if (out.size()==0) {out.push_back(i);}
else
{
bool isp=true;
for (j=0;out[j]*out[j]<=i;j++)
if (i%out[j]==0) isp=false;
if (isp) 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(count_up_to(5) , {2,3}));
assert (issame(count_up_to(6) , {2,3,5}));
assert (issame(count_up_to(7) , {2,3,5}));
assert (issame(count_up_to(10) , {2,3,5,7}));
assert (issame(count_up_to(0) , {}));
assert (issame(count_up_to(22) , {2,3,5,7,11,13,17,19}));
assert (issame(count_up_to(1) , {}));
assert (issame(count_up_to(18) , {2,3,5,7,11,13,17}));
assert (issame(count_up_to(47) , {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43}));
assert (issame(count_up_to(101) , {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}));
}
|
CPP/97_spans_2
| 2
|
/*
Complete the function that takes two integers and returns
the product of their unit digits.
Assume the input is always valid.
Examples:
multiply(148, 412) should return 16.
multiply(19, 28) should return 72.
multiply(2020, 1851) should return 0.
multiply(14,-15) should return 20.
*/
#include<stdio.h>
#include<math.h>
using namespace std;
int multiply(int a,int b){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int multiply(int a,int b){
|
[
" ",
"b)",
""
] |
[
" return (abs(a)%10)*(abs(",
"%10);\n}\n"
] |
return (abs(a)%10)*(abs(b)%10);
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (multiply(148, 412) == 16 );
assert (multiply(19, 28) == 72 );
assert (multiply(2020, 1851) == 0);
assert (multiply(14,-15) == 20 );
assert (multiply(76, 67) == 42 );
assert (multiply(17, 27) == 49 );
assert (multiply(0, 1) == 0);
assert (multiply(0, 0) == 0);
}
|
CPP/98_spans_2
| 2
|
/*
Given a string s, count the number of uppercase vowels in even indices.
For example:
count_upper("aBCdEf") returns 1
count_upper("abcdefg") returns 0
count_upper("dBBE") returns 0
*/
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
int count_upper(string s){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int count_upper(string s){
|
[
" string uvowel=\"A",
"(find(uvowel.",
"t;\n}\n"
] |
[
"EIOU\";\n int count=0;\n for (int i=0;i*2<s.length();i++)\n if ",
"begin(),uvowel.end(),s[i*2])!=uvowel.end())\n count+=1;\n return coun"
] |
string uvowel="AEIOU";
int count=0;
for (int i=0;i*2<s.length();i++)
if (find(uvowel.begin(),uvowel.end(),s[i*2])!=uvowel.end())
count+=1;
return count;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (count_upper("aBCdEf") == 1);
assert (count_upper("abcdefg") == 0);
assert (count_upper("dBBE") == 0);
assert (count_upper("B") == 0);
assert (count_upper("U") == 1);
assert (count_upper("") == 0);
assert (count_upper("EEEE") == 2);
}
|
CPP/100_spans_2
| 2
|
/*
Given a positive integer n, you have to make a pile of n levels of stones.
The first level has n stones.
The number of stones in the next level is:
- the next odd number if n is odd.
- the next even number if n is even.
Return the number of stones in each level in a vector, where element at index
i represents the number of stones in the level (i+1).
Examples:
>>> make_a_pile(3)
{3, 5, 7}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> make_a_pile(int n){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> make_a_pile(int n){
|
[
"",
"",
";\n return out;\n}\n"
] |
[
" vector<in",
"nt> out={n};\n for (int i=1;i<n;i++)\n out.push_back(out[out.size()-1]+2)"
] |
vector<int> out={n};
for (int i=1;i<n;i++)
out.push_back(out[out.size()-1]+2);
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(make_a_pile(3) , {3, 5, 7}));
assert (issame(make_a_pile(4) , {4,6,8,10}));
assert (issame(make_a_pile(5) , {5, 7, 9, 11, 13}));
assert (issame(make_a_pile(6) , {6, 8, 10, 12, 14, 16}));
assert (issame(make_a_pile(8) , {8, 10, 12, 14, 16, 18, 20, 22}));
}
|
CPP/101_spans_2
| 2
|
/*
You will be given a string of words separated by commas or spaces. Your task is
to split the string into words and return a vector of the words.
For example:
words_string("Hi, my name is John") == {"Hi", "my", "name", "is", "John"}
words_string("One, two, three, four, five, six") == {"One", 'two", 'three", "four", "five", 'six"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> words_string(string s){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<string> words_string(string s){
|
[
" string current=\"\";\n vector<string> out={};\n s=s+' ';\n f",
")>0)\n {\n out.push_back(current);\n current=\"\";\n }\n }\n e",
" return out;\n}\n"
] |
[
"or (int i=0;i<s.length();i++)\n if (s[i]==' ' or s[i]==',')\n {\n if (current.length(",
"lse current=current+s[i];\n "
] |
string current="";
vector<string> out={};
s=s+' ';
for (int i=0;i<s.length();i++)
if (s[i]==' ' or s[i]==',')
{
if (current.length()>0)
{
out.push_back(current);
current="";
}
}
else current=current+s[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(words_string("Hi, my name is John") , {"Hi", "my", "name", "is", "John"}));
assert (issame(words_string("One, two, three, four, five, six") , {"One", "two", "three", "four", "five", "six"}));
assert (issame(words_string("Hi, my name") , {"Hi", "my", "name"}));
assert (issame(words_string("One,, two, three, four, five, six,") , {"One", "two", "three", "four", "five", "six"}));
assert (issame(words_string("") , {}));
assert (issame(words_string("ahmed , gamal") , {"ahmed", "gamal"}));
}
|
CPP/102_spans_2
| 2
|
/*
This function takes two positive numbers x and y and returns the
biggest even integer number that is in the range [x, y] inclusive. If
there's no such number, then the function should return -1.
For example:
choose_num(12, 15) = 14
choose_num(13, 12) = -1
*/
#include<stdio.h>
using namespace std;
int choose_num(int x,int y){
|
#include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int choose_num(int x,int y){
|
[
" ",
"<",
"urn y;\n}\n"
] |
[
" if (y",
"x) return -1;\n if (y==x and y%2==1) return -1;\n if (y%2==1) return y-1;\n ret"
] |
if (y<x) return -1;
if (y==x and y%2==1) return -1;
if (y%2==1) return y-1;
return y;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (choose_num(12, 15) == 14);
assert (choose_num(13, 12) == -1);
assert (choose_num(33, 12354) == 12354);
assert (choose_num(5234, 5233) == -1);
assert (choose_num(6, 29) == 28);
assert (choose_num(27, 10) == -1);
assert (choose_num(7, 7) == -1);
assert (choose_num(546, 546) == 546);
}
|
CPP/103_spans_2
| 2
|
/*
You are given two positive integers n and m, and your task is to compute the
average of the integers from n through m (including n and m).
Round the answer to the nearest integer(smaller one) and convert that to binary.
If n is greater than m, return "-1".
Example:
rounded_avg(1, 5) => "11"
rounded_avg(7, 5) => "-1"
rounded_avg(10, 20) => "1111"
rounded_avg(20, 33) => "11010"
*/
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
string rounded_avg(int n,int m){
|
#include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string rounded_avg(int n,int m){
|
[
" if (n>m) retu",
"2;\n string out=\"\";\n ",
"um>0)\n {\n out=to_string(num%2)+out;\n num=num/2;\n }\n return out;\n}\n"
] |
[
"rn \"-1\";\n int num=(m+n)/",
" while (n"
] |
if (n>m) return "-1";
int num=(m+n)/2;
string out="";
while (num>0)
{
out=to_string(num%2)+out;
num=num/2;
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (rounded_avg(1, 5) == "11");
assert (rounded_avg(7, 13) == "1010");
assert (rounded_avg(964,977) == "1111001010");
assert (rounded_avg(996,997) == "1111100100");
assert (rounded_avg(560,851) == "1011000001");
assert (rounded_avg(185,546) == "101101101");
assert (rounded_avg(362,496) == "110101101");
assert (rounded_avg(350,902) == "1001110010");
assert (rounded_avg(197,233) == "11010111");
assert (rounded_avg(7, 5) == "-1");
assert (rounded_avg(5, 1) == "-1");
assert (rounded_avg(5, 5) == "101");
}
|
CPP/104_spans_2
| 2
|
/*
Given a vector of positive integers x. return a sorted vector of all
elements that hasn't any even digit.
Note: Returned vector should be sorted in increasing order.
For example:
>>> unique_digits({15, 33, 1422, 1})
{1, 15, 33}
>>> unique_digits({152, 323, 1422, 10})
{}
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> unique_digits(vector<int> x){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> unique_digits(vector<int> x){
|
[
" vector<int> out={};\n for (int i=0;i<x.size();",
"ol u=true;\n if (num==0) u=false;\n ",
" num=num/10;\n }\n if (u) out.push_back(x[i]);\n }\n sort(out.begin(),out.end());\n return out;\n}\n"
] |
[
"i++)\n {\n int num=x[i];\n bo",
" while (num>0 and u)\n {\n if (num%2==0) u=false;\n "
] |
vector<int> out={};
for (int i=0;i<x.size();i++)
{
int num=x[i];
bool u=true;
if (num==0) u=false;
while (num>0 and u)
{
if (num%2==0) u=false;
num=num/10;
}
if (u) out.push_back(x[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_digits({15, 33, 1422, 1}) , {1, 15, 33}));
assert (issame(unique_digits({152, 323, 1422, 10}) , {}));
assert (issame(unique_digits({12345, 2033, 111, 151}) , {111, 151}));
assert (issame(unique_digits({135, 103, 31}) , {31, 135}));
}
|
CPP/105_spans_2
| 2
|
/*
Given a vector of integers, sort the integers that are between 1 and 9 inclusive,
reverse the resulting vector, and then replace each digit by its corresponding name from
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine".
For example:
arr = {2, 1, 1, 4, 5, 8, 2, 3}
-> sort arr -> {1, 1, 2, 2, 3, 4, 5, 8}
-> reverse arr -> {8, 5, 4, 3, 2, 2, 1, 1}
return {"Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"}
If the vector is empty, return an empty vector:
arr = {}
return {}
If the vector has any strange number ignore it:
arr = {1, -1 , 55}
-> sort arr -> {-1, 1, 55}
-> reverse arr -> {55, 1, -1}
return = {"One"}
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
vector<string> by_length(vector<int> arr){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<string> by_length(vector<int> arr){
|
[
" map<int,string> numto={{0,\"Zero\"},{1,\"One\"},{2,\"Two\"},{3,\"Three\"},{4,\"Four\"},{5,",
"string> out={};\n for (int i=arr.size()-1;",
"[i]>=1 and arr[i]<=9)\n out.push_back(numto[arr[i]]);\n return out;\n}\n"
] |
[
"\"Five\"},{6,\"Six\"},{7,\"Seven\"},{8,\"Eight\"},{9,\"Nine\"}};\n sort(arr.begin(),arr.end());\n vector<",
"i>=0;i-=1)\n if (arr"
] |
map<int,string> numto={{0,"Zero"},{1,"One"},{2,"Two"},{3,"Three"},{4,"Four"},{5,"Five"},{6,"Six"},{7,"Seven"},{8,"Eight"},{9,"Nine"}};
sort(arr.begin(),arr.end());
vector<string> out={};
for (int i=arr.size()-1;i>=0;i-=1)
if (arr[i]>=1 and arr[i]<=9)
out.push_back(numto[arr[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(by_length({2, 1, 1, 4, 5, 8, 2, 3}) , {"Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"}));
assert (issame(by_length({}) , {}));
assert (issame(by_length({1, -1 , 55}) , {"One"}));
assert (issame(by_length({1, -1, 3, 2}) , {"Three", "Two", "One"}));
assert (issame(by_length({9, 4, 8}) , {"Nine", "Eight", "Four"}));
}
|
CPP/106_spans_2
| 2
|
/*
Implement the function f that takes n as a parameter,
and returns a vector of size n, such that the value of the element at index i is the factorial of i if i is even
or the sum of numbers from 1 to i otherwise.
i starts from 1.
the factorial of i is the multiplication of the numbers from 1 to i (1 * 2 * ... * i).
Example:
f(5) == {1, 2, 6, 24, 15}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> f(int n){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> f(int n){
|
[
" int sum=0,prod",
" if (i%2==0) out.",
"eturn out;\n}\n"
] |
[
"=1;\n vector<int> out={};\n for (int i=1;i<=n;i++)\n {\n sum+=i;\n prod*=i;\n ",
"push_back(prod);\n else out.push_back(sum);\n } \n r"
] |
int sum=0,prod=1;
vector<int> out={};
for (int i=1;i<=n;i++)
{
sum+=i;
prod*=i;
if (i%2==0) out.push_back(prod);
else out.push_back(sum);
}
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(f(5) , {1, 2, 6, 24, 15}));
assert (issame(f(7) , {1, 2, 6, 24, 15, 720, 28}));
assert (issame(f(1) , {1}));
assert (issame(f(3) , {1, 2, 6}));
}
|
CPP/107_spans_2
| 2
|
/*
Given a positive integer n, return a vector that has the number of even and odd
integer palindromes that fall within the range(1, n), inclusive.
Example 1:
Input: 3
Output: (1, 2)
Explanation:
Integer palindrome are 1, 2, 3. one of them is even, and two of them are odd.
Example 2:
Input: 12
Output: (4, 6)
Explanation:
Integer palindrome are 1, 2, 3, 4, 5, 6, 7, 8, 9, 11. four of them are even, and 6 of them are odd.
Note:
1. 1 <= n <= 10^3
2. returned vector has the number of even and odd integer palindromes respectively.
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<int> even_odd_palindrome(int n){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> even_odd_palindrome(int n){
|
[
" int num1=0,num2=0;\n for (in",
" {\n string w",
" if (w==p and i%2==0) num2+=1;\n \n }\n return {num2,num1};\n}\n"
] |
[
"t i=1;i<=n;i++)\n ",
"=to_string(i);\n string p(w.rbegin(),w.rend());\n if (w==p and i%2==1) num1+=1;\n "
] |
int num1=0,num2=0;
for (int i=1;i<=n;i++)
{
string w=to_string(i);
string p(w.rbegin(),w.rend());
if (w==p and i%2==1) num1+=1;
if (w==p and i%2==0) num2+=1;
}
return {num2,num1};
}
|
#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_palindrome(123) , {8, 13}));
assert (issame(even_odd_palindrome(12) , {4, 6}));
assert (issame(even_odd_palindrome(3) , {1, 2}));
assert (issame(even_odd_palindrome(63) , {6, 8}));
assert (issame(even_odd_palindrome(25) , {5, 6}));
assert (issame(even_odd_palindrome(19) , {4, 6}));
assert (issame(even_odd_palindrome(9) , {4, 5}));
assert (issame(even_odd_palindrome(1) , {0, 1}));
}
|
CPP/108_spans_2
| 2
|
/*
Write a function count_nums which takes a vector of integers and returns
the number of elements which has a sum of digits > 0.
If a number is negative, then its first signed digit will be negative:
e.g. -123 has signed digits -1, 2, and 3.
>>> count_nums({}) == 0
>>> count_nums({-1, 11, -11}) == 1
>>> count_nums({1, 1, 2}) == 3
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
int count_nums(vector<int> n){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int count_nums(vector<int> n){
|
[
" int num=0;\n for (int i=0;i<n.size();i++)\n if (n[i]>0) num+=1;\n else\n {\n int sum=0;\n int w;\n w=abs(n[i]);\n ",
"w=w/10;",
" if (sum>0) num+=1;\n }\n return num;\n}\n"
] |
[
" while (w>=10)\n {\n sum+=w%10;\n ",
"\n }\n sum-=w;\n "
] |
int num=0;
for (int i=0;i<n.size();i++)
if (n[i]>0) num+=1;
else
{
int sum=0;
int w;
w=abs(n[i]);
while (w>=10)
{
sum+=w%10;
w=w/10;
}
sum-=w;
if (sum>0) num+=1;
}
return num;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (count_nums({}) == 0);
assert (count_nums({-1, -2, 0}) == 0);
assert (count_nums({1, 1, 2, -2, 3, 4, 5}) == 6);
assert (count_nums({1, 6, 9, -6, 0, 1, 5}) == 5);
assert (count_nums({1, 100, 98, -7, 1, -1}) == 4);
assert (count_nums({12, 23, 34, -45, -56, 0}) == 5);
assert (count_nums({-0, 1}) == 1);
assert (count_nums({1}) == 1);
}
|
CPP/109_spans_2
| 2
|
/*
We have a vector "arr" of N integers arr[1], arr[2], ..., arr[N].The
numbers in the vector will be randomly ordered. Your task is to determine if
it is possible to get a vector sorted in non-decreasing order by performing
the following operation on the given vector:
You are allowed to perform right shift operation any number of times.
One right shift operation means shifting all elements of the vector by one
position in the right direction. The last element of the vector will be moved to
the starting position in the vector i.e. 0th index.
If it is possible to obtain the sorted vector by performing the above operation
then return true else return false.
If the given vector is empty then return true.
Note: The given vector is guaranteed to have unique elements.
For Example:
move_one_ball({3, 4, 5, 1, 2})==>true
Explanation: By performing 2 right shift operations, non-decreasing order can
be achieved for the given vector.
move_one_ball({3, 5, 4, 1, 2})==>false
Explanation:It is not possible to get non-decreasing order for the given
vector by performing any number of right shift operations.
*/
#include<stdio.h>
#include<vector>
using namespace std;
bool move_one_ball(vector<int> arr){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool move_one_ball(vector<int> arr){
|
[
" ",
"rr.size();i++)\n if (arr[i]<arr[i-1]) num+=1;\n if (arr[arr.size()-1]>arr[0]) num+=1;\n i",
"\n return false;\n}\n"
] |
[
" int num=0;\n if (arr.size()==0) return true;\n for (int i=1;i<a",
"f (num<2) return true;"
] |
int num=0;
if (arr.size()==0) return true;
for (int i=1;i<arr.size();i++)
if (arr[i]<arr[i-1]) num+=1;
if (arr[arr.size()-1]>arr[0]) num+=1;
if (num<2) return true;
return false;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (move_one_ball({3, 4, 5, 1, 2})==true);
assert (move_one_ball({3, 5, 10, 1, 2})==true);
assert (move_one_ball({4, 3, 1, 2})==false);
assert (move_one_ball({3, 5, 4, 1, 2})==false);
assert (move_one_ball({})==true);
}
|
CPP/110_spans_2
| 2
|
/*
In this problem, you will implement a function that takes two vectors of numbers,
and determines whether it is possible to perform an exchange of elements
between them to make lst1 a vector of only even numbers.
There is no limit on the number of exchanged elements between lst1 and lst2.
If it is possible to exchange elements between the lst1 and lst2 to make
all the elements of lst1 to be even, return "YES".
Otherwise, return "NO".
For example:
exchange({1, 2, 3, 4}, {1, 2, 3, 4}) => "YES"
exchange({1, 2, 3, 4}, {1, 5, 3, 4}) => "NO"
It is assumed that the input vectors will be non-empty.
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
string exchange(vector<int> lst1,vector<int> lst2){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string exchange(vector<int> lst1,vector<int> lst2){
|
[
" int num=0;\n for (int i=0;i<lst1.size();i++)\n if (ls",
" for ",
"+)\n if (lst2[i]%2==0) num+=1;\n if (num>=lst1.size()) return \"YES\";\n return \"NO\";\n}\n"
] |
[
"t1[i]%2==0) num+=1;\n ",
"(int i=0;i<lst2.size();i+"
] |
int num=0;
for (int i=0;i<lst1.size();i++)
if (lst1[i]%2==0) num+=1;
for (int i=0;i<lst2.size();i++)
if (lst2[i]%2==0) num+=1;
if (num>=lst1.size()) return "YES";
return "NO";
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (exchange({1, 2, 3, 4}, {1, 2, 3, 4}) == "YES");
assert (exchange({1, 2, 3, 4}, {1, 5, 3, 4}) == "NO");
assert (exchange({1, 2, 3, 4}, {2, 1, 4, 3}) == "YES" );
assert (exchange({5, 7, 3}, {2, 6, 4}) == "YES");
assert (exchange({5, 7, 3}, {2, 6, 3}) == "NO" );
assert (exchange({3, 2, 6, 1, 8, 9}, {3, 5, 5, 1, 1, 1}) == "NO");
assert (exchange({100, 200}, {200, 200}) == "YES");
}
|
CPP/111_spans_2
| 2
|
/*
Given a string representing a space separated lowercase letters, return a map
of the letter with the most repetition and containing the corresponding count.
If several letters have the same occurrence, return all of them.
Example:
histogram("a b c") == {{"a", 1}, {"b", 1}, {"c", 1}}
histogram("a b b a") == {{"a", 2}, {"b", 2}}
histogram("a b c a b") == {{"a", 2}, {"b", 2}}
histogram("b b b b a") == {{"b", 4}}
histogram("") == {}
*/
#include<stdio.h>
#include<string>
#include<map>
using namespace std;
map<char,int> histogram(string test){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<map>
using namespace std;
#include<algorithm>
#include<stdlib.h>
map<char,int> histogram(string test){
|
[
" map<char,int> count={},out={};\n map <char,int>::iterator it;\n int max=0;\n for (int i=0;i<test.length",
"]+=1;\n if (count[test[i]]>max) max=count[test[i]];\n }\n for (it=count.begin();it!=count.end();it++)\n",
" int w2=it->second;\n if (w2==max) out[w1]=w2;\n }\n return out;\n}\n"
] |
[
"();i++)\n if (test[i]!=' ')\n {\n count[test[i]",
" {\n char w1=it->first;\n "
] |
map<char,int> count={},out={};
map <char,int>::iterator it;
int max=0;
for (int i=0;i<test.length();i++)
if (test[i]!=' ')
{
count[test[i]]+=1;
if (count[test[i]]>max) max=count[test[i]];
}
for (it=count.begin();it!=count.end();it++)
{
char w1=it->first;
int w2=it->second;
if (w2==max) out[w1]=w2;
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
bool issame(map<char,int> a,map<char,int> b){
if (a.size()!=b.size()) return false;
map <char,int>::iterator it;
for (it=a.begin();it!=a.end();it++)
{
char w1=it->first;
int w2=it->second;
if (b.find(w1)==b.end()) return false;
if (b[w1]!=w2) return false;
}
return true;
}
int main(){
assert (issame(histogram("a b b a") , {{'a',2},{'b', 2}}));
assert (issame(histogram("a b c a b") , {{'a', 2},{'b', 2}}));
assert (issame(histogram("a b c d g") , {{'a', 1}, {'b', 1}, {'c', 1}, {'d', 1}, {'g', 1}}));
assert (issame(histogram("r t g") , {{'r', 1},{'t', 1},{'g', 1}}));
assert (issame(histogram("b b b b a") , {{'b', 4}}));
assert (issame(histogram("r t g") , {{'r', 1},{'t', 1},{'g', 1}}));
assert (issame(histogram("") , {}));
assert (issame(histogram("a") , {{'a', 1}}));
}
|
CPP/112_spans_2
| 2
|
/*
Task
We are given two strings s and c, you have to deleted all the characters in s that are equal to any character in c
then check if the result string is palindrome.
A string is called palindrome if it reads the same backward as forward.
You should return a vector containing the result string and "True"/"False" for the check.
Example
For s = "abcde", c = "ae", the result should be ("bcd","False")
For s = "abcdef", c = "b" the result should be ("acdef","False")
For s = "abcdedcba", c = "ab", the result should be ("cdedc","True")
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
vector<string> reverse_delete(string s,string c){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<string> reverse_delete(string s,string c){
|
[
" string n=\"\";\n for (int i=0;i<s.length();i++)\n if (find(c.begin(),c.end(),s[i])==c.end())\n n=n+s[i]; \n if (n.length()==0",
"ng",
"n,\"False\"};\n}\n"
] |
[
") return {n,\"True\"};\n stri",
" w(n.rbegin(),n.rend());\n if (w==n) return {n,\"True\"};\n return {"
] |
string n="";
for (int i=0;i<s.length();i++)
if (find(c.begin(),c.end(),s[i])==c.end())
n=n+s[i];
if (n.length()==0) return {n,"True"};
string w(n.rbegin(),n.rend());
if (w==n) return {n,"True"};
return {n,"False"};
}
|
#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(reverse_delete("abcde","ae") , {"bcd","False"}));
assert (issame(reverse_delete("abcdef", "b") , {"acdef","False"}));
assert (issame(reverse_delete("abcdedcba","ab") , {"cdedc","True"}));
assert (issame(reverse_delete("dwik","w") , {"dik","False"}));
assert (issame(reverse_delete("a","a") , {"","True"}));
assert (issame(reverse_delete("abcdedcba","") , {"abcdedcba","True"}));
assert (issame(reverse_delete("abcdedcba","v") , {"abcdedcba","True"}));
assert (issame(reverse_delete("vabba","v") , {"abba","True"}));
assert (issame(reverse_delete("mamma", "mia") , {"", "True"}));
}
|
CPP/113_spans_2
| 2
|
/*
Given a vector of strings, where each string consists of only digits, return a vector.
Each element i of the output should be 'the number of odd elements in the
string i of the input." where all the i's should be replaced by the number
of odd digits in the i'th string of the input.
>>> odd_count({"1234567"})
{'the number of odd elements 4n the str4ng 4 of the 4nput."}
>>> odd_count({"3","11111111"})
{'the number of odd elements 1n the str1ng 1 of the 1nput.",
'the number of odd elements 8n the str8ng 8 of the 8nput."}
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<map>
using namespace std;
vector<string> odd_count(vector<string> lst){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<map>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<string> odd_count(vector<string> lst){
|
[
" vector<string> out={};\n for (int i=0;i<lst.size();i++)\n {\n int s",
")\n if (lst[i][j]>=48 and lst[i][j]<=57 and lst[i][j]%2==1)\n sum+=1;\n ",
"string i of the input.\";\n string s2=\"\";\n for (int j=0;j<s.length();j++)\n if (s[j]=='i') s2=s2+to_string(sum);\n else s2=s2+s[j];\n out.push_back(s2);\n }\n return out;\n}\n"
] |
[
"um=0;\n for (int j=0;j<lst[i].length();j++",
" string s=\"the number of odd elements in the "
] |
vector<string> out={};
for (int i=0;i<lst.size();i++)
{
int sum=0;
for (int j=0;j<lst[i].length();j++)
if (lst[i][j]>=48 and lst[i][j]<=57 and lst[i][j]%2==1)
sum+=1;
string s="the number of odd elements in the string i of the input.";
string s2="";
for (int j=0;j<s.length();j++)
if (s[j]=='i') s2=s2+to_string(sum);
else s2=s2+s[j];
out.push_back(s2);
}
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(odd_count({"1234567"}) , {"the number of odd elements 4n the str4ng 4 of the 4nput."}));
assert (issame(odd_count({"3","11111111"}) , {"the number of odd elements 1n the str1ng 1 of the 1nput.", "the number of odd elements 8n the str8ng 8 of the 8nput."}));
assert (issame(odd_count({"271", "137", "314"}) , {
"the number of odd elements 2n the str2ng 2 of the 2nput.",
"the number of odd elements 3n the str3ng 3 of the 3nput.",
"the number of odd elements 2n the str2ng 2 of the 2nput."
}));
}
|
CPP/114_spans_2
| 2
|
/*
Given a vector of integers nums, find the minimum sum of any non-empty sub-vector
of nums.
Example
minSubArraySum({2, 3, 4, 1, 2, 4}) == 1
minSubArraySum({-1, -2, -3}) == -6
*/
#include<stdio.h>
#include<vector>
using namespace std;
long long minSubArraySum(vector<long long> nums){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
long long minSubArraySum(vector<long long> nums){
|
[
" long long current,min;\n current=nums[0];\n min=nu",
" {\n if (current<0) current=cu",
"<min) min=current;\n }\n return min;\n}\n"
] |
[
"ms[0];\n for (int i=1;i<nums.size();i++)\n",
"rrent+nums[i];\n else current=nums[i];\n if (current"
] |
long long current,min;
current=nums[0];
min=nums[0];
for (int i=1;i<nums.size();i++)
{
if (current<0) current=current+nums[i];
else current=nums[i];
if (current<min) min=current;
}
return min;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (minSubArraySum({2, 3, 4, 1, 2, 4}) == 1);
assert (minSubArraySum({-1, -2, -3}) == -6);
assert (minSubArraySum({-1, -2, -3, 2, -10}) == -14);
assert (minSubArraySum({-9999999999999999}) == -9999999999999999);
assert (minSubArraySum({0, 10, 20, 1000000}) == 0);
assert (minSubArraySum({-1, -2, -3, 10, -5}) == -6);
assert (minSubArraySum({100, -1, -2, -3, 10, -5}) == -6);
assert (minSubArraySum({10, 11, 13, 8, 3, 4}) == 3);
assert (minSubArraySum({100, -33, 32, -1, 0, -2}) == -33);
assert (minSubArraySum({-10}) == -10);
assert (minSubArraySum({7}) == 7);
assert (minSubArraySum({1, -1}) == -1);
}
|
CPP/115_spans_2
| 2
|
/*
You are given a rectangular grid of wells. Each row represents a single well,
and each 1 in a row represents a single unit of water.
Each well has a corresponding bucket that can be used to extract water from it,
and all buckets have the same capacity.
Your task is to use the buckets to empty the wells.
Output the number of times you need to lower the buckets.
Example 1:
Input:
grid : {{0,0,1,0}, {0,1,0,0}, {1,1,1,1}}
bucket_capacity : 1
Output: 6
Example 2:
Input:
grid : {{0,0,1,1}, {0,0,0,0}, {1,1,1,1}, {0,1,1,1}}
bucket_capacity : 2
Output: 5
Example 3:
Input:
grid : {{0,0,0}, {0,0,0}}
bucket_capacity : 5
Output: 0
Constraints:
* all wells have the same length
* 1 <= grid.length <= 10^2
* 1 <= grid{:,1}.length <= 10^2
* grid{i}{j} -> 0 | 1
* 1 <= capacity <= 10
*/
#include<stdio.h>
#include<vector>
using namespace std;
int max_fill(vector<vector<int>> grid,int capacity){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int max_fill(vector<vector<int>> grid,int capacity){
|
[
" int out=0;\n for (int i=0;i<grid.size();i++)\n {\n ",
"f ",
""
] |
[
" int sum=0;\n for (int j=0;j<grid[i].size();j++)\n sum+=grid[i][j];\n i",
"(sum>0) out+=(sum-1)/capacity+1;\n }\n return out;\n}\n"
] |
int out=0;
for (int i=0;i<grid.size();i++)
{
int sum=0;
for (int j=0;j<grid[i].size();j++)
sum+=grid[i][j];
if (sum>0) out+=(sum-1)/capacity+1;
}
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (max_fill({{0,0,1,0}, {0,1,0,0}, {1,1,1,1}}, 1) == 6);
assert (max_fill({{0,0,1,1}, {0,0,0,0}, {1,1,1,1}, {0,1,1,1}}, 2) == 5);
assert (max_fill({{0,0,0}, {0,0,0}}, 5) == 0);
assert (max_fill({{1,1,1,1}, {1,1,1,1}}, 2) == 4);
assert (max_fill({{1,1,1,1}, {1,1,1,1}}, 9) == 2);
}
|
CPP/116_spans_2
| 2
|
/*
In this Kata, you have to sort a vector of non-negative integers according to
number of ones in their binary representation in ascending order.
For similar number of ones, sort based on decimal value.
It must be implemented like this:
>>> sort_vector({1, 5, 2, 3, 4}) == {1, 2, 3, 4, 5}
>>> sort_vector({-2, -3, -4, -5, -6}) == {-6, -5, -4, -3, -2}
>>> sort_vector({1, 0, 2, 3, 4}) == {0, 1, 2, 3, 4}
*/
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> sort_array(vector<int> arr){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> sort_array(vector<int> arr){
|
[
" vector<int> bin={};\n int m;\n\n for ",
"b=0,n=abs(arr[i]);\n while (n>0)\n {\n b+=n%2;n=n/2;\n }\n bin.push_back(b);\n }\n for (int i=0;i<arr.size();i++)\n for (int j=1;j<arr.size();j++)\n if (bin[j]<bin[j-1] or (bin[j]==bin[j-1] and arr[j]<arr[j-1]))\n {\n m=arr[j];arr[j]=arr[j-1];arr[j-1]=m",
"];bin[j]=bin[j-1];bin[j-1]=m;\n }\n return arr;\n}\n"
] |
[
"(int i=0;i<arr.size();i++)\n {\n int ",
";\n m=bin[j"
] |
vector<int> bin={};
int m;
for (int i=0;i<arr.size();i++)
{
int b=0,n=abs(arr[i]);
while (n>0)
{
b+=n%2;n=n/2;
}
bin.push_back(b);
}
for (int i=0;i<arr.size();i++)
for (int j=1;j<arr.size();j++)
if (bin[j]<bin[j-1] or (bin[j]==bin[j-1] and arr[j]<arr[j-1]))
{
m=arr[j];arr[j]=arr[j-1];arr[j-1]=m;
m=bin[j];bin[j]=bin[j-1];bin[j-1]=m;
}
return arr;
}
|
#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_array({1,5,2,3,4}) , {1, 2, 4, 3, 5}));
assert (issame(sort_array({-2,-3,-4,-5,-6}) , {-4, -2, -6, -5, -3}));
assert (issame(sort_array({1,0,2,3,4}) , {0, 1, 2, 4, 3}));
assert (issame(sort_array({}) , {}));
assert (issame(sort_array({2,5,77,4,5,3,5,7,2,3,4}) , {2, 2, 4, 4, 3, 3, 5, 5, 5, 7, 77}));
assert (issame(sort_array({3,6,44,12,32,5}) , {32, 3, 5, 6, 12, 44}));
assert (issame(sort_array({2,4,8,16,32}) , {2, 4, 8, 16, 32}));
assert (issame(sort_array({2,4,8,16,32}) , {2, 4, 8, 16, 32}));
}
|
CPP/117_spans_2
| 2
|
/*
Given a string s and a natural number n, you have been tasked to implement
a function that returns a vector of all words from string s that contain exactly
n consonants, in order these words appear in the string s.
If the string s is empty then the function should return an empty vector.
Note: you may assume the input string contains only letters and spaces.
Examples:
select_words("Mary had a little lamb", 4) ==> {"little"}
select_words("Mary had a little lamb", 3) ==> {"Mary", "lamb"}
select_words('simple white space", 2) ==> {}
select_words("Hello world", 4) ==> {"world"}
select_words("Uncle sam", 3) ==> {"Uncle"}
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
vector<string> select_words(string s,int n){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<string> select_words(string s,int n){
|
[
" string vowels=\"aeiouAEIOU\";\n string current=\"\";\n vector<string> out={};\n int nu",
" ')\n {\n if (numc==n) out.push_back(current);\n current=\"\";\n ",
" else\n {\n current=current+s[i];\n if ((s[i]>=65 and s[i]<=90) or (s[i]>=97 and s[i]<=122))\n if (find(vowels.begin(),vowels.end(),s[i])==vowels.end())\n numc+=1;\n }\n return out;\n}\n"
] |
[
"mc=0;\n s=s+' ';\n for (int i=0;i<s.length();i++)\n if (s[i]=='",
" numc=0;\n }\n "
] |
string vowels="aeiouAEIOU";
string current="";
vector<string> out={};
int numc=0;
s=s+' ';
for (int i=0;i<s.length();i++)
if (s[i]==' ')
{
if (numc==n) out.push_back(current);
current="";
numc=0;
}
else
{
current=current+s[i];
if ((s[i]>=65 and s[i]<=90) or (s[i]>=97 and s[i]<=122))
if (find(vowels.begin(),vowels.end(),s[i])==vowels.end())
numc+=1;
}
return out;
}
|
#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(select_words("Mary had a little lamb", 4) , {"little"} ));
assert (issame(select_words("Mary had a little lamb", 3) , {"Mary", "lamb"} ));
assert (issame(select_words("simple white space", 2) , {} ));
assert (issame(select_words("Hello world", 4) , {"world"} ));
assert (issame(select_words("Uncle sam", 3) , {"Uncle"}));
assert (issame(select_words("", 4) , {}));
assert (issame(select_words("a b c d e f", 1) , {"b", "c", "d", "f"}));
}
|
CPP/118_spans_2
| 2
|
/*
You are given a word. Your task is to find the closest vowel that stands between
two consonants from the right side of the word (case sensitive).
Vowels in the beginning and ending doesn't count. Return empty string if you didn't
find any vowel met the above condition.
You may assume that the given string contains English letter only.
Example:
get_closest_vowel("yogurt") ==> "u"
get_closest_vowel("FULL") ==> "U"
get_closest_vowel("quick") ==> ""
get_closest_vowel("ab") ==> ""
*/
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
string get_closest_vowel(string word){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
string get_closest_vowel(string word){
|
[
" string ou",
"ls=\"AEIOUaeiou\";\n for (int i=word.length()-2;i>=1;i-=1)\n if (find(vowels.begin(),vowels.end(),word[i])!=vowels.end())\n if (find(vowels.begin(),vowels.end(),word[i+1])==vowels.end())\n if (find(vowels.",
"1])==vowels.end())\n return out+word[i];\n return out;\n}\n"
] |
[
"t=\"\";\n string vowe",
"begin(),vowels.end(),word[i-"
] |
string out="";
string vowels="AEIOUaeiou";
for (int i=word.length()-2;i>=1;i-=1)
if (find(vowels.begin(),vowels.end(),word[i])!=vowels.end())
if (find(vowels.begin(),vowels.end(),word[i+1])==vowels.end())
if (find(vowels.begin(),vowels.end(),word[i-1])==vowels.end())
return out+word[i];
return out;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (get_closest_vowel("yogurt") == "u");
assert (get_closest_vowel("full") == "u");
assert (get_closest_vowel("easy") == "");
assert (get_closest_vowel("eAsy") == "");
assert (get_closest_vowel("ali") == "");
assert (get_closest_vowel("bad") == "a");
assert (get_closest_vowel("most") =="o");
assert (get_closest_vowel("ab") == "");
assert (get_closest_vowel("ba") == "");
assert (get_closest_vowel("quick") == "");
assert (get_closest_vowel("anime") == "i");
assert (get_closest_vowel("Asia") == "");
assert (get_closest_vowel("Above") == "o");
}
|
CPP/119_spans_2
| 2
|
/*
You are given a vector of two strings, both strings consist of open
parentheses '(' or close parentheses ')' only.
Your job is to check if it is possible to concatenate the two strings in
some order, that the resulting string will be good.
A string S is considered to be good if and only if all parentheses in S
are balanced. For example: the string "(())()" is good, while the string
"())" is not.
Return "Yes" if there's a way to make a good string, and return "No" otherwise.
Examples:
match_parens({"()(", ")"}) == "Yes"
match_parens({")", ")"}) == "No"
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
string match_parens(vector<string> lst){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string match_parens(vector<string> lst){
|
[
" string l1=lst[0]+lst[1];\n ",
"rue;\n for (i=0;i<l1.length();i++)\n {\n if (l1[i]=='(') count+=1;\n if (l1[i]==')') cou",
" }\n if (count!=0) return \"No\";\n if (can==true) return \"Yes\";\n l1=lst[1]+lst[0];\n can=true;\n for (i=0;i<l1.length();i++)\n {\n if (l1[i]=='(') count+=1;\n if (l1[i]==')') count-=1;\n if (count<0) can=false;\n }\n if (can==true) return \"Yes\";\n return \"No\";\n}\n"
] |
[
" int i,count=0;\n bool can=t",
"nt-=1;\n if (count<0) can=false;\n "
] |
string l1=lst[0]+lst[1];
int i,count=0;
bool can=true;
for (i=0;i<l1.length();i++)
{
if (l1[i]=='(') count+=1;
if (l1[i]==')') count-=1;
if (count<0) can=false;
}
if (count!=0) return "No";
if (can==true) return "Yes";
l1=lst[1]+lst[0];
can=true;
for (i=0;i<l1.length();i++)
{
if (l1[i]=='(') count+=1;
if (l1[i]==')') count-=1;
if (count<0) can=false;
}
if (can==true) return "Yes";
return "No";
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (match_parens({"()(", ")"}) == "Yes");
assert (match_parens({")", ")"}) == "No");
assert (match_parens({"(()(())", "())())"}) == "No");
assert (match_parens({")())", "(()()("}) == "Yes");
assert (match_parens({"(())))", "(()())(("}) == "Yes");
assert (match_parens({"()", "())"}) == "No");
assert (match_parens({"(()(", "()))()"}) == "Yes");
assert (match_parens({"((((", "((())"}) == "No");
assert (match_parens({")(()", "(()("}) == "No");
assert (match_parens({")(", ")("}) == "No");
assert (match_parens({"(", ")"}) == "Yes");
assert (match_parens({")", "("}) == "Yes" );
}
|
CPP/120_spans_2
| 2
|
/*
Given a vector arr of integers and a positive integer k, return a sorted vector
of length k with the maximum k numbers in arr.
Example 1:
Input: arr = {-3, -4, 5}, k = 3
Output: {-4, -3, 5}
Example 2:
Input: arr = {4, -4, 4}, k = 2
Output: {4, 4}
Example 3:
Input: arr = {-3, 2, 1, 2, -1, -2, 1}, k = 1
Output: {2}
Note:
1. The length of the vector will be in the range of {1, 1000}.
2. The elements in the vector will be in the range of {-1000, 1000}.
3. 0 <= k <= len(arr)
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> maximum(vector<int> arr,int k){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> maximum(vector<int> arr,int k){
|
[
" sort(arr.be",
"\n ",
"turn out;\n}\n"
] |
[
"gin(),arr.end());",
" vector<int> out(arr.end()-k,arr.end());\n re"
] |
sort(arr.begin(),arr.end());
vector<int> out(arr.end()-k,arr.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(maximum({-3, -4, 5}, 3) , {-4, -3, 5}));
assert (issame(maximum({4, -4, 4}, 2) , {4, 4}));
assert (issame(maximum({-3, 2, 1, 2, -1, -2, 1}, 1) , {2}));
assert (issame(maximum({123, -123, 20, 0 , 1, 2, -3}, 3) , {2, 20, 123}));
assert (issame(maximum({-123, 20, 0 , 1, 2, -3}, 4) , {0, 1, 2, 20}));
assert (issame(maximum({5, 15, 0, 3, -13, -8, 0}, 7) , {-13, -8, 0, 0, 3, 5, 15}));
assert (issame(maximum({-1, 0, 2, 5, 3, -10}, 2) , {3, 5}));
assert (issame(maximum({1, 0, 5, -7}, 1) , {5}));
assert (issame(maximum({4, -4}, 2) , {-4, 4}));
assert (issame(maximum({-10, 10}, 2) , {-10, 10}));
assert (issame(maximum({1, 2, 3, -23, 243, -400, 0}, 0) , {}));
}
|
CPP/121_spans_2
| 2
|
/*
Given a non-empty vector of integers, return the sum of all of the odd elements that are in even positions.
Examples
solution({5, 8, 7, 1}) ==> 12
solution({3, 3, 3, 3, 3}) ==> 9
solution({30, 13, 24, 321}) ==>0
*/
#include<stdio.h>
#include<vector>
using namespace std;
int solutions(vector<int> lst){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int solutions(vector<int> lst){
|
[
" ",
"t.size();i++)\n if (lst[",
";\n return sum;\n}\n"
] |
[
" int sum=0;\n for (int i=0;i*2<ls",
"i*2]%2==1) sum+=lst[i*2]"
] |
int sum=0;
for (int i=0;i*2<lst.size();i++)
if (lst[i*2]%2==1) sum+=lst[i*2];
return sum;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (solutions({5, 8, 7, 1}) == 12);
assert (solutions({3, 3, 3, 3, 3}) == 9);
assert (solutions({30, 13, 24, 321}) == 0);
assert (solutions({5, 9}) == 5);
assert (solutions({2, 4, 8}) == 0);
assert (solutions({30, 13, 23, 32}) == 23);
assert (solutions({3, 13, 2, 9}) == 3);
}
|
CPP/122_spans_2
| 2
|
/*
Given a non-empty vector of integers arr and an integer k, return
the sum of the elements with at most two digits from the first k elements of arr.
Example:
Input: arr = {111,21,3,4000,5,6,7,8,9}, k = 4
Output: 24 # sum of 21 + 3
Constraints:
1. 1 <= len(arr) <= 100
2. 1 <= k <= len(arr)
*/
#include<stdio.h>
#include<vector>
using namespace std;
int add_elements(vector<int> arr,int k){
|
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int add_elements(vector<int> arr,int k){
|
[
" int sum=0;\n for (int i=0;i<",
"arr[i]<=99)\n ",
"i];\n return sum;\n}\n"
] |
[
"k;i++)\n if( arr[i]>=-99 and ",
" sum+=arr["
] |
int sum=0;
for (int i=0;i<k;i++)
if( arr[i]>=-99 and arr[i]<=99)
sum+=arr[i];
return sum;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (add_elements({1,-2,-3,41,57,76,87,88,99}, 3) == -4);
assert (add_elements({111,121,3,4000,5,6}, 2) == 0);
assert (add_elements({11,21,3,90,5,6,7,8,9}, 4) == 125);
assert (add_elements({111,21,3,4000,5,6,7,8,9}, 4) == 24);
assert (add_elements({1}, 1) == 1);
}
|
CPP/123_spans_2
| 2
|
/*
Given a positive integer n, return a sorted vector that has the odd numbers in collatz sequence.
The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined
as follows: start with any positive integer n. Then each term is obtained from the
previous term as follows: if the previous term is even, the next term is one half of
the previous term. If the previous term is odd, the next term is 3 times the previous
term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1.
Note:
1. Collatz(1) is {1}.
2. returned vector sorted in increasing order.
For example:
get_odd_collatz(5) returns {1, 5} // The collatz sequence for 5 is {5, 16, 8, 4, 2, 1}, so the odd numbers are only 1, and 5.
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> get_odd_collatz(int n){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> get_odd_collatz(int n){
|
[
" vector<int",
"n%2==1) {out.push_back(n); n=n*3+1;}\n ",
" return out;\n}\n"
] |
[
"> out={1};\n while (n!=1)\n {\n if (",
" else n=n/2;\n }\n sort(out.begin(),out.end());\n "
] |
vector<int> out={1};
while (n!=1)
{
if (n%2==1) {out.push_back(n); n=n*3+1;}
else n=n/2;
}
sort(out.begin(),out.end());
return out;
}
|
#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(get_odd_collatz(14) , {1, 5, 7, 11, 13, 17}));
assert (issame(get_odd_collatz(5) , {1, 5}));
assert (issame(get_odd_collatz(12) , {1, 3, 5}));
assert (issame(get_odd_collatz(1) , {1}));
}
|
CPP/124_spans_2
| 2
|
/*
You have to write a function which validates a given date string and
returns true if the date is valid otherwise false.
The date is valid if all of the following rules are satisfied:
1. The date string is not empty.
2. The number of days is not less than 1 or higher than 31 days for months 1,3,5,7,8,10,12. And the number of days is not less than 1 or higher than 30 days for months 4,6,9,11. And, the number of days is not less than 1 or higher than 29 for the month 2.
3. The months should not be less than 1 or higher than 12.
4. The date should be in the format: mm-dd-yyyy
for example:
valid_date("03-11-2000") => true
valid_date("15-01-2012") => false
valid_date("04-0-2040") => false
valid_date("06-04-2020") => true
valid_date("06/04/2020") => false
*/
#include<stdio.h>
#include<string>
using namespace std;
bool valid_date(string date){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
bool valid_date(string date){
|
[
" int mm,dd,yy,i;\n if (date.length()!=10) return false;\n for (int i=0;i<10;i++)\n if (i==2 or i==5)\n {\n if (date[i]!='-') return false;\n }\n else\n if (date[i]<48 or date[i]>57) return false;\n\n mm=atoi(date.substr(0,2).c_str());\n dd=atoi(date.substr(3,2).c_st",
"yy=atoi(date.substr(6,4).c_str());\n if (mm<1 or mm>12) return false;\n if (dd<1 or dd>31) ",
") return false;\n if (dd==30 and mm==2) return false;\n return true;\n\n}\n"
] |
[
"r());\n ",
"return false;\n if (dd==31 and (mm==4 or mm==6 or mm==9 or mm==11 or mm==2)"
] |
int mm,dd,yy,i;
if (date.length()!=10) return false;
for (int i=0;i<10;i++)
if (i==2 or i==5)
{
if (date[i]!='-') return false;
}
else
if (date[i]<48 or date[i]>57) return false;
mm=atoi(date.substr(0,2).c_str());
dd=atoi(date.substr(3,2).c_str());
yy=atoi(date.substr(6,4).c_str());
if (mm<1 or mm>12) return false;
if (dd<1 or dd>31) return false;
if (dd==31 and (mm==4 or mm==6 or mm==9 or mm==11 or mm==2)) return false;
if (dd==30 and mm==2) return false;
return true;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (valid_date("03-11-2000") == true);
assert (valid_date("15-01-2012") == false);
assert (valid_date("04-0-2040") == false);
assert (valid_date("06-04-2020") == true);
assert (valid_date("01-01-2007") == true);
assert (valid_date("03-32-2011") == false);
assert (valid_date("") == false);
assert (valid_date("04-31-3000") == false);
assert (valid_date("06-06-2005") == true);
assert (valid_date("21-31-2000") == false);
assert (valid_date("04-12-2003") == true);
assert (valid_date("04122003") == false);
assert (valid_date("20030412") == false);
assert (valid_date("2003-04") == false);
assert (valid_date("2003-04-12") == false);
assert (valid_date("04-2003") == false);
}
|
CPP/125_spans_2
| 2
|
/*
Given a string of words, return a vector of words split on whitespace, if no whitespaces exists in the text you
should split on commas ',' if no commas exists you should return a vector with one element, the number of lower-case letters with odd order in the
alphabet, ord("a") = 0, ord("b") = 1, ... ord("z") = 25
Examples
split_words("Hello world!") β {"Hello", "world!"}
split_words("Hello,world!") β {"Hello", "world!"}
split_words("abcdef") == {"3"}
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
vector<string> split_words(string txt){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<string> split_words(string txt){
|
[
" int i;\n string current=\"\";\n vector<string> out={};\n if (find(txt.begin(),txt.end(),' ')!=txt.end())\n {\n txt=txt+' ';\n for (i=0;i<txt.length();i++)\n if (txt[i]==' ') \n {\n if (current.length()>0)out.push_back(current); \n current=",
" }\n else current=current+txt[i];\n return out;\n }\n if (find(txt.begin(),txt.end(),',')!=txt.end())\n {\n txt=txt+',';\n for (i=0;i<txt.length();i++)\n if (txt[i]==',') \n {\n if (current.length()>0)out.push_back(current); \n current=\"\";\n }\n else current=current+txt[i];\n return out;\n }\n int num=0;\n for (i=0;i<txt.length();i++)\n if (txt[i",
"(num)};\n}\n"
] |
[
"\"\";\n ",
"]>=97 and txt[i]<=122 and txt[i]%2==0)\n num+=1;\n return {to_string"
] |
int i;
string current="";
vector<string> out={};
if (find(txt.begin(),txt.end(),' ')!=txt.end())
{
txt=txt+' ';
for (i=0;i<txt.length();i++)
if (txt[i]==' ')
{
if (current.length()>0)out.push_back(current);
current="";
}
else current=current+txt[i];
return out;
}
if (find(txt.begin(),txt.end(),',')!=txt.end())
{
txt=txt+',';
for (i=0;i<txt.length();i++)
if (txt[i]==',')
{
if (current.length()>0)out.push_back(current);
current="";
}
else current=current+txt[i];
return out;
}
int num=0;
for (i=0;i<txt.length();i++)
if (txt[i]>=97 and txt[i]<=122 and txt[i]%2==0)
num+=1;
return {to_string(num)};
}
|
#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(split_words("Hello world!") , {"Hello","world!"}));
assert (issame(split_words("Hello,world!") , {"Hello","world!"}));
assert (issame(split_words("Hello world,!") , {"Hello","world,!"}));
assert (issame(split_words("Hello,Hello,world !") , {"Hello,Hello,world","!"}));
assert (issame(split_words("abcdef") , {"3"}));
assert (issame(split_words("aaabb") , {"2"}));
assert (issame(split_words("aaaBb") , {"1"}));
assert (issame(split_words("") ,{"0"}));
}
|
CPP/126_spans_2
| 2
|
/*
Given a vector of numbers, return whether or not they are sorted
in ascending order. If vector has more than 1 duplicate of the same
number, return false. Assume no negative numbers and only integers.
Examples
is_sorted({5}) β true
is_sorted({1, 2, 3, 4, 5}) β true
is_sorted({1, 3, 2, 4, 5}) β false
is_sorted({1, 2, 3, 4, 5, 6}) β true
is_sorted({1, 2, 3, 4, 5, 6, 7}) β true
is_sorted({1, 3, 2, 4, 5, 6, 7}) β false
is_sorted({1, 2, 2, 3, 3, 4}) β true
is_sorted({1, 2, 2, 2, 3, 4}) β false
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
bool is_sorted(vector<int> lst){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
bool is_sorted(vector<int> lst){
|
[
" for (int i=1;i<lst.size();i++)\n",
"",
"se;\n }\n return true;\n}\n"
] |
[
" {\n if (lst[i]<lst[i-",
"1]) return false;\n if (i>=2 and lst[i]==lst[i-1] and lst[i]==lst[i-2]) return fal"
] |
for (int i=1;i<lst.size();i++)
{
if (lst[i]<lst[i-1]) return false;
if (i>=2 and lst[i]==lst[i-1] and lst[i]==lst[i-2]) return false;
}
return true;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (is_sorted({5}) == true);
assert (is_sorted({1, 2, 3, 4, 5}) == true);
assert (is_sorted({1, 3, 2, 4, 5}) == false);
assert (is_sorted({1, 2, 3, 4, 5, 6}) == true);
assert (is_sorted({1, 2, 3, 4, 5, 6, 7}) == true);
assert (is_sorted({1, 3, 2, 4, 5, 6, 7}) == false);
assert (is_sorted({}) == true);
assert (is_sorted({1}) == true);
assert (is_sorted({3, 2, 1}) == false);
assert (is_sorted({1, 2, 2, 2, 3, 4}) == false);
assert (is_sorted({1, 2, 3, 3, 3, 4}) == false);
assert (is_sorted({1, 2, 2, 3, 3, 4}) == true);
assert (is_sorted({1, 2, 3, 4}) == true);
}
|
CPP/127_spans_2
| 2
|
/*
You are given two intervals,
where each interval is a pair of integers. For example, interval = (start, end) = (1, 2).
The given intervals are closed which means that the interval (start, end)
includes both start and end.
For each given interval, it is assumed that its start is less or equal its end.
Your task is to determine whether the length of intersection of these two
intervals is a prime number.
Example, the intersection of the intervals (1, 3), (2, 4) is (2, 3)
which its length is 1, which not a prime number.
If the length of the intersection is a prime number, return "YES",
otherwise, return "NO".
If the two intervals don't intersect, return "NO".
{input/output} samples:
intersection({1, 2}, {2, 3}) ==> "NO"
intersection({-1, 1}, {0, 4}) ==> "NO"
intersection({-3, -1}, {-5, 5}) ==> "YES"
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
string intersection( vector<int> interval1,vector<int> interval2){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
string intersection( vector<int> interval1,vector<int> interval2){
|
[
" int inter1,inter",
"terval1[0],interval2[0]);\n inter2=min(interval1[1],interval2[1]);\n l=inter2-inter1;\n if (l<2) return \"NO\";\n for (i=2;i*i<=l;i",
";\n}\n"
] |
[
"2,l,i;\n inter1=max(in",
"++)\n if (l%i==0) return \"NO\";\n return \"YES\""
] |
int inter1,inter2,l,i;
inter1=max(interval1[0],interval2[0]);
inter2=min(interval1[1],interval2[1]);
l=inter2-inter1;
if (l<2) return "NO";
for (i=2;i*i<=l;i++)
if (l%i==0) return "NO";
return "YES";
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (intersection({1, 2}, {2, 3}) == "NO");
assert (intersection({-1, 1}, {0, 4}) == "NO");
assert (intersection({-3, -1}, {-5, 5}) == "YES");
assert (intersection({-2, 2}, {-4, 0}) == "YES");
assert (intersection({-11, 2}, {-1, -1}) == "NO");
assert (intersection({1, 2}, {3, 5}) == "NO");
assert (intersection({1, 2}, {1, 2}) == "NO");
assert (intersection({-2, -2}, {-3, -2}) == "NO");
}
|
CPP/128_spans_2
| 2
|
/*
You are given a vector arr of integers and you need to return
sum of magnitudes of integers multiplied by product of all signs
of each number in the vector, represented by 1, -1 or 0.
Note: return -32768 for empty arr.
Example:
>>> prod_signs({1, 2, 2, -4}) == -9
>>> prod_signs({0, 1}) == 0
>>> prod_signs({}) == -32768
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
int prod_signs(vector<int> arr){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int prod_signs(vector<int> arr){
|
[
" if (arr.size()==0) return -32768;\n int i,sum=0,prods=1;\n for (i=0",
"]);\n if (arr[i]==0) prods=0;\n if (arr",
"\n}\n"
] |
[
";i<arr.size();i++)\n {\n sum+=abs(arr[i",
"[i]<0) prods=-prods;\n }\n return sum*prods;"
] |
if (arr.size()==0) return -32768;
int i,sum=0,prods=1;
for (i=0;i<arr.size();i++)
{
sum+=abs(arr[i]);
if (arr[i]==0) prods=0;
if (arr[i]<0) prods=-prods;
}
return sum*prods;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (prod_signs({1, 2, 2, -4}) == -9);
assert (prod_signs({0, 1}) == 0);
assert (prod_signs({1, 1, 1, 2, 3, -1, 1}) == -10);
assert (prod_signs({}) == -32768);
assert (prod_signs({2, 4,1, 2, -1, -1, 9}) == 20);
assert (prod_signs({-1, 1, -1, 1}) == 4);
assert (prod_signs({-1, 1, 1, 1}) == -4);
assert (prod_signs({-1, 1, 1, 0}) == 0);
}
|
CPP/129_spans_2
| 2
|
/*
Given a grid with N rows and N columns (N >= 2) and a positive integer k,
each cell of the grid contains a value. Every integer in the range {1, N * N}
inclusive appears exactly once on the cells of the grid.
You have to find the minimum path of length k in the grid. You can start
from any cell, and in each step you can move to any of the neighbor cells,
in other words, you can go to cells which share an edge with you current
cell.
Please note that a path of length k means visiting exactly k cells (not
necessarily distinct).
You CANNOT go off the grid.
A path A (of length k) is considered less than a path B (of length k) if
after making the ordered vectors of the values on the cells that A and B go
through (let's call them lst_A and lst_B), lst_A is lexicographically less
than lst_B, in other words, there exist an integer index i (1 <= i <= k)
such that lst_A[i] < lst_B[i] and for any j (1 <= j < i) we have
lst_A[j] = lst_B[j].
It is guaranteed that the answer is unique.
Return an ordered vector of the values on the cells that the minimum path go through.
Examples:
Input: grid = { {1,2,3}, {4,5,6}, {7,8,9}}, k = 3
Output: {1, 2, 1}
Input: grid = { {5,9,3}, {4,1,6}, {7,8,2}}, k = 1
Output: {1}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> minPath(vector<vector<int>> grid, int k){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> minPath(vector<vector<int>> grid, int k){
|
[
" int i,j,x,y,min;\n for (i=0;i<grid.size();i++)\n for (j=0;j<grid[i].size();j++)\n if (grid[i][j]==1) {\n x=i;y=j;\n }\n m",
") min=grid[x-1][y];\n if (x<grid.size()-1 and grid[x+1][y]<min) min=grid[x+1][y];\n if (y>0 and grid[x][y-1]<min) min=grid[x][y-1];\n if (y<grid.size()-1 and grid[x][y+1]<min) min=grid[x][y+1];\n vecto",
"ush_back(min);\n return out;\n}\n"
] |
[
"in=grid.size()*grid.size();\n if (x>0 and grid[x-1][y]<min",
"r<int> out={};\n for (i=0;i<k;i++)\n if (i%2==0) out.push_back(1);\n else out.p"
] |
int i,j,x,y,min;
for (i=0;i<grid.size();i++)
for (j=0;j<grid[i].size();j++)
if (grid[i][j]==1) {
x=i;y=j;
}
min=grid.size()*grid.size();
if (x>0 and grid[x-1][y]<min) min=grid[x-1][y];
if (x<grid.size()-1 and grid[x+1][y]<min) min=grid[x+1][y];
if (y>0 and grid[x][y-1]<min) min=grid[x][y-1];
if (y<grid.size()-1 and grid[x][y+1]<min) min=grid[x][y+1];
vector<int> out={};
for (i=0;i<k;i++)
if (i%2==0) out.push_back(1);
else out.push_back(min);
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(minPath({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, 3) , {1, 2, 1}));
assert (issame(minPath({{5, 9, 3}, {4, 1, 6}, {7, 8, 2}}, 1) , {1}));
assert (issame(minPath({{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}, 4) , {1, 2, 1, 2}));
assert (issame(minPath({{6, 4, 13, 10}, {5, 7, 12, 1}, {3, 16, 11, 15}, {8, 14, 9, 2}}, 7) , {1, 10, 1, 10, 1, 10, 1}));
assert (issame(minPath({{8, 14, 9, 2}, {6, 4, 13, 15}, {5, 7, 1, 12}, {3, 10, 11, 16}}, 5) , {1, 7, 1, 7, 1}));
assert (issame(minPath({{11, 8, 7, 2}, {5, 16, 14, 4}, {9, 3, 15, 6}, {12, 13, 10, 1}}, 9) , {1, 6, 1, 6, 1, 6, 1, 6, 1}));
assert (issame(minPath({{12, 13, 10, 1}, {9, 3, 15, 6}, {5, 16, 14, 4}, {11, 8, 7, 2}}, 12) , {1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6}));
assert (issame(minPath({{2, 7, 4}, {3, 1, 5}, {6, 8, 9}}, 8) , {1, 3, 1, 3, 1, 3, 1, 3}));
assert (issame(minPath({{6, 1, 5}, {3, 8, 9}, {2, 7, 4}}, 8) , {1, 5, 1, 5, 1, 5, 1, 5}));
assert (issame(minPath({{1, 2}, {3, 4}}, 10) , {1, 2, 1, 2, 1, 2, 1, 2, 1, 2}));
assert (issame(minPath({{1, 3}, {3, 2}}, 10) , {1, 3, 1, 3, 1, 3, 1, 3, 1, 3}));
}
|
CPP/130_spans_2
| 2
|
/*
Everyone knows Fibonacci sequence, it was studied deeply by mathematicians in
the last couple centuries. However, what people don't know is Tribonacci sequence.
Tribonacci sequence is defined by the recurrence:
tri(1) = 3
tri(n) = 1 + n / 2, if n is even.
tri(n) = tri(n - 1) + tri(n - 2) + tri(n + 1), if n is odd.
For example:
tri(2) = 1 + (2 / 2) = 2
tri(4) = 3
tri(3) = tri(2) + tri(1) + tri(4)
= 2 + 3 + 3 = 8
You are given a non-negative integer number n, you have to a return a vector of the
first n + 1 numbers of the Tribonacci sequence.
Examples:
tri(3) = {1, 3, 2, 8}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> tri(int n){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> tri(int n){
|
[
" vector<int> out={1,3};\n if",
" {1};\n ",
"out.push_back(out[i-1]+out[i-2]+1+(i+1)/2);\n }\n return out;\n}\n"
] |
[
" (n==0) return",
" for (int i=2;i<=n;i++)\n {\n if (i%2==0) out.push_back(1+i/2);\n else "
] |
vector<int> out={1,3};
if (n==0) return {1};
for (int i=2;i<=n;i++)
{
if (i%2==0) out.push_back(1+i/2);
else out.push_back(out[i-1]+out[i-2]+1+(i+1)/2);
}
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(tri(3) , {1, 3, 2, 8}));
assert (issame(tri(4) , {1, 3, 2, 8, 3}));
assert (issame(tri(5) , {1, 3, 2, 8, 3, 15}));
assert (issame(tri(6) , {1, 3, 2, 8, 3, 15, 4}));
assert (issame(tri(7) , {1, 3, 2, 8, 3, 15, 4, 24}));
assert (issame(tri(8) , {1, 3, 2, 8, 3, 15, 4, 24, 5}));
assert (issame(tri(9) , {1, 3, 2, 8, 3, 15, 4, 24, 5, 35}));
assert (issame(tri(20) , {1, 3, 2, 8, 3, 15, 4, 24, 5, 35, 6, 48, 7, 63, 8, 80, 9, 99, 10, 120, 11}));
assert (issame(tri(0) , {1}));
assert (issame(tri(1) , {1, 3}));
}
|
CPP/131_spans_2
| 2
|
/*
Given a positive integer n, return the product of the odd digits.
Return 0 if all digits are even.
For example:
digits(1) == 1
digits(4) == 0
digits(235) == 15
*/
#include<stdio.h>
#include<string>
using namespace std;
int digits(int n){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int digits(int n){
|
[
" int prod=1,has=0;\n string s=to_string(n);\n for (int i=0;i<s.length();i++)\n if (s[i]%2==1) \n {\n ",
" ",
"0) return 0;\n return prod;\n}\n"
] |
[
" has=1;\n ",
"prod=prod*(s[i]-48);\n }\n if (has=="
] |
int prod=1,has=0;
string s=to_string(n);
for (int i=0;i<s.length();i++)
if (s[i]%2==1)
{
has=1;
prod=prod*(s[i]-48);
}
if (has==0) return 0;
return prod;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (digits(5) == 5);
assert (digits(54) == 5);
assert (digits(120) ==1);
assert (digits(5014) == 5);
assert (digits(98765) == 315);
assert (digits(5576543) == 2625);
assert (digits(2468) == 0);
}
|
CPP/132_spans_2
| 2
|
/*
Create a function that takes a string as input which contains only square brackets.
The function should return true if and only if there is a valid subsequence of brackets
where at least one bracket in the subsequence is nested.
is_nested("[[]]") β true
is_nested("[]]]]]]][[[[[]") β false
is_nested("[][]") β false
is_nested("[]") β false
is_nested("[[][]]") β true
is_nested("[[]][[") β true
*/
#include<stdio.h>
#include<string>
using namespace std;
bool is_nested(string str){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
bool is_nested(string str){
|
[
" int count=0,maxcount=0;\n for (",
" ",
"count;\n if (count<=maxcount-2) return true;\n }\n return false;\n}\n"
] |
[
"int i=0;i<str.length();i++)\n {\n if (str[i]=='[') count+=1;\n ",
" if (str[i]==']') count-=1;\n if (count<0) count=0;\n if (count>maxcount) maxcount="
] |
int count=0,maxcount=0;
for (int i=0;i<str.length();i++)
{
if (str[i]=='[') count+=1;
if (str[i]==']') count-=1;
if (count<0) count=0;
if (count>maxcount) maxcount=count;
if (count<=maxcount-2) return true;
}
return false;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (is_nested("[[]]") == true);
assert (is_nested("[]]]]]]][[[[[]") == false);
assert (is_nested("[][]") == false);
assert (is_nested(("[]")) == false);
assert (is_nested("[[[[]]]]") == true);
assert (is_nested("[]]]]]]]]]]") == false);
assert (is_nested("[][][[]]") == true);
assert (is_nested("[[]") == false);
assert (is_nested("[]]") == false);
assert (is_nested("[[]][[") == true);
assert (is_nested("[[][]]") == true);
assert (is_nested("") == false);
assert (is_nested("[[[[[[[[") == false);
assert (is_nested("]]]]]]]]") == false);
}
|
CPP/133_spans_2
| 2
|
/*
You are given a vector of numbers.
You need to return the sum of squared numbers in the given vector,
round each element in the vector to the upper int(Ceiling) first.
Examples:
For lst = {1,2,3} the output should be 14
For lst = {1,4,9} the output should be 98
For lst = {1,3,5,7} the output should be 84
For lst = {1.4,4.2,0} the output should be 29
For lst = {-2.4,1,1} the output should be 6
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
int sum_squares(vector<float> lst){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int sum_squares(vector<float> lst){
|
[
" i",
"um+=ceil(lst[i])*ceil(lst[",
""
] |
[
"nt sum=0;\n for (int i=0;i<lst.size();i++)\n s",
"i]);\n return sum;\n}\n"
] |
int sum=0;
for (int i=0;i<lst.size();i++)
sum+=ceil(lst[i])*ceil(lst[i]);
return sum;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (sum_squares({1,2,3})==14);
assert (sum_squares({1.0,2,3})==14);
assert (sum_squares({1,3,5,7})==84);
assert (sum_squares({1.4,4.2,0})==29);
assert (sum_squares({-2.4,1,1})==6);
assert (sum_squares({100,1,15,2})==10230);
assert (sum_squares({10000,10000})==200000000);
assert (sum_squares({-1.4,4.6,6.3})==75);
assert (sum_squares({-1.4,17.9,18.9,19.9})==1086);
assert (sum_squares({0})==0);
assert (sum_squares({-1})==1);
assert (sum_squares({-1,1,0})==2);
}
|
CPP/134_spans_2
| 2
|
/*
Create a function that returns true if the last character
of a given string is an alphabetical character and is not
a part of a word, and false otherwise.
Note: "word" is a group of characters separated by space.
Examples:
check_if_last_char_is_a_letter("apple pie") β false
check_if_last_char_is_a_letter("apple pi e") β true
check_if_last_char_is_a_letter("apple pi e ") β false
check_if_last_char_is_a_letter("") β false
*/
#include<stdio.h>
#include<string>
using namespace std;
bool check_if_last_char_is_a_letter(string txt){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
bool check_if_last_char_is_a_letter(string txt){
|
[
" ",
")==0) return false;\n char chr=txt[tx",
"hr>90 and chr<97) or chr>122) return false;\n if (txt.length()==1) return true;\n chr=txt[txt.length()-2];\n if ((chr>=65 and chr<=90) or (chr>=97 and chr<=122)) return false;\n return true;\n}\n"
] |
[
" if (txt.length(",
"t.length()-1];\n if (chr<65 or (c"
] |
if (txt.length()==0) return false;
char chr=txt[txt.length()-1];
if (chr<65 or (chr>90 and chr<97) or chr>122) return false;
if (txt.length()==1) return true;
chr=txt[txt.length()-2];
if ((chr>=65 and chr<=90) or (chr>=97 and chr<=122)) return false;
return true;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (check_if_last_char_is_a_letter("apple") == false);
assert (check_if_last_char_is_a_letter("apple pi e") == true);
assert (check_if_last_char_is_a_letter("eeeee") == false);
assert (check_if_last_char_is_a_letter("A") == true);
assert (check_if_last_char_is_a_letter("Pumpkin pie ") == false);
assert (check_if_last_char_is_a_letter("Pumpkin pie 1") == false);
assert (check_if_last_char_is_a_letter("") == false);
assert (check_if_last_char_is_a_letter("eeeee e ") == false);
assert (check_if_last_char_is_a_letter("apple pie") == false);
assert (check_if_last_char_is_a_letter("apple pi e ") == false);
}
|
CPP/135_spans_2
| 2
|
/*
Create a function which returns the largest index of an element which
is not greater than or equal to the element immediately preceding it. If
no such element exists then return -1. The given vector will not contain
duplicate values.
Examples:
can_arrange({1,2,4,3,5}) = 3
can_arrange({1,2,3}) = -1
*/
#include<stdio.h>
#include<vector>
using namespace std;
int can_arrange(vector<int> arr){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int can_arrange(vector<int> arr){
|
[
" int m",
"for (int i=",
"rr[i]<=i) max=i;\n return max;\n}\n"
] |
[
"ax=-1;\n ",
"0;i<arr.size();i++)\n if (a"
] |
int max=-1;
for (int i=0;i<arr.size();i++)
if (arr[i]<=i) max=i;
return max;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (can_arrange({1,2,4,3,5})==3);
assert (can_arrange({1,2,4,5})==-1);
assert (can_arrange({1,4,2,5,6,7,8,9,10})==2);
assert (can_arrange({4,8,5,7,3})==4);
assert (can_arrange({})==-1);
}
|
CPP/136_spans_2
| 2
|
/*
Create a function that returns a vector (a, b), where "a" is
the largest of negative integers, and "b" is the smallest
of positive integers in a vector.
If there is no negative or positive integers, return them as 0.
Examples:
largest_smallest_integers({2, 4, 1, 3, 5, 7}) == {0, 1}
largest_smallest_integers({}) == {0,0}
largest_smallest_integers({0}) == {0,0}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> largest_smallest_integers(vector<int> lst){
|
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> largest_smallest_integers(vector<int> lst){
|
[
" int maxneg=0,minpos=0;\n",
"e();i++)\n {\n if (lst[i]<0 and (maxneg==0 or lst[i]>maxneg)) maxneg=lst[i];\n if (lst[i]>0 and (minpos==0 or lst[",
" {maxneg,minpos};\n}\n"
] |
[
" for (int i=0;i<lst.siz",
"i]<minpos)) minpos=lst[i];\n }\n return"
] |
int maxneg=0,minpos=0;
for (int i=0;i<lst.size();i++)
{
if (lst[i]<0 and (maxneg==0 or lst[i]>maxneg)) maxneg=lst[i];
if (lst[i]>0 and (minpos==0 or lst[i]<minpos)) minpos=lst[i];
}
return {maxneg,minpos};
}
|
#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(largest_smallest_integers({2, 4, 1, 3, 5, 7}) , {0, 1}));
assert (issame(largest_smallest_integers({2, 4, 1, 3, 5, 7, 0}) , {0, 1}));
assert (issame(largest_smallest_integers({1, 3, 2, 4, 5, 6, -2}) , {-2, 1}));
assert (issame(largest_smallest_integers({4, 5, 3, 6, 2, 7, -7}) , {-7, 2}));
assert (issame(largest_smallest_integers({7, 3, 8, 4, 9, 2, 5, -9}) , {-9, 2}));
assert (issame(largest_smallest_integers({}) , {0, 0}));
assert (issame(largest_smallest_integers({0}) , {0, 0}));
assert (issame(largest_smallest_integers({-1, -3, -5, -6}) , {-1, 0}));
assert (issame(largest_smallest_integers({-1, -3, -5, -6, 0}) , {-1, 0}));
assert (issame(largest_smallest_integers({-6, -4, -4, -3, 1}) , {-3, 1}));
assert (issame(largest_smallest_integers({-6, -4, -4, -3, -100, 1}) , {-3, 1}));
}
|
CPP/137_spans_2
| 2
|
/*
Create a function that takes integers, floats, or strings representing
real numbers, and returns the larger variable in its given variable type.
Return "None" if the values are equal.
Note: If a real number is represented as a string, the floating point might be . or ,
compare_one(1, 2.5) β 2.5
compare_one(1, "2,3") β "2,3"
compare_one("5,1", "6") β "6"
compare_one("1", 1) β "None"
*/
#include<stdio.h>
#include<string>
#include<algorithm>
#include<boost/any.hpp>
using namespace std;
boost::any compare_one(boost::any a,boost::any b){
|
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
#include<boost/any.hpp>
using namespace std;
#include<stdlib.h>
boost::any compare_one(boost::any a,boost::any b){
|
[
" double numa,numb;\n boost::any out;\n \n if (a.type()==typeid(string))\n {\n string s;\n s=boost::any_cast<string>(a);\n if (find(s.begin(),s.end(),',')!=s.end())\n for (int i=0;i<s.length();i++)\n if (s[i]==',') s=s.substr(0,i)+'.'+s.su",
" }\n else \n {\n if (a.type()==typeid(int)) numa=boost::any_cast<int>(a);\n if (a.type()==typeid(double)) numa=boost::any_cast<double>(a);\n }\n if (b.type()==typeid(string))\n {\n string s;\n s=boost::any_cast<string>(b);\n if (find(s.begin(),s.end(),',')!=s.end())\n for (int i=0;i<s.length();i++)\n if (s[i]==',') s=s.substr(0,i)+'.'+s.substr(i+1);\n numb=atof(s.c_str());\n }\n else \n {\n if (b.type()==typeid(int)) numb=boost::any_cast<int>(b);\n if (b.type()==typeid(double)) numb=boost::an",
" if (numa==numb) return string(\"None\");\n if (numa<numb) return b;\n if (numa>numb) return a;\n}\n"
] |
[
"bstr(i+1);\n numa=atof(s.c_str());\n \n",
"y_cast<double>(b);\n }\n\n"
] |
double numa,numb;
boost::any out;
if (a.type()==typeid(string))
{
string s;
s=boost::any_cast<string>(a);
if (find(s.begin(),s.end(),',')!=s.end())
for (int i=0;i<s.length();i++)
if (s[i]==',') s=s.substr(0,i)+'.'+s.substr(i+1);
numa=atof(s.c_str());
}
else
{
if (a.type()==typeid(int)) numa=boost::any_cast<int>(a);
if (a.type()==typeid(double)) numa=boost::any_cast<double>(a);
}
if (b.type()==typeid(string))
{
string s;
s=boost::any_cast<string>(b);
if (find(s.begin(),s.end(),',')!=s.end())
for (int i=0;i<s.length();i++)
if (s[i]==',') s=s.substr(0,i)+'.'+s.substr(i+1);
numb=atof(s.c_str());
}
else
{
if (b.type()==typeid(int)) numb=boost::any_cast<int>(b);
if (b.type()==typeid(double)) numb=boost::any_cast<double>(b);
}
if (numa==numb) return string("None");
if (numa<numb) return b;
if (numa>numb) return a;
}
|
#undef NDEBUG
#include<assert.h>
int main(){
assert (boost::any_cast<int>(compare_one(1, 2)) == 2);
assert (boost::any_cast<double>(compare_one(1, 2.5))== 2.5);
assert (boost::any_cast<int>(compare_one(2, 3)) == 3);
assert (boost::any_cast<int>(compare_one(5, 6)) == 6);
assert (boost::any_cast<string>(compare_one(1, string("2,3")))== "2,3");
assert (boost::any_cast<string>(compare_one(string("5,1"), string("6"))) == "6");
assert (boost::any_cast<string>(compare_one(string("1"), string("2"))) == "2");
assert (boost::any_cast<string>(compare_one(string("1"), 1)) == "None");
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.