task_id stringlengths 5 7 | prompt stringlengths 133 1.35k | canonical_solution stringlengths 18 1.4k | test stringlengths 148 1.76k | declaration stringlengths 111 254 | example_test stringlengths 0 679 | full_code stringlengths 135 1.59k |
|---|---|---|---|---|---|---|
CPP/0 | /*
Check if in given vector of numbers, are any two numbers closer to each other than
given threshold.
>>> has_close_elements({1.0, 2.0, 3.0}, 0.5)
false
>>> has_close_elements({1.0, 2.8, 3.0, 4.0, 5.0, 2.0}, 0.3)
true
*/
#include<stdio.h>
#include<vector>
#include<math.h>
using namespace std;
bool has_close_elements(v... | int i,j;
for (i=0;i<numbers.size();i++)
for (j=i+1;j<numbers.size();j++)
if (abs(numbers[i]-numbers[j])<threshold)
return true;
return false;
}
| #undef NDEBUG
#include<assert.h>
int main(){
vector<float> a={1.0, 2.0, 3.9, 4.0, 5.0, 2.2};
assert (has_close_elements(a, 0.3)==true);
assert (has_close_elements(a, 0.05) == false);
assert (has_close_elements({1.0, 2.0, 5.9, 4.0, 5.0}, 0.95) == true);
assert (has_close_elements({1.0, 2.0, 5.9, 4.0... | #include<stdio.h>
#include<vector>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool has_close_elements(vector<float> numbers, float threshold){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (has_close_elements({1.0, 2.0, 3.0}, 0.5) == false && "failure 1");
assert (has_close_elements({1.0, 2.8, 3.0, 4.0, 5.0, 2.0}, 0.3) && "failure 2") ;
}
| #include<stdio.h>
#include<vector>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool has_close_elements(vector<float> numbers, float threshold){
int i,j;
for (i=0;i<numbers.size();i++)
for (j=i+1;j<numbers.size();j++)
if (abs(numbers[i]-numbers[j])<threshold)
ret... |
CPP/1 | /*
Input to this function is a string containing multiple groups of nested parentheses. Your goal is to
separate those group into separate strings and return the vector of those.
Separate groups are balanced (each open brace is properly closed) and not nested within each other
Ignore any spaces in the input string.
>>>... | vector<string> all_parens;
string current_paren;
int level=0;
char chr;
int i;
for (i=0;i<paren_string.length();i++)
{
chr=paren_string[i];
if (chr=='(')
{
level+=1;
current_paren+=chr;
}
if (chr==')')
{
level-=1;
... | #undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(separate_paren_groups("(()()) ((())) () ((())()())"),{"(()())", "((... | #include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<string> separate_paren_groups(string paren_string){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(separate_paren_groups("( ) (( )) (( )( ))") ,{"()", "(())", "(()())... | #include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<string> separate_paren_groups(string paren_string){
vector<string> all_parens;
string current_paren;
int level=0;
char chr;
int i;
for (i=0;i<paren_string.lengt... |
CPP/2 | /*
Given a positive floating point number, it can be decomposed into
and integer part (largest integer smaller than given number) and decimals
(leftover part always smaller than 1).
Return the decimal part of the number.
>>> truncate_number(3.5)
0.5
*/
#include<stdio.h>
#include<math.h>
using namespace std;
float trun... | return number-int(number);
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (truncate_number(3.5) == 0.5);
assert (abs(truncate_number(1.33) - 0.33) < 1e-4);
assert (abs(truncate_number(123.456) - 0.456) < 1e-4);
} | #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
float truncate_number(float number){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (truncate_number(3.5) == 0.5);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
float truncate_number(float number){
return number-int(number);
}
|
CPP/3 | /*
You"re given a vector of deposit and withdrawal operations on a bank account that starts with
zero balance. Your task is to detect if at any point the balance of account falls below zero, and
at that point function should return true. Otherwise it should return false.
>>> below_zero({1, 2, 3})
false
>>> below_zero({... | int num=0;
for (int i=0;i<operations.size();i++)
{
num+=operations[i];
if (num<0) return true;
}
return false;
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (below_zero({}) == false);
assert (below_zero({1, 2, -3, 1, 2, -3}) == false);
assert (below_zero({1, 2, -4, 5, 6}) == true);
assert (below_zero({1, -1, 2, -2, 5, -5, 4, -4}) == false);
assert (below_zero({1, -1, 2, -2, 5, -5, 4, -5}) == true);
... | #include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
bool below_zero(vector<int> operations){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (below_zero({1, 2, 3}) == false);
assert (below_zero({1, 2, -4, 5}) == true);
}
| #include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
bool below_zero(vector<int> operations){
int num=0;
for (int i=0;i<operations.size();i++)
{
num+=operations[i];
if (num<0) return true;
}
return false;
}
|
CPP/4 | /*
For a given vector of input numbers, calculate Mean Absolute Deviation
around the mean of this dataset.
Mean Absolute Deviation is the average absolute difference between each
element and a centerpoint (mean in this case):
MAD = average | x - x_mean |
>>> mean_absolute_deviation({1.0, 2.0, 3.0, 4.0})
1.0
*/
#include... | float sum=0;
float avg,msum,mavg;
int i=0;
for (i=0;i<numbers.size();i++)
sum+=numbers[i];
avg=sum/numbers.size();
msum=0;
for (i=0;i<numbers.size();i++)
msum+=abs(numbers[i]-avg);
return msum/numbers.size();
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (abs(mean_absolute_deviation({1.0, 2.0, 3.0}) - 2.0/3.0) < 1e-4);
assert (abs(mean_absolute_deviation({1.0, 2.0, 3.0, 4.0}) - 1.0) < 1e-4);
assert (abs(mean_absolute_deviation({1.0, 2.0, 3.0, 4.0, 5.0}) - 6.0/5.0) < 1e-4);
} | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
float mean_absolute_deviation(vector<float> numbers){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (abs(mean_absolute_deviation({1.0, 2.0, 3.0, 4.0}) - 1.0) < 1e-4);
}
| #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
float mean_absolute_deviation(vector<float> numbers){
float sum=0;
float avg,msum,mavg;
int i=0;
for (i=0;i<numbers.size();i++)
sum+=numbers[i];
avg=sum/numbers.size();
msum=0;... |
CPP/5 | /*
Insert a number "delimeter" between every two consecutive elements of input vector `numbers"
>>> intersperse({}, 4)
{}
>>> intersperse({1, 2, 3}, 4)
{1, 4, 2, 4, 3}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> intersperse(vector<int> numbers, int delimeter){
| vector<int> out={};
if (numbers.size()>0) out.push_back(numbers[0]);
for (int i=1;i<numbers.size();i++)
{
out.push_back(delimeter);
out.push_back(numbers[i]);
}
return out;
}
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(intersperse({}, 7), {}));
assert (issame(intersperse({5, 6, 3,... | #include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<int> intersperse(vector<int> numbers, int delimeter){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(intersperse({}, 4), {}));
assert (issame(intersperse({1, 2, 3}, 4),... | #include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<int> intersperse(vector<int> numbers, int delimeter){
vector<int> out={};
if (numbers.size()>0) out.push_back(numbers[0]);
for (int i=1;i<numbers.size();i++)
{
out.push_back(de... |
CPP/6 | /*
Input to this function is a string represented multiple groups for nested parentheses separated by spaces.
For each of the group, output the deepest level of nesting of parentheses.
E.g. (()()) has maximum two levels of nesting while ((())) has three.
>>> parse_nested_parens("(()()) ((())) () ((())()())")
{2, 3, 1,... | vector<int> all_levels;
string current_paren;
int level=0,max_level=0;
char chr;
int i;
for (i=0;i<paren_string.length();i++)
{
chr=paren_string[i];
if (chr=='(')
{
level+=1;
if (level>max_level) max_level=level;
current_paren+=chr;
}
... | #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(parse_nested_parens("(()()) ((())) () ((())()())"),{2, 3, 1, 3}));
... | #include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<int> parse_nested_parens(string paren_string){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(parse_nested_parens("(()()) ((())) () ((())()())"),{2, 3, 1, 3}));
}
| #include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<int> parse_nested_parens(string paren_string){
vector<int> all_levels;
string current_paren;
int level=0,max_level=0;
char chr;
int i;
for (i=0;i<paren_string.l... |
CPP/7 | /*
Filter an input vector of strings only for ones that contain given substring
>>> filter_by_substring({}, "a")
{}
>>> filter_by_substring({"abc", "bacd", "cde", "vector"}, "a")
{"abc", "bacd", "vector"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> filter_by_substring(vect... | vector<string> out;
for (int i=0;i<strings.size();i++)
{
if (strings[i].find(substring)!=strings[i].npos)
out.push_back(strings[i]);
}
return out;
}
| #undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(filter_by_substring({}, "john"),{}));
assert (issame(filter_by_s... | #include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<string> filter_by_substring(vector<string> strings, string substring){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(filter_by_substring({}, "a"),{}));
assert (issame(filter_by_subs... | #include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<string> filter_by_substring(vector<string> strings, string substring){
vector<string> out;
for (int i=0;i<strings.size();i++)
{
if (strings[i].find(substring)!=stri... |
CPP/8 | /*
For a given vector of integers, return a vector consisting of a sum and a product of all the integers in a vector.
Empty sum should be equal to 0 and empty product should be equal to 1.
>>> sum_product({})
(0, 1)
>>> sum_product({1, 2, 3, 4})
(10, 24)
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector... | int sum=0,product=1;
for (int i=0;i<numbers.size();i++)
{
sum+=numbers[i];
product*=numbers[i];
}
return {sum,product};
}
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(sum_product({}) ,{0, 1}));
assert (issame(sum_product({1, 1, 1}), {... | #include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<int> sum_product(vector<int> numbers){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(sum_product({}) ,{0, 1}));
assert (issame(sum_product({1, 2, 3,4}),... | #include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<int> sum_product(vector<int> numbers){
int sum=0,product=1;
for (int i=0;i<numbers.size();i++)
{
sum+=numbers[i];
product*=numbers[i];
}
return {sum,product};
}
|
CPP/9 | /*
From a given vector of integers, generate a vector of rolling maximum element found until given moment
in the sequence.
>>> rolling_max({1, 2, 3, 2, 3, 4, 2})
{1, 2, 3, 3, 3, 4, 4}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> rolling_max(vector<int> numbers){
| vector<int> out;
int max=0;
for (int i=0;i<numbers.size();i++)
{
if (numbers[i]>max) max=numbers[i];
out.push_back(max);
}
return out;
}
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(rolling_max({}),{}));
assert (issame(rolling_max({1, 2, 3, 4}) , {... | #include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<int> rolling_max(vector<int> numbers){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(rolling_max({1, 2, 3, 2, 3, 4, 2}),{1, 2, 3, 3, 3, 4, 4}));
}
| #include<stdio.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<int> rolling_max(vector<int> numbers){
vector<int> out;
int max=0;
for (int i=0;i<numbers.size();i++)
{
if (numbers[i]>max) max=numbers[i];
out.push_back(max);
}
... |
CPP/11 | /*
Input are two strings a and b consisting only of 1s and 0s.
Perform binary XOR on these inputs and return result also as a string.
>>> string_xor("010", "110")
"100"
*/
#include<stdio.h>
#include<string>
using namespace std;
string string_xor(string a,string b){
| string output="";
for (int i=0;(i<a.length() and i<b.length());i++)
{
if (i<a.length() and i<b.length())
{
if (a[i]== b[i])
{
output+='0';
}
else output+='1';
}
else
{
if (i>=a.length())
... | #undef NDEBUG
#include<assert.h>
int main(){
assert (string_xor("111000", "101010") == "010010");
assert (string_xor("1", "1") == "0");
assert (string_xor("0101", "0000") == "0101");
}
| #include<stdio.h>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
string string_xor(string a,string b){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (string_xor("010", "110") == "100");
}
| #include<stdio.h>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
string string_xor(string a,string b){
string output="";
for (int i=0;(i<a.length() and i<b.length());i++)
{
if (i<a.length() and i<b.length())
{
if (a[i]== b[i])
... |
CPP/12 | /*
Out of vector of strings, return the longest one. Return the first one in case of multiple
strings of the same length. Return None in case the input vector is empty.
>>> longest({})
>>> longest({"a", "b", "c"})
"a"
>>> longest({"a", "bb", "ccc"})
"ccc"
*/
#include<stdio.h>
#include<vector>
#include<string>
using na... | string out;
for (int i=0;i<strings.size();i++)
{
if (strings[i].length()>out.length()) out=strings[i];
}
return out;
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (longest({}) == "");
assert (longest({"x", "y", "z"}) == "x");
assert (longest({"x", "yyy", "zzzz", "www", "kkkk", "abc"}) == "zzzz");
}
| #include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
string longest(vector<string> strings){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (longest({}) == "");
assert (longest({"a", "b", "c"}) == "a");
assert (longest({"a", "bb", "ccc"}) == "ccc");
}
| #include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
string longest(vector<string> strings){
string out;
for (int i=0;i<strings.size();i++)
{
if (strings[i].length()>out.length()) out=strings[i];
}
return out;
}
|
CPP/13 | /*
Return a greatest common divisor of two integers a and b
>>> greatest_common_divisor(3, 5)
1
>>> greatest_common_divisor(25, 15)
5
*/
#include<stdio.h>
using namespace std;
int greatest_common_divisor(int a, int b){
| int out,m;
while (true){
if (a<b)
{
m=a;a=b;b=m;
}
a=a%b;
if (a==0) return b;
}
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (greatest_common_divisor(3, 7) == 1);
assert (greatest_common_divisor(10, 15) == 5);
assert (greatest_common_divisor(49, 14) == 7);
assert (greatest_common_divisor(144, 60) == 12);
}
| #include<stdio.h>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
int greatest_common_divisor(int a, int b){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (greatest_common_divisor(3, 5) == 1);
assert (greatest_common_divisor(25, 15) == 5);
}
| #include<stdio.h>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
int greatest_common_divisor(int a, int b){
int out,m;
while (true){
if (a<b)
{
m=a;a=b;b=m;
}
a=a%b;
if (a==0) return b;
}
}
|
CPP/14 | /*
Return vector of all prefixes from shortest to longest of the input string
>>> all_prefixes("abc")
{"a", "ab", "abc"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> all_prefixes(string str){
| vector<string> out;
string current="";
for (int i=0;i<str.length();i++)
{
current=current+str[i];
out.push_back(current);
}
return out;
}
| #undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(all_prefixes(""),{}));
assert (issame(all_prefixes("asdfgh") ,{"a... | #include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<string> all_prefixes(string str){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(all_prefixes("abc"),{"a","ab","abc"}));
}
| #include<stdio.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<math.h>
#include<stdlib.h>
vector<string> all_prefixes(string str){
vector<string> out;
string current="";
for (int i=0;i<str.length();i++)
{
current=current+str[i];
out.push_back(curren... |
CPP/15 | /*
Return a string containing space-delimited numbers starting from 0 upto n inclusive.
>>> string_sequence(0)
"0"
>>> string_sequence(5)
"0 1 2 3 4 5"
*/
#include<stdio.h>
#include<string>
using namespace std;
string string_sequence(int n){
| string out="0";
for (int i=1;i<=n;i++)
out=out+" "+to_string(i);
return out;
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (string_sequence(0) == "0");
assert (string_sequence(3) == "0 1 2 3");
assert (string_sequence(10) == "0 1 2 3 4 5 6 7 8 9 10");
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string string_sequence(int n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (string_sequence(0) == "0");
assert (string_sequence(5) == "0 1 2 3 4 5");
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string string_sequence(int n){
string out="0";
for (int i=1;i<=n;i++)
out=out+" "+to_string(i);
return out;
}
|
CPP/16 | /*
Given a string, find out how many distinct characters (regardless of case) does it consist of
>>> count_distinct_characters("xyzXYZ")
3
>>> count_distinct_characters("Jerry")
4
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int count_distinct_characters(string str){
| vector<char> distinct={};
transform(str.begin(),str.end(),str.begin(),::tolower);
for (int i=0;i<str.size();i++)
{
bool isin=false;
for (int j=0;j<distinct.size();j++)
if (distinct[j]==str[i])
isin=true;
if (isin==false) distinct.push_back(str[i]);
... | #undef NDEBUG
#include<assert.h>
int main(){
assert (count_distinct_characters("") == 0);
assert (count_distinct_characters("abcde") == 5);
assert (count_distinct_characters("abcdecadeCADE") == 5);
assert (count_distinct_characters("aaaaAAAAaaaa") == 1);
assert (count_distinct_characters("Jerry jERR... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int count_distinct_characters(string str){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (count_distinct_characters("xyzXYZ") == 3);
assert (count_distinct_characters("Jerry") == 4);
}
| #include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int count_distinct_characters(string str){
vector<char> distinct={};
transform(str.begin(),str.end(),str.begin(),::tolower);
for (int i=0;i<str.size();i++)
{
bool isi... |
CPP/17 | /*
Input to this function is a string representing musical notes in a special ASCII format.
Your task is to parse this string and return vector of integers corresponding to how many beats does each
not last.
Here is a legend:
"o" - whole note, lasts four beats
"o|" - half note, lasts two beats
".|" - quater note, last... | string current="";
vector<int> out={};
if (music_string.length()>0)
music_string=music_string+' ';
for (int i=0;i<music_string.length();i++)
{
if (music_string[i]==' ')
{
if (current=="o") out.push_back(4);
if (current=="o|") out.push_back(2);
... | #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(parse_music("") , {}));
assert (issame(parse_music("o o o o") ,{4,... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> parse_music(string music_string){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(parse_music("o o| .| o| o| .| .| .| .| o o") , {4, 2, 1, 2, 2, 1, 1, 1,... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> parse_music(string music_string){
string current="";
vector<int> out={};
if (music_string.length()>0)
music_string=music_string+' ';
for (int i=0;i<music_... |
CPP/18 | /*
Find how many times a given substring can be found in the original string. Count overlaping cases.
>>> how_many_times("", "a")
0
>>> how_many_times("aaa", "a")
3
>>> how_many_times("aaaa", "aa")
3
*/
#include<stdio.h>
#include<string>
using namespace std;
int how_many_times(string str,string substring){
| int out=0;
if (str.length()==0) return 0;
for (int i=0;i<=str.length()-substring.length();i++)
if (str.substr(i,substring.length())==substring)
out+=1;
return out;
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (how_many_times("", "x") == 0);
assert (how_many_times("xyxyxyx", "x") == 4);
assert (how_many_times("cacacacac", "cac") == 4);
assert (how_many_times("john doe", "john") == 1);
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int how_many_times(string str,string substring){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (how_many_times("", "a") == 0);
assert (how_many_times("aaa", "a") == 3);
assert (how_many_times("aaaa", "aa") == 3);
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int how_many_times(string str,string substring){
int out=0;
if (str.length()==0) return 0;
for (int i=0;i<=str.length()-substring.length();i++)
if (str.substr(i,substring.length())==substring)... |
CPP/19 | /*
Input is a space-delimited string of numberals from "zero" to "nine".
Valid choices are "zero", "one", 'two", 'three", "four", "five", 'six", 'seven", "eight" and "nine".
Return the string with numbers sorted from smallest to largest
>>> sort_numbers('three one five")
"one three five"
*/
#include<stdio.h>
#include<s... | map<string,int> tonum={{"zero",0},{"one",1},{"two",2},{"three",3},{"four",4},{"five",5},{"six",6},{"seven",7},{"eight",8},{"nine",9}};
map<int,string> numto={{0,"zero"},{1,"one"},{2,"two"},{3,"three"},{4,"four"},{5,"five"},{6,"six"},{7,"seven"},{8,"eight"},{9,"nine"}};
int count[10];
for (int i=0;i<10;i... | #undef NDEBUG
#include<assert.h>
int main(){
assert (sort_numbers("") == "");
assert (sort_numbers("three") == "three");
assert (sort_numbers("three five nine") == "three five nine");
assert (sort_numbers("five zero four seven nine eight") == "zero four five seven eight nine");
assert (sort_numbe... | #include<stdio.h>
#include<math.h>
#include<string>
#include<map>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string sort_numbers(string numbers){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (sort_numbers("three one five") == "one three five");
}
| #include<stdio.h>
#include<math.h>
#include<string>
#include<map>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string sort_numbers(string numbers){
map<string,int> tonum={{"zero",0},{"one",1},{"two",2},{"three",3},{"four",4},{"five",5},{"six",6},{"seven",7},{"eight",8},{"nine",9}};
map<int,string... |
CPP/20 | /*
From a supplied vector of numbers (of length at least two) select and return two that are the closest to each
other and return them in order (smaller number, larger number).
>>> find_closest_elements({1.0, 2.0, 3.0, 4.0, 5.0, 2.2})
(2.0, 2.2)
>>> find_closest_elements({1.0, 2.0, 3.0, 4.0, 5.0, 2.0})
(2.0, 2.0)
*/
#i... | vector<float> out={};
for (int i=0;i<numbers.size();i++)
for (int j=i+1;j<numbers.size();j++)
if (out.size()==0 or abs(numbers[i]-numbers[j])<abs(out[0]-out[1]))
out={numbers[i],numbers[j]};
if (out[0]>out[1])
out={out[1],out[0]};
return out;
}
| #undef NDEBUG
#include<assert.h>
bool issame(vector<float> a,vector<float>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (abs(a[i]-b[i])>1e-4) return false;
}
return true;
}
int main(){
assert (issame(find_closest_elements({1.0, 2.0, 3.9, 4.0, 5.0, 2.2}) , {3... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<float> find_closest_elements(vector<float> numbers){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<float> a,vector<float>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (abs(a[i]-b[i])>1e-4) return false;
}
return true;
}
int main(){
assert (issame(find_closest_elements({1.0, 2.0, 3.0, 4.0, 5.0, 2.2}) ,{2.... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<float> find_closest_elements(vector<float> numbers){
vector<float> out={};
for (int i=0;i<numbers.size();i++)
for (int j=i+1;j<numbers.size();j++)
if (out.size()==0 or abs(numbers[i... |
CPP/21 | /*
Given vector of numbers (of at least two elements), apply a linear transform to that vector,
such that the smallest number will become 0 and the largest will become 1
>>> rescale_to_unit({1.0, 2.0, 3.0, 4.0, 5.0})
{0.0, 0.25, 0.5, 0.75, 1.0}
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;... | float min=100000,max=-100000;
for (int i=0;i<numbers.size();i++)
{
if (numbers[i]<min) min=numbers[i];
if (numbers[i]>max) max=numbers[i];
}
for (int i=0;i<numbers.size();i++)
numbers[i]=(numbers[i]-min)/(max-min);
return numbers;
}
| #undef NDEBUG
#include<assert.h>
bool issame(vector<float> a,vector<float>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (abs(a[i]-b[i])>1e-4) return false;
}
return true;
}
int main(){
assert (issame(rescale_to_unit({2.0, 49.9}) , {0.0, 1.0}));
assert (... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<float> rescale_to_unit(vector<float> numbers){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<float> a,vector<float>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (abs(a[i]-b[i])>1e-4) return false;
}
return true;
}
int main(){
assert (issame(rescale_to_unit({1.0, 2.0, 3.0, 4.0, 5.0}) , {0.0, 0.25, 0... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<float> rescale_to_unit(vector<float> numbers){
float min=100000,max=-100000;
for (int i=0;i<numbers.size();i++)
{
if (numbers[i]<min) min=numbers[i];
if (number... |
CPP/22 | /*
Filter given vector of any python values only for integers
>>> filter_integers({"a", 3.14, 5})
{5}
>>> filter_integers({1, 2, 3, "abc", {}, {}})
{1, 2, 3}
*/
#include<stdio.h>
#include<vector>
#include<string>
#include<boost/any.hpp>
#include<list>
typedef std::list<boost::any> list_any;
using namespace std;
vector<... | list_any::iterator it;
boost::any anyone;
vector<int> out;
for (it=values.begin();it!=values.end();it++)
{
anyone=*it;
if( anyone.type() == typeid(int) )
out.push_back(boost::any_cast<int>(*it));
}
return out;
}
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(filter_integers({}),{}));
assert (issame(filter_integers({4, {},2... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<boost/any.hpp>
#include<list>
typedef std::list<boost::any> list_any;
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> filter_integers(list_any values){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(filter_integers({string("a"), 3.14, 5}),{5}));
assert (issame(filte... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<boost/any.hpp>
#include<list>
typedef std::list<boost::any> list_any;
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> filter_integers(list_any values){
list_any::iterator it;
boost::any anyone;
vector<int> ... |
CPP/23 | /*
Return length of given string
>>> strlen("")
0
>>> strlen("abc")
3
*/
#include<stdio.h>
#include<string>
using namespace std;
int strlen(string str){
| return str.length();
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (strlen("") == 0);
assert (strlen("x") == 1);
assert (strlen("asdasnakj") == 9);
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int strlen(string str){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (strlen("") == 0);
assert (strlen("abc") == 3);
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int strlen(string str){
return str.length();
}
|
CPP/24 | /*
For a given number n, find the largest number that divides n evenly, smaller than n
>>> largest_divisor(15)
5
*/
#include<stdio.h>
using namespace std;
int largest_divisor(int n){
| for (int i=2;i*i<=n;i++)
if (n%i==0) return n/i;
return 1;
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (largest_divisor(3) == 1);
assert (largest_divisor(7) == 1);
assert (largest_divisor(10) == 5);
assert (largest_divisor(100) == 50);
assert (largest_divisor(49) == 7);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int largest_divisor(int n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (largest_divisor(15) == 5);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int largest_divisor(int n){
for (int i=2;i*i<=n;i++)
if (n%i==0) return n/i;
return 1;
}
|
CPP/25 | /*
Return vector of prime factors of given integer in the order from smallest to largest.
Each of the factors should be vectored number of times corresponding to how many times it appeares in factorization.
Input number should be equal to the product of all factors
>>> factorize(8)
{2, 2, 2}
>>> factorize(25)
{5, 5}
>>... | vector<int> out={};
for (int i=2;i*i<=n;i++)
if (n%i==0)
{
n=n/i;
out.push_back(i);
i-=1;
}
out.push_back(n);
return out;
}
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(factorize(2) , {2}));
assert (issame(factorize(4) , {2, 2}));
a... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> factorize(int n){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(factorize(8) , {2, 2, 2}));
assert (issame(factorize(25) , {5,5}))... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> factorize(int n){
vector<int> out={};
for (int i=2;i*i<=n;i++)
if (n%i==0)
{
n=n/i;
out.push_back(i);
i-=1;
}
out.push_back(... |
CPP/26 | /*
From a vector of integers, remove all elements that occur more than once.
Keep order of elements left the same as in the input.
>>> remove_duplicates({1, 2, 3, 2, 4})
{1, 3, 4}
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> remove_duplicates(vector<int> numbers){
| vector<int> out={};
vector<int> has1={};
vector<int> has2={};
for (int i=0;i<numbers.size();i++)
{
if (find(has2.begin(),has2.end(),numbers[i])!=has2.end()) continue;
if (find(has1.begin(),has1.end(),numbers[i])!=has1.end())
{
has2.push_back(numbers[i]);
... | #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(remove_duplicates({}) , {}));
assert (issame(remove_duplicates({1,... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> remove_duplicates(vector<int> numbers){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(remove_duplicates({1, 2, 3, 2,4}) , {1, 3, 4}));
}
| #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> remove_duplicates(vector<int> numbers){
vector<int> out={};
vector<int> has1={};
vector<int> has2={};
for (int i=0;i<numbers.size();i++)
{
if (find(has2.begin(),has2.en... |
CPP/27 | /*
For a given string, flip lowercase characters to uppercase and uppercase to lowercase.
>>> flip_case("Hello")
"hELLO"
*/
#include<stdio.h>
#include<string>
using namespace std;
string filp_case(string str){
| string out="";
for (int i=0;i<str.length();i++)
{
char w=str[i];
if (w>=97 and w<=122) {w-=32;}
else
if (w>=65 and w<=90){ w+=32;}
out=out+w;
}
return out;
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (filp_case("") == "");
assert (filp_case("Hello!") == "hELLO!");
assert (filp_case("These violent delights have violent ends") == "tHESE VIOLENT DELIGHTS HAVE VIOLENT ENDS");
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string filp_case(string str){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (filp_case("Hello") == "hELLO");
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string filp_case(string str){
string out="";
for (int i=0;i<str.length();i++)
{
char w=str[i];
if (w>=97 and w<=122) {w-=32;}
else
if (w>=65 and w<=90){ w+=32;}... |
CPP/28 | /*
Concatenate vector of strings into a single string
>>> concatenate({})
""
>>> concatenate({"a", "b", "c"})
"abc"
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
string concatenate(vector<string> strings){
| string out="";
for (int i=0;i<strings.size();i++)
out=out+strings[i];
return out;
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (concatenate({}) == "");
assert (concatenate({"x", "y", "z"}) == "xyz");
assert (concatenate({"x", "y", "z", "w", "k"}) == "xyzwk");
}
| #include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string concatenate(vector<string> strings){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (concatenate({}) == "");
assert (concatenate({"a", "b", "c"}) == "abc");
}
| #include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string concatenate(vector<string> strings){
string out="";
for (int i=0;i<strings.size();i++)
out=out+strings[i];
return out;
}
|
CPP/29 | /*
Filter an input vector of strings only for ones that start with a given prefix.
>>> filter_by_prefix({}, "a")
{}
>>> filter_by_prefix({"abc", "bcd", "cde", "vector"}, "a")
{"abc", "vector"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> filter_by_prefix(vector<string> stri... | vector<string> out={};
for (int i=0;i<strings.size();i++)
if (strings[i].substr(0,prefix.length())==prefix) out.push_back(strings[i]);
return out;
}
| #undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(filter_by_prefix({}, "john") , {}));
assert (issame(filter_by_pre... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<string> filter_by_prefix(vector<string> strings, string prefix){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(filter_by_prefix({}, "a") , {}));
assert (issame(filter_by_prefix... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<string> filter_by_prefix(vector<string> strings, string prefix){
vector<string> out={};
for (int i=0;i<strings.size();i++)
if (strings[i].substr(0,prefix.length())==pre... |
CPP/30 | /*
Return only positive numbers in the vector.
>>> get_positive({-1, 2, -4, 5, 6})
{2, 5, 6}
>>> get_positive({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10})
{5, 3, 2, 3, 9, 123, 1}
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
vector<float> get_positive(vector<float> l){
| vector<float> out={};
for (int i=0;i<l.size();i++)
if (l[i]>0) out.push_back(l[i]);
return out;
}
| #undef NDEBUG
#include<assert.h>
bool issame(vector<float> a,vector<float>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (abs(a[i]-b[i])>1e-4) return false;
}
return true;
}
int main(){
assert (issame(get_positive({-1, -2, 4, 5, 6}) , {4, 5, 6} ));
assert... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<float> get_positive(vector<float> l){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<float> a,vector<float>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (abs(a[i]-b[i])>1e-4) return false;
}
return true;
}
int main(){
assert (issame(get_positive({-1, 2, -4, 5, 6}) , {2, 5, 6} ));
assert... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<float> get_positive(vector<float> l){
vector<float> out={};
for (int i=0;i<l.size();i++)
if (l[i]>0) out.push_back(l[i]);
return out;
}
|
CPP/31 | /*
Return true if a given number is prime, and false otherwise.
>>> is_prime(6)
false
>>> is_prime(101)
true
>>> is_prime(11)
true
>>> is_prime(13441)
true
>>> is_prime(61)
true
>>> is_prime(4)
false
>>> is_prime(1)
false
*/
#include<stdio.h>
using namespace std;
bool is_prime(long long n){
| if (n<2) return false;
for (long long i=2;i*i<=n;i++)
if (n%i==0) return false;
return true;
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (is_prime(6) == false);
assert (is_prime(101) == true);
assert (is_prime(11) == true);
assert (is_prime(13441) == true);
assert (is_prime(61) == true);
assert (is_prime(4) == false);
assert (is_prime(1) == false);
assert (is_prime(5) ==... | #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_prime(long long n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (is_prime(6) == false);
assert (is_prime(101) == true);
assert (is_prime(11) == true);
assert (is_prime(13441) == true);
assert (is_prime(61) == true);
assert (is_prime(4) == false);
assert (is_prime(1) == false);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_prime(long long n){
if (n<2) return false;
for (long long i=2;i*i<=n;i++)
if (n%i==0) return false;
return true;
}
|
CPP/33 | /*
This function takes a vector l and returns a vector l' such that
l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal
to the values of the corresponding indicies of l, but sorted.
>>> sort_third({1, 2, 3})
{1, 2, 3}
>>> sort_thir... | vector<int> third={};
int i;
for (i=0;i*3<l.size();i++)
third.push_back(l[i*3]);
sort(third.begin(),third.end());
vector<int> out={};
for (i=0;i<l.size();i++)
{
if (i%3==0) {out.push_back(third[i/3]);}
else out.push_back(l[i]);
}
return out;
}
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(sort_third({1, 2, 3}) , sort_third({1, 2, 3})));
assert (issame(sor... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> sort_third(vector<int> 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(sort_third({1, 2, 3}) , {1, 2, 3}));
assert (issame(sort_third({5, ... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> sort_third(vector<int> l){
vector<int> third={};
int i;
for (i=0;i*3<l.size();i++)
third.push_back(l[i*3]);
sort(third.begin(),third.end());
vector<int> out={};
... |
CPP/34 | /*
Return sorted unique elements in a vector
>>> unique({5, 3, 5, 2, 3, 3, 9, 0, 123})
{0, 2, 3, 5, 9, 123}
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> unique(vector<int> l){
| vector<int> out={};
for (int i=0;i<l.size();i++)
if (find(out.begin(),out.end(),l[i])==out.end())
out.push_back(l[i]);
sort(out.begin(),out.end());
return out;
}
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(unique({5, 3, 5, 2, 3, 3, 9, 0, 123}) , {0, 2, 3, 5, 9, 123}));
}
| #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> unique(vector<int> 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(unique({5, 3, 5, 2, 3, 3, 9, 0, 123}) , {0, 2, 3, 5, 9, 123}));
}
| #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> unique(vector<int> l){
vector<int> out={};
for (int i=0;i<l.size();i++)
if (find(out.begin(),out.end(),l[i])==out.end())
out.push_back(l[i]);
sort(out.begin(),out.e... |
CPP/35 | /*
Return maximum element in the vector.
>>> max_element({1, 2, 3})
3
>>> max_element({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10})
123
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
float max_element(vector<float> l){
| float max=-10000;
for (int i=0;i<l.size();i++)
if (max<l[i]) max=l[i];
return max;
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (abs(max_element({1, 2, 3})- 3)<1e-4);
assert (abs(max_element({5, 3, -5, 2, -3, 3, 9, 0, 124, 1, -10})- 124)<1e-4);
}
| #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
float max_element(vector<float> l){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (abs(max_element({1, 2, 3})- 3)<1e-4);
assert (abs(max_element({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10})- 123)<1e-4);
}
| #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
float max_element(vector<float> l){
float max=-10000;
for (int i=0;i<l.size();i++)
if (max<l[i]) max=l[i];
return max;
}
|
CPP/36 | /*
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){
| 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) == ... | #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int fizz_buzz(int n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (fizz_buzz(50) == 0);
assert (fizz_buzz(78) == 2);
assert (fizz_buzz(79) == 3);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int fizz_buzz(int 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;
}
}
ret... |
CPP/37 | /*
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<mat... | 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... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<float> sort_even(vector<float> l){
| #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... | #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> 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 (in... |
CPP/39 | /*
prime_fib returns n-th number that is a Fibonacci number and it's also prime.
>>> prime_fib(1)
2
>>> prime_fib(2)
3
>>> prime_fib(3)
5
>>> prime_fib(4)
13
>>> prime_fib(5)
89
*/
#include<stdio.h>
using namespace std;
int prime_fib(int n){
| int f1,f2,m;
f1=1;f2=2;
int count=0;
while (count<n)
{
f1=f1+f2;
m=f1;f1=f2;f2=m;
bool isprime=true;
for (int w=2;w*w<=f1;w++)
if (f1%w==0)
{
isprime=false; break;
}
if (isprime) count+=1;
if (count==n) ... | #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);
asse... | #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int prime_fib(int n){
| #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);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int prime_fib(int n){
int f1,f2,m;
f1=1;f2=2;
int count=0;
while (count<n)
{
f1=f1+f2;
m=f1;f1=f2;f2=m;
bool isprime=true;
for (int w=2;w*w<=f1;w++)
if (f1%w==0)... |
CPP/40 | /*
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... | 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}) ... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
bool triples_sum_to_zero(vector<int> l){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (triples_sum_to_zero({1, 3, 5, 0}) == 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({2, 4, -5, 3, 9, 7}) == true);
}
| #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.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;
}
|
CPP/41 | /*
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 m... | 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);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
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){
return n*n;
}
| |
CPP/42 | /*
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){
| 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, ... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> incr_list(vector<int> 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({1, 2, 3}) , {2, 3, 4}));
assert (issame(incr_list({5, 2,... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> incr_list(vector<int> l){
for (int i=0;i<l.size();i++)
l[i]+=1;
return l;
}
|
CPP/43 | /*
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,... | 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);
... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool pairs_sum_to_zero(vector<int> l){
| #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);
}
| #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool pairs_sum_to_zero(vector<int> l){
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;
}
|
CPP/44 | /*
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){
| 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<... | #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string change_base(int x,int base){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (change_base(8, 3) == "22");
assert (change_base(8, 2) == "1000");
assert (change_base(7, 2) == "111");
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string change_base(int x,int base){
string out="";
while (x>0)
{
out=to_string(x%base)+out;
x=x/base;
}
return out;
}
|
CPP/45 | /*
Given length of a side and high return area for a triangle.
>>> triangle_area(5, 3)
7.5
*/
#include<stdio.h>
#include<math.h>
using namespace std;
float triangle_area(float a,float h){
| return (a*h)*0.5;
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (abs(triangle_area(5, 3) - 7.5)<1e-4);
assert (abs(triangle_area(2, 2) - 2.0)<1e-4);
assert (abs(triangle_area(10, 8) - 40.0)<1e-4);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
float triangle_area(float a,float h){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (abs(triangle_area(5, 3) - 7.5)<1e-4);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
float triangle_area(float a,float h){
return (a*h)*0.5;
}
|
CPP/46 | /*
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 r... | 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);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int fib4(int n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (fib4(5) == 4);
assert (fib4(6) == 8);
assert (fib4(7) == 14);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int fib4(int n){
int f[100];
f[0]=0;
f[1]=0;
f[2]=2;
f[3]=0;
for (int i=4;i<=n;i++)
{
f[i]=f[i-1]+f[i-2]+f[i-3]+f[i-4];
}
return f[n];
}
|
CPP/47 | /*
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){
| 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 );
}
| #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
float median(vector<float> l){
| #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);
}
| #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
float median(vector<float> l){
sort(l.begin(),l.end());
if (l.size()%2==1) return l[l.size()/2];
return 0.5*(l[l.size()/2]+l[l.size()/2-1]);
}
|
CPP/48 | /*
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){
| 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 ... | #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_palindrome(string 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);
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_palindrome(string text){
string pr(text.rbegin(),text.rend());
return pr==text;
}
|
CPP/49 | /*
Return 2^n modulo p (be aware of numerics).
>>> modp(3, 5)
3
>>> modp(1101, 101)
2
>>> modp(0, 101)
1
>>> modp(3, 11)
8
>>> modp(100, 101)
1
*/
#include<stdio.h>
using namespace std;
int modp(int n,int p){
| int out=1;
for (int i=0;i<n;i++)
out=(out*2)%p;
return out;
}
| #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);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int modp(int n,int p){
| #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);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int modp(int n,int p){
int out=1;
for (int i=0;i<n;i++)
out=(out*2)%p;
return out;
}
|
CPP/51 | /*
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 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") =... | #include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
string remove_vowels(string text){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (remove_vowels("") == "");
assert (remove_vowels("abcdef\nghijklm") == "bcdf\nghjklm");
assert (remove_vowels("abcdef") == "bcdf");
assert (remove_vowels("aaaaa") == "");
assert (remove_vowels("aaBAA") == "B");
assert (remove_vowels("zbcd") == ... | #include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
string remove_vowels(string text){
string out="";
string vowels="AEIOUaeiou";
for (int i=0;i<text.length();i++)
if (find(vowels.begin(),vowels.end(),text[i])==vowels.end())
out... |
CPP/52 | /*
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){
| 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_... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool below_threshold(vector<int>l, int t){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (below_threshold({1, 2, 4, 10}, 100));
assert (not(below_threshold({1, 20, 4, 10}, 5)));
}
| #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool below_threshold(vector<int>l, int t){
for (int i=0;i<l.size();i++)
if (l[i]>=t) return false;
return true;
}
|
CPP/53 | /*
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){
| 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);
... | #include<stdio.h>
#include<stdlib.h>
using namespace std;
#include<algorithm>
#include<math.h>
int add(int x,int y){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (add(2, 3) == 5);
assert (add(5, 7) == 12);
}
| #include<stdio.h>
#include<stdlib.h>
using namespace std;
#include<algorithm>
#include<math.h>
int add(int x,int y){
return x+y;
}
|
CPP/54 | /*
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", "dddzzzzzzzddddab... | 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", "ddddddda... | #include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
bool same_chars(string s0,string s1){
| #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", "ddddddda... | #include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
bool same_chars(string s0,string s1){
for (int i=0;i<s0.length();i++)
if (find(s1.begin(),s1.end(),s0[i])==s1.end())
return false;
for (int i=0;i<s1.length();i++)
if (find(s0.begin(),s... |
CPP/55 | /*
Return n-th Fibonacci number.
>>> fib(10)
55
>>> fib(1)
1
>>> fib(8)
21
*/
#include<stdio.h>
using namespace std;
int fib(int n){
| int f[1000];
f[0]=0;f[1]=1;
for (int i=2;i<=n; i++)
f[i]=f[i-1]+f[i-2];
return f[n];
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (fib(10) == 55);
assert (fib(1) == 1);
assert (fib(8) == 21);
assert (fib(11) == 89);
assert (fib(12) == 144);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int fib(int n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (fib(10) == 55);
assert (fib(1) == 1);
assert (fib(8) == 21);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int fib(int n){
int f[1000];
f[0]=0;f[1]=1;
for (int i=2;i<=n; i++)
f[i]=f[i-1]+f[i-2];
return f[n];
}
|
CPP/56 | /*
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;
boo... | 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_brac... | #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool correct_bracketing(string brackets){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (correct_bracketing("<>"));
assert (correct_bracketing("<<><>>"));
assert (not (correct_bracketing("><<>")));
assert (not (correct_bracketing("<")));
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool correct_bracketing(string brackets){
int level=0;
for (int i=0;i<brackets.length();i++)
{
if (brackets[i]=='<') level+=1;
if (brackets[i]=='>') level-=1;
if (level<0) ... |
CPP/57 | /*
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){
| 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, ... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool monotonic(vector<float> l){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (monotonic({1, 2, 4, 10}) == true);
assert (monotonic({1, 20, 4, 10}) == false);
assert (monotonic({4, 1, 0, -10}) == true);
}
| #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool monotonic(vector<float> l){
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) retu... |
CPP/58 | /*
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){
| 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, 65... | #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){
| #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, 65... | #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={};
for (int i=0;i<l1.size();i++)
if (find(out.begin(),out.end(),l1[i])==out.end())
if (find(l2.begin(),l2.end(),... |
CPP/59 | /*
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){
| 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);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int largest_prime_factor(int n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (largest_prime_factor(2048) == 2);
assert (largest_prime_factor(13195) == 29);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int largest_prime_factor(int n){
for (int i=2;i*i<=n;i++)
while (n%i==0 and n>i) n=n/i;
return n;
}
|
CPP/60 | /*
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){
| 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);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int sum_to_n(int n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (sum_to_n(1) == 1);
assert (sum_to_n(5) == 15);
assert (sum_to_n(10) == 55);
assert (sum_to_n(30) == 465);
assert (sum_to_n(100) == 5050);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int sum_to_n(int n){
return n*(n+1)/2;
}
|
CPP/61 | /*
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;
boo... | 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_brac... | #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool correct_bracketing(string brackets){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (correct_bracketing("()"));
assert (correct_bracketing("(()())"));
assert (not (correct_bracketing(")(()")));
assert (not (correct_bracketing("(")));
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool correct_bracketing(string brackets){
int level=0;
for (int i=0;i<brackets.length();i++)
{
if (brackets[i]=='(') level+=1;
if (brackets[i]==')') level-=1;
if (level<0) ... |
CPP/62 | /*
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> derivativ... | 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... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<float> derivative(vector<float> xs){
| #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... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<float> derivative(vector<float> xs){
vector<float> out={};
for (int i=1;i<xs.size();i++)
out.push_back(i*xs[i]);
return out;
}
|
CPP/63 | /*
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
>>> ... | 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);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int fibfib(int n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (fibfib(1) == 0);
assert (fibfib(5) == 4);
assert (fibfib(8) == 24);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int fibfib(int n){
int ff[100];
ff[0]=0;
ff[1]=0;
ff[2]=1;
for (int i=3;i<=n;i++)
ff[i]=ff[i-1]+ff[i-2]+ff[i-3];
return ff[n];
}
|
CPP/64 | /*
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("ACED... | 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") == ... | #include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int vowels_count(string s){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (vowels_count("abcde") == 2);
assert (vowels_count("ACEDY") == 3);
}
| #include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int vowels_count(string s){
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.leng... |
CPP/65 | /*
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 ... | 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");
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string circular_shift(int x,int shift){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (circular_shift(12, 2) == "12");
assert (circular_shift(12, 1) == "21");
}
| #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;
xs=to_string(x);
if (xs.length()<shift)
{
string s(xs.rbegin(),xs.rend());
return s;
}
xs=xs.substr(xs.length()-shift... |
CPP/66 | /*
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>
#... | 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);... | #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int digitSum(string s){
| #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);
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int digitSum(string s){
int sum=0;
for (int i=0;i<s.length();i++)
if (s[i]>=65 and s[i]<=90)
sum+=s[i];
return sum;
}
|
CPP/67 | /*
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 th... | 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.... | #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);
asse... | #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int fruit_distribution(string s,int n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (fruit_distribution("5 apples and 6 oranges",19) == 8);
assert (fruit_distribution("0 apples and 1 oranges",3) == 2);
assert (fruit_distribution("2 apples and 3 oranges",100) == 95);
assert (fruit_distribution("1 apples and 100 oranges",120) == 19);
}
| #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="";
int is12;
is12=0;
for (int i=0;i<s.size();i++)
if (s[i]>=48 and s[i]<=57)
{
if (is12==0) nu... |
CPP/68 | /*
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 plucke... | 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}))... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> pluck(vector<int> 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(pluck({4,2,3}) , {2, 1}));
assert (issame(pluck({1,2,3}) , {2, 1}))... | #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={};
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;
}
|
CPP/69 | /*
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:
... | 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=fre... | #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,... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int search(vector<int> lst){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (search({4, 1, 2, 2, 3, 1}) == 2);
assert (search({1, 2, 2, 3, 3, 3, 4, 4, 4}) == 3);
assert (search({5, 5, 4, 4, 4}) == -1);
}
| #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={};
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]==f... |
CPP/70 | /*
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({}) =... | 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(st... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> strange_sort_list(vector<int> lst){
| #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(st... | #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={};
sort(lst.begin(),lst.end());
int l=0,r=lst.size()-1;
while (l<r)
{
out.push_back(lst[l]);
l+=1;
... |
CPP/71 | /*
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
trian... | 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(... | #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
float triangle_area(float a,float b,float c){
| #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);
}
| #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 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;
}
|
CPP/72 | /*
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 wei... | 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);
}
| #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){
| #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);
}
| #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool will_it_fly(vector<int> q,int w){
int sum=0;
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 t... |
CPP/73 | /*
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}) =... | 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 (smal... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int smallest_change(vector<int> arr){
| #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, 2, 3, 2, 1}) == 0);
assert (smallest_change({3, 1, 1, 3}) == 0);
}
| #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int smallest_change(vector<int> arr){
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;
}
|
CPP/74 | /*
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", "... | 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", "ad... | #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){
| #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", "ad... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<string> total_match(vector<string> lst1,vector<string> lst2){
int num1,num2,i;
num1=0;num2=0;
for (i=0;i<lst1.size();i++)
num1+=lst1[i].length();
for (i=0;i<lst... |
CPP/75 | /*
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){
| 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);
... | #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_multiply_prime(int a){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (is_multiply_prime(30) == true);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_multiply_prime(int a){
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;
}
|
CPP/76 | /*
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
i... | 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_simp... | #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_simple_power(int x,int n){
| #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);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_simple_power(int x,int n){
int p=1,count=0;
while (p<=x and count<100)
{
if (p==x) return true;
p=p*n;count+=1;
}
return false;
}
|
CPP/77 | /*
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<... | 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)... | #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool iscuber(int a){
| #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(0) == true);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool iscuber(int a){
for (int i=0;i*i*i<=abs(a);i++)
if (i*i*i==abs(a)) return true;
return false;
}
|
CPP/78 | /*
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,... | 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 );
... | #include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int hex_key(string num){
| #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 );
}
| #include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int hex_key(string num){
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;
}
|
CPP/79 | /*
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 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");
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string decimal_to_binary(int decimal){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (decimal_to_binary(32) == "db100000db");
assert (decimal_to_binary(15) == "db1111db");
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string decimal_to_binary(int decimal){
string out="";
if (decimal==0) return "db0db";
while (decimal>0)
{
out=to_string(decimal%2)+out;
decimal=decimal/2;
}
out="db"+ou... |
CPP/80 | /*
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("xy... | 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") == tr... | #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_happy(string s){
| #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 );
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool is_happy(string s){
if (s.length()<3) return false;
for (int i=2;i<s.length();i++)
if (s[i]==s[i-1] or s[i]==s[i-2]) return false;
return true;
}
|
CPP/81 | /*
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<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) o... | #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"... | #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){
| #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"... | #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={};
for (int i=0;i<grades.size();i++)
{
if (grades[i]>=3.9999) out.push_back("A+");
... |
CPP/82 | /*
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_... | 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);
ass... | #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool prime_length(string str){
| #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);
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool prime_length(string str){
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;
}
|
CPP/83 | /*
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){
| 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);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int starts_one_ends(int n){
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int starts_one_ends(int n){
if (n<1) return 0;
if (n==1) return 1;
int out=18;
for (int i=2;i<n;i++)
out=out*10;
return out;
}
| |
CPP/84 | /*
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:
... | 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");
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
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="";
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;
s... | |
CPP/85 | /*
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){
| 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);
}
| #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int add(vector<int> lst){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (add({4, 2, 6, 7}) == 2);
}
| #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int add(vector<int> lst){
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;
}
|
CPP/86 | /*
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... | 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("")... | #include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
string anti_shuffle(string s){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (anti_shuffle("Hi") == "Hi");
assert (anti_shuffle("hello") == "ehllo");
assert (anti_shuffle("Hello World!!!") == "Hello !!!Wdlor");
}
| #include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
string anti_shuffle(string s){
string out="";
string current="";
s=s+' ';
for (int i=0;i<s.length();i++)
if (s[i]==' ')
{
sort(current.begin(),current.end());
if (out.l... |
CPP/87 | /*
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 - {r... | 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;
}
... | #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){
| #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;
}
... | #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={};
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... |
CPP/88 | /*
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... | 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[... | #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}));
asse... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
vector<int> sort_array(vector<int> array){
| #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}));
asse... | #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.size()==0) return {};
if ((array[0]+array[array.size()-1]) %2==1)
{
sort(array.begin(),array.end());
return array;
}
el... |
CPP/89 | /*
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"
... | 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");
a... | #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string encrypt(string s){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (encrypt("hi") == "lm");
assert (encrypt("asdfghjkl") == "ewhjklnop");
assert (encrypt("gf") == "kj");
assert (encrypt("et") == "ix");
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string encrypt(string s){
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;
}
|
CPP/90 | /*
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>
#inc... | 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}) == ... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int next_smallest(vector<int> lst){
| #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);
}
| #include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int next_smallest(vector<int> lst){
sort(lst.begin(),lst.end());
for (int i=1;i<lst.size();i++)
if (lst[i]!=lst[i-1]) return lst[i];
return -1;
}
|
CPP/91 | /*
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<st... | 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]=='!') iss... | #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(... | #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int is_bored(string S){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (is_bored("Hello world") == 0);
assert (is_bored("The sky is blue. The sun is shining. I love this weather") == 1);
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int is_bored(string S){
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... |
CPP/92 | /*
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>... | 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);... | #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool any_int(float a,float b,float c){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (any_int(5, 2, 7)==true);
assert (any_int(3, 2, 2)==false);
assert (any_int(3, -2, 1)==true);
assert (any_int(3.6, -2.2, 2)==false);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
bool any_int(float a,float b,float 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;
}
|
CPP/93 | /*
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")
'tHK... | 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");
}
| #include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
string encode(string message){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (encode("test") == "TGST");
assert (encode("This is a message") == "tHKS KS C MGSSCGG");
}
| #include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
string encode(string message){
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 ... |
CPP/94 | /*
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,8327... | 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... | #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... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int skjkasdkd(vector<int> lst){
| #undef NDEBUG
#include<assert.h>
#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,16... | #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;
for (int i=0;i<lst.size();i++)
if (lst[i]>largest)
{
bool prime=true;
for (int j=2;j*j<=lst[i];j... |
CPP/95 | /*
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","bana... | 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 f... | #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... | #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){
| #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... | #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;
int islower=0,isupper=0;
if (dict.size()==0) return false;
for (it=dict.begin();it!=dict.end();it+... |
CPP/96 | /*
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... | 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}));
... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> count_up_to(int n){
| #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(11) , {2,3,5,7}));... | #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={};
int i,j;
for (i=2;i<n;i++)
if (out.size()==0) {out.push_back(i);}
else
{
bool isp=true;
for (j=0... |
CPP/97 | /*
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.... | 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 ... | #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int multiply(int a,int b){
| #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 );
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int multiply(int a,int b){
return (abs(a)%10)*(abs(b)%10);
}
|
CPP/98 | /*
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){
| 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);
}
| #include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int count_upper(string s){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (count_upper("aBCdEf") == 1);
assert (count_upper("abcdefg") == 0);
assert (count_upper("dBBE") == 0);
}
| #include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#include<stdlib.h>
int count_upper(string s){
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;
... |
CPP/99 | /*
Create a function that takes a value (string) representing a number
and returns the closest integer to it. If the number is equidistant
from two integers, round it away from zero.
Examples
>>> closest_integer("10")
10
>>> closest_integer("15.3")
15
Note:
Rounding away from zero means that if the given number is eq... | double w;
w=atof(value.c_str());
return round(w);
}
| #undef NDEBUG
#include<assert.h>
int main(){
assert (closest_integer("10") == 10);
assert (closest_integer("14.5") == 15);
assert (closest_integer("-15.5") == -16);
assert (closest_integer("15.3") == 15);
assert (closest_integer("0") == 0);
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int closest_integer(string value){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (closest_integer("10") == 10);
assert (closest_integer("15.3") == 15);
}
| #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int closest_integer(string value){
double w;
w=atof(value.c_str());
return round(w);
}
|
CPP/100 | /*
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 represent... | 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... | #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> make_a_pile(int n){
| #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}));
}
| #include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<int> make_a_pile(int n){
vector<int> out={n};
for (int i=1;i<n;i++)
out.push_back(out[out.size()-1]+2);
return out;
}
|
CPP/101 | /*
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", ... | 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", "Joh... | #include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
vector<string> words_string(string s){
| #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", "Joh... | #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="";
vector<string> out={};
s=s+' ';
for (int i=0;i<s.length();i++)
if (s[i]==' ' or s[i]==',')
{
if (cu... |
CPP/102 | /*
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(in... | 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) == ... | #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int choose_num(int x,int y){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (choose_num(12, 15) == 14);
assert (choose_num(13, 12) == -1);
}
| #include<stdio.h>
#include<math.h>
using namespace std;
#include<algorithm>
#include<stdlib.h>
int choose_num(int x,int y){
if (y<x) return -1;
if (y==x and y%2==1) return -1;
if (y%2==1) return y-1;
return y;
}
|
CPP/103 | /*
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... | 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) == "... | #include<stdio.h>
#include<math.h>
#include<string>
using namespace std;
#include<algorithm>
#include<stdlib.h>
string rounded_avg(int n,int m){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (rounded_avg(1, 5) == "11");
assert (rounded_avg(7, 5) == "-1");
assert (rounded_avg(10,20) == "1111");
assert (rounded_avg(20,33) == "11010");
}
| #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) return "-1";
int num=(m+n)/2;
string out="";
while (num>0)
{
out=to_string(num%2)+out;
num=num/2;
}
return out;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.