Dataset Viewer
Auto-converted to Parquet Duplicate
task_id
stringlengths
4
14
prompt
stringlengths
96
1.07k
declaration
stringlengths
10
433
canonical_solution
stringlengths
16
969
buggy_solution
stringlengths
16
974
bug_type
stringclasses
6 values
failure_symptoms
stringclasses
3 values
entry_point
stringlengths
1
30
import
stringclasses
19 values
test_setup
stringclasses
4 values
test
stringlengths
111
2.08k
example_test
stringlengths
0
1.39k
signature
stringlengths
4
77
docstring
stringlengths
23
926
instruction
stringlengths
104
1.04k
language
stringclasses
6 values
reference
stringlengths
41
1.2k
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...
#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) return true; return false; }
int i,j; for (i=0;i<numbers.size();i++) for (j=i+1;j<numbers.size();j++) if (numbers[i]-numbers[j]<threshold) return true; return false; }
missing logic
incorrect output
has_close_elements
#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...
#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") ; }
bool has_close_elements(vector<float> numbers, float threshold)
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
Write a C++ function `bool has_close_elements(vector<float> numbers, float threshold)` to solve the following problem: 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...
cpp
#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/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...
#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; for (i=0;i<numbers.size();i++) msum+=abs(numbers[i]-avg); return msum/numbers.size(); }
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/avg; }
variable misuse
incorrect output
mean_absolute_deviation
#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); }
#undef NDEBUG #include<assert.h> int main(){ assert (abs(mean_absolute_deviation({1.0, 2.0, 3.0, 4.0}) - 1.0) < 1e-4); }
float mean_absolute_deviation(vector<float> numbers)
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
Write a C++ function `float mean_absolute_deviation(vector<float> numbers)` to solve the following problem: 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 t...
cpp
#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){
#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(delimeter); out.push_back(numbers[i]); } return out; }
vector<int> out={}; for (int i=1;i<numbers.size();i++) { out.push_back(delimeter); out.push_back(numbers[i]); } return out; }
missing logic
incorrect output
intersperse
#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,...
#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),...
vector<int> intersperse(vector<int> numbers, int delimeter)
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}
Write a C++ function `vector<int> intersperse(vector<int> numbers, int delimeter)` to solve the following problem: 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}
cpp
#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/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...
#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}; }
int sum=0,product=0; for (int i=0;i<numbers.size();i++) { sum+=numbers[i]; product*=numbers[i]; } return {sum,product}; }
value misuse
incorrect output
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}), {...
#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}),...
vector<int> sum_product(vector<int> numbers)
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)
Write a C++ function `vector<int> sum_product(vector<int> numbers)` to solve the following problem: 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...
cpp
#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){
#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); } return out; }
vector<int> out; int max=0; for (int i=0;i<numbers.size();i++) { if (numbers[i]>max) max=numbers[i]; out.push_back(numbers[i]); } return out; }
variable misuse
incorrect output
rolling_max
#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}) , {...
#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})); }
vector<int> rolling_max(vector<int> numbers)
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}
Write a C++ function `vector<int> rolling_max(vector<int> numbers)` to solve the following problem: 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}
cpp
#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){
#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]) { output+='0'; } else output+='1'; } else { if (i>=a.length()) ...
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+='1'; } else output+='0'; } else { if (i>=a.length()) ...
value misuse
incorrect output
string_xor
#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"); }
#undef NDEBUG #include<assert.h> int main(){ assert (string_xor("010", "110") == "100"); }
string string_xor(string a,string b)
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"
Write a C++ function `string string_xor(string a,string b)` to solve the following problem: 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"
cpp
#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...
#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; }
string out; for (int i=0;i<strings.size();i++) { if (strings[i].length()<out.length()) out=strings[i]; } return out; }
operator misuse
incorrect output
longest
#undef NDEBUG #include<assert.h> int main(){ assert (longest({}) == ""); assert (longest({"x", "y", "z"}) == "x"); assert (longest({"x", "yyy", "zzzz", "www", "kkkk", "abc"}) == "zzzz"); }
#undef NDEBUG #include<assert.h> int main(){ assert (longest({}) == ""); assert (longest({"a", "b", "c"}) == "a"); assert (longest({"a", "bb", "ccc"}) == "ccc"); }
string longest(vector<string> strings)
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"
Write a C++ function `string longest(vector<string> strings)` to solve the following problem: 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...
cpp
#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/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){
#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(current); } return out; }
vector<string> out; string current=""; for (int i=0;i<str.length();i++) { current=current+str[i]; out.push_back(current); } out.push_back(current); return out; }
excess logic
incorrect output
all_prefixes
#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...
#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"})); }
vector<string> all_prefixes(string str)
Return vector of all prefixes from shortest to longest of the input string >>> all_prefixes("abc") {"a", "ab", "abc"}
Write a C++ function `vector<string> all_prefixes(string str)` to solve the following problem: Return vector of all prefixes from shortest to longest of the input string >>> all_prefixes("abc") {"a", "ab", "abc"}
cpp
#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/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){
#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) out+=1; return out; }
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; }
value misuse
incorrect output
how_many_times
#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); }
#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); }
int how_many_times(string str,string substring)
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
Write a C++ function `int how_many_times(string str,string substring)` to solve the following problem: 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
cpp
#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/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;...
#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 (numbers[i]>max) max=numbers[i]; } for (int i=0;i<numbers.size();i++) numbers[i]=(numbers[i]-min)/(max-min); return numbers; }
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; }
operator misuse
incorrect output
rescale_to_unit
#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 (...
#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...
vector<float> rescale_to_unit(vector<float> numbers)
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}
Write a C++ function `vector<float> rescale_to_unit(vector<float> numbers)` to solve the following problem: 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}...
cpp
#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<...
#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> 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; }
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) ) values.push_back(boost::any_cast<int>(*it)); } return out; }
variable misuse
incorrect output
filter_integers
#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...
#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...
vector<int> filter_integers(list_any values)
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}
Write a C++ function `vector<int> filter_integers(list_any values)` to solve the following problem: 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}
cpp
#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){
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> int strlen(string str){
return str.length(); }
return str.length() - 1; }
value misuse
incorrect output
strlen
#undef NDEBUG #include<assert.h> int main(){ assert (strlen("") == 0); assert (strlen("x") == 1); assert (strlen("asdasnakj") == 9); }
#undef NDEBUG #include<assert.h> int main(){ assert (strlen("") == 0); assert (strlen("abc") == 3); }
int strlen(string str)
Return length of given string >>> strlen("") 0 >>> strlen("abc") 3
Write a C++ function `int strlen(string str)` to solve the following problem: Return length of given string >>> strlen("") 0 >>> strlen("abc") 3
cpp
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> int strlen(string str){ return str.length(); }
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 flip_case(string str){
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> string flip_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; }
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; }
function misuse
incorrect output
flip_case
#undef NDEBUG #include<assert.h> int main(){ assert (flip_case("") == ""); assert (flip_case("Hello!") == "hELLO!"); assert (flip_case("These violent delights have violent ends") == "tHESE VIOLENT DELIGHTS HAVE VIOLENT ENDS"); }
#undef NDEBUG #include<assert.h> int main(){ assert (flip_case("Hello") == "hELLO"); }
string flip_case(string str)
For a given string, flip lowercase characters to uppercase and uppercase to lowercase. >>> flip_case("Hello") "hELLO"
Write a C++ function `string flip_case(string str)` to solve the following problem: For a given string, flip lowercase characters to uppercase and uppercase to lowercase. >>> flip_case("Hello") "hELLO"
cpp
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> string flip_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){
#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; }
string out=" "; for (int i=0;i<strings.size();i++) out=out+strings[i]; return out; }
excess logic
incorrect output
concatenate
#undef NDEBUG #include<assert.h> int main(){ assert (concatenate({}) == ""); assert (concatenate({"x", "y", "z"}) == "xyz"); assert (concatenate({"x", "y", "z", "w", "k"}) == "xyzwk"); }
#undef NDEBUG #include<assert.h> int main(){ assert (concatenate({}) == ""); assert (concatenate({"a", "b", "c"}) == "abc"); }
string concatenate(vector<string> strings)
Concatenate vector of strings into a single string >>> concatenate({}) "" >>> concatenate({"a", "b", "c"}) "abc"
Write a C++ function `string concatenate(vector<string> strings)` to solve the following problem: Concatenate vector of strings into a single string >>> concatenate({}) "" >>> concatenate({"a", "b", "c"}) "abc"
cpp
#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/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){
#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; }
vector<float> out={}; for (int i=0;i<l.size();i++) if (l[i]<0) out.push_back(l[i]); return out; }
operator misuse
incorrect output
get_positive
#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...
#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...
vector<float> get_positive(vector<float> l)
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}
Write a C++ function `vector<float> get_positive(vector<float> l)` to solve the following problem: 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}
cpp
#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){
#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; }
if (n<1) return false; for (long long i=1;i*i<=n;i++) if (n%i==0) return false; return true; }
value misuse
incorrect output
is_prime
#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) ==...
#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); }
bool is_prime(long long n)
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
Write a C++ function `bool is_prime(long long n)` to solve the following problem: 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
cpp
#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/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){
#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.end()); return out; }
sort(l.begin(),l.end()); return l; }
missing logic
incorrect output
unique
#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})); }
#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})); }
vector<int> unique(vector<int> l)
Return sorted unique elements in a vector >>> unique({5, 3, 5, 2, 3, 3, 9, 0, 123}) {0, 2, 3, 5, 9, 123}
Write a C++ function `vector<int> unique(vector<int> l)` to solve the following problem: Return sorted unique elements in a vector >>> unique({5, 3, 5, 2, 3, 3, 9, 0, 123}) {0, 2, 3, 5, 9, 123}
cpp
#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){
#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; }
float max=-10000; for (int i=0;i<l.size();i++) if (max>l[i]) max=l[i]; return max; }
operator misuse
incorrect output
max_element
#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); }
#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); }
float max_element(vector<float> l)
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
Write a C++ function `float max_element(vector<float> l)` to solve the following problem: 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
cpp
#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/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){
#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) { isprime=false; break; } if (isprime) count+=1; if (count==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=1;w*w<f1;w++) if (f1%w==0) { isprime=false; break; } if (isprime) count+=1; if (count==n) r...
value misuse
incorrect output
prime_fib
#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...
#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); }
int prime_fib(int n)
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
Write a C++ function `int prime_fib(int n)` to solve the following problem: 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
cpp
#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...
#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; }
for (int i=1;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; }
value misuse
incorrect output
triples_sum_to_zero
#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}) ...
#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); }
bool triples_sum_to_zero(vector<int> l)
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_zer...
Write a C++ function `bool triples_sum_to_zero(vector<int> l)` to solve the following problem: 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...
cpp
#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/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,...
#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; }
for (int i=0;i<l.size();i++) for (int j=i;j<l.size();j++) if (l[i]+l[j]==0) return true; return false; }
value misuse
incorrect output
pairs_sum_to_zero
#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); ...
#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); }
bool pairs_sum_to_zero(vector<int> l)
pairs_sum_to_zero takes a vector of integers as an input. it returns true if there are two distinct elements in the vector that sum to zero, and false otherwise. >>> pairs_sum_to_zero({1, 3, 5, 0}) false >>> pairs_sum_to_zero({1, 3, -2, 1}) false >>> pairs_sum_to_zero({1, 2, 3, 7}) false >>> pairs_sum_to_zero({2, 4, -5...
Write a C++ function `bool pairs_sum_to_zero(vector<int> l)` to solve the following problem: 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({...
cpp
#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){
#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; }
string out=""; while (x>0) { out=to_string(x%base)+out; x=x-base; } return out; }
operator misuse
infinite loop
change_base
#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<...
#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"); }
string change_base(int x,int base)
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"
Write a C++ function `string change_base(int x,int base)` to solve the following problem: 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"
cpp
#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){
#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; }
return (a*h)*2; }
value misuse
incorrect output
triangle_area
#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); }
#undef NDEBUG #include<assert.h> int main(){ assert (abs(triangle_area(5, 3) - 7.5)<1e-4); }
float triangle_area(float a,float h)
Given length of a side and high return area for a triangle. >>> triangle_area(5, 3) 7.5
Write a C++ function `float triangle_area(float a,float h)` to solve the following problem: Given length of a side and high return area for a triangle. >>> triangle_area(5, 3) 7.5
cpp
#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/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){
#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]); }
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()-1/2]); }
value misuse
incorrect output
median
#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 ); }
#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); }
float median(vector<float> l)
Return median of elements in the vector l. >>> median({3, 1, 2, 4, 5}) 3 >>> median({-10, 4, 6, 1000, 10, 20}) 15.0
Write a C++ function `float median(vector<float> l)` to solve the following problem: Return median of elements in the vector l. >>> median({3, 1, 2, 4, 5}) 3 >>> median({-10, 4, 6, 1000, 10, 20}) 15.0
cpp
#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){
#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; }
string pr(text.rend(),text.rbegin()); return pr==text; }
value misuse
incorrect output
is_palindrome
#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 ...
#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); }
bool is_palindrome(string text)
Checks if given string is a palindrome >>> is_palindrome("") true >>> is_palindrome("aba") true >>> is_palindrome("aaaaa") true >>> is_palindrome("zbcd") false
Write a C++ function `bool is_palindrome(string text)` to solve the following problem: Checks if given string is a palindrome >>> is_palindrome("") true >>> is_palindrome("aba") true >>> is_palindrome("aaaaa") true >>> is_palindrome("zbcd") false
cpp
#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/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){
#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; }
for (int i=2;i*i<=n;i++) while (n%i==0 and n>i) n=i/n; return n; }
variable misuse
incorrect output
largest_prime_factor
#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); }
#undef NDEBUG #include<assert.h> int main(){ assert (largest_prime_factor(2048) == 2); assert (largest_prime_factor(13195) == 29); }
int largest_prime_factor(int n)
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
Write a C++ function `int largest_prime_factor(int n)` to solve the following problem: 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
cpp
#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){
#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; }
return n*n/2; }
value misuse
incorrect output
sum_to_n
#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); }
#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); }
int sum_to_n(int n)
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
Write a C++ function `int sum_to_n(int n)` to solve the following problem: 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
cpp
#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...
#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) return false; } if (level!=0) return false; return true; }
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 true; } if (level!=0) return false; return true; }
operator misuse
incorrect output
correct_bracketing
#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...
#undef NDEBUG #include<assert.h> int main(){ assert (correct_bracketing("()")); assert (correct_bracketing("(()())")); assert (not (correct_bracketing(")(()"))); assert (not (correct_bracketing("("))); }
bool correct_bracketing(string brackets)
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
Write a C++ function `bool correct_bracketing(string brackets)` to solve the following problem: 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 >>> cor...
cpp
#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/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...
#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; }
if (a+b<=c or a+c<=b or b+c<=a) return -1; float h=(a+b+c); float area; area=pow(h*(h-a)*(h-b)*(h-c),0.5); return area; }
missing logic
incorrect output
triangle_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(...
#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); }
float triangle_area(float a,float b,float c)
Given the lengths of the three sides of a triangle. Return the area of the triangle rounded to 2 decimal points if the three sides form a valid triangle. Otherwise return -1 Three sides make a valid triangle when the sum of any two sides is greater than the third side. Example: triangle_area(3, 4, 5) == 6.00 triangle_a...
Write a C++ function `float triangle_area(float a,float b,float c)` to solve the following problem: 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...
cpp
#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/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<...
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> bool iscube(int a){
for (int i=0;i*i*i<=abs(a);i++) if (i*i*i==abs(a)) return true; return false; }
for (int i=0;i*i*i<=abs(a);i++) if (i*i==abs(a)) return true; return false; }
missing logic
incorrect output
iscube
#undef NDEBUG #include<assert.h> int main(){ assert (iscube(1) == true); assert (iscube(2) == false); assert (iscube(-1) == true); assert (iscube(64) == true); assert (iscube(180) == false); assert (iscube(1000) == true); assert (iscube(0) == true); assert (iscube(1729) == false); }
#undef NDEBUG #include<assert.h> int main(){ assert (iscube(1) == true); assert (iscube(2) == false); assert (iscube(-1) == true); assert (iscube(64) == true); assert (iscube(180) == false); assert (iscube(0) == true); }
bool iscube(int a)
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
Write a C++ function `bool iscube(int a)` to solve the following problem: 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 i...
cpp
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> bool iscube(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,...
#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; }
string key="2357BD"; int out=1; for (int i=0;i<num.length();i++) if (find(key.begin(),key.end(),num[i])!=key.end()) out+=1; return out; }
value misuse
incorrect output
hex_key
#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 ); ...
#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 ); }
int hex_key(string num)
You have been tasked to write a function that receives a hexadecimal number as a string and counts the number of hexadecimal digits that are primes (prime number, or a prime, is a natural number greater than 1 that is not a product of two smaller natural numbers). Hexadecimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A,...
Write a C++ function `int hex_key(string num)` to solve the following problem: 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 sm...
cpp
#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/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: ...
#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; sum=sum/2; } return bi; }
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; }
operator misuse
incorrect output
solve
#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"); }
string solve(int N)
Given a positive integer N, return the total sum of its digits in binary. Example For N = 1000, the sum of digits will be 1 the output should be "1". For N = 150, the sum of digits will be 6 the output should be "110". For N = 147, the sum of digits will be 12 the output should be "1100". Variables: @N integer Constrai...
Write a C++ function `string solve(int N)` to solve the following problem: 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 di...
cpp
#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/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...
#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,j}); return out; }
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({j,i}); return out; }
variable misuse
incorrect output
get_row
#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; } ...
#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; } ...
vector<vector<int>> get_row(vector<vector<int>> lst, int x)
You are given a 2 dimensional data, as a nested vectors, which is similar to matrix, however, unlike matrices, each row may contain a different number of columns. Given lst, and integer x, find integers x in the vector, and return vector of vectors, {{x1, y1}, {x2, y2} ...} such that each vector is a coordinate - {row,...
Write a C++ function `vector<vector<int>> get_row(vector<vector<int>> lst, int x)` to solve the following problem: 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 ...
cpp
#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...
#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; } else { sort(array.begin(),array.end()); vector<int> out={}; for (int i=array.size()-1;i>=0;i-=1) out.push_back(array[...
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[...
operator misuse
incorrect output
sort_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...
#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...
vector<int> sort_array(vector<int> array)
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 vec...
Write a C++ function `vector<int> sort_array(vector<int> array)` to solve the following problem: 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 o...
cpp
#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/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...
#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++) if (lst[i]%j==0) prime=false; if (prime) largest=lst[i]; } int sum=0; string s; s=to_string(largest); for...
int largest=0; for (int i=0;i<lst.size();i++) if (lst[i]>largest) { bool prime=false; for (int j=2;j*j<=lst[i];j++) if (lst[i]%j==0) prime=true; if (prime) largest=lst[i]; } int sum=0; string s; s=to_string(largest); for...
operator misuse
incorrect output
skjkasdkd
#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...
#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...
int skjkasdkd(vector<int> lst)
You are given a vector of integers. You need to find the largest prime value and return the sum of its digits. Examples: For lst = {0,3,2,1,3,5,7,4,5,5,5,2,181,32,4,32,3,2,32,324,4,3} the output should be 10 For lst = {1,0,1,8,2,4597,2,1,3,40,1,2,1,2,4,2,5,1} the output should be 25 For lst = {1,3,1,32,5107,34,83278,10...
Write a C++ function `int skjkasdkd(vector<int> lst)` to solve the following problem: 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,...
cpp
#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/108
/* Write a function count_nums which takes a vector of integers and returns the number of elements which has a sum of digits > 0. If a number is negative, then its first signed digit will be negative: e.g. -123 has signed digits -1, 2, and 3. >>> count_nums({}) == 0 >>> count_nums({-1, 11, -11}) == 1 >>> count_nums({1,...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> int count_nums(vector<int> n){
int num=0; for (int i=0;i<n.size();i++) if (n[i]>0) num+=1; else { int sum=0; int w; w=abs(n[i]); while (w>=10) { sum+=w%10; w=w/10; } sum-=w; if (sum>0) num+=1...
int num=0; for (int i=0;i<n.size();i++) if (n[i]>0) num+=1; else { int sum=0; int w; w=abs(n[i]); while (w>=10) { sum+=w%10; w=w/10; } sum-=w*-1; if (sum>0) num...
excess logic
incorrect output
count_nums
#undef NDEBUG #include<assert.h> int main(){ assert (count_nums({}) == 0); assert (count_nums({-1, -2, 0}) == 0); assert (count_nums({1, 1, 2, -2, 3, 4, 5}) == 6); assert (count_nums({1, 6, 9, -6, 0, 1, 5}) == 5); assert (count_nums({1, 100, 98, -7, 1, -1}) == 4); assert (count_nums({12, 23, 34,...
#undef NDEBUG #include<assert.h> int main(){ assert (count_nums({}) == 0); assert (count_nums({-1, 11, -11}) == 1); assert (count_nums({1, 1, 2}) == 3); }
int count_nums(vector<int> n)
Write a function count_nums which takes a vector of integers and returns the number of elements which has a sum of digits > 0. If a number is negative, then its first signed digit will be negative: e.g. -123 has signed digits -1, 2, and 3. >>> count_nums({}) == 0 >>> count_nums({-1, 11, -11}) == 1 >>> count_nums({1, 1,...
Write a C++ function `int count_nums(vector<int> n)` to solve the following problem: Write a function count_nums which takes a vector of integers and returns the number of elements which has a sum of digits > 0. If a number is negative, then its first signed digit will be negative: e.g. -123 has signed digits -1, 2, an...
cpp
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> int count_nums(vector<int> n){ int num=0; for (int i=0;i<n.size();i++) if (n[i]>0) num+=1; else { int sum=0; int w; w=abs(n[i]); ...
CPP/111
/* Given a string representing a space separated lowercase letters, return a map of the letter with the most repetition and containing the corresponding count. If several letters have the same occurrence, return all of them. Example: histogram("a b c") == {{"a", 1}, {"b", 1}, {"c", 1}} histogram("a b b a") == {{"a", 2...
#include<stdio.h> #include<math.h> #include<string> #include<map> using namespace std; #include<algorithm> #include<stdlib.h> map<char,int> histogram(string test){
map<char,int> count={},out={}; map <char,int>::iterator it; int max=0; for (int i=0;i<test.length();i++) if (test[i]!=' ') { count[test[i]]+=1; if (count[test[i]]>max) max=count[test[i]]; } for (it=count.begin();it!=count.end();it++) { char...
map<char,int> count={},out={}; map <char,int>::iterator it; int max=0; for (int i=1;i<test.length();i++) if (test[i]!=' ') { count[test[i]]+=1; if (count[test[i]]>max) max=count[test[i]]; } for (it=count.begin();it!=count.end();it++) { char...
value misuse
incorrect output
histogram
#undef NDEBUG #include<assert.h> bool issame(map<char,int> a,map<char,int> b){ if (a.size()!=b.size()) return false; map <char,int>::iterator it; for (it=a.begin();it!=a.end();it++) { char w1=it->first; int w2=it->second; if (b.find(w1)==b.end()) return false; if (b[w1]!=...
#undef NDEBUG #include<assert.h> bool issame(map<char,int> a,map<char,int> b){ if (a.size()!=b.size()) return false; map <char,int>::iterator it; for (it=a.begin();it!=a.end();it++) { char w1=it->first; int w2=it->second; if (b.find(w1)==b.end()) return false; if (b[w1]!=...
map<char,int> histogram(string test)
Given a string representing a space separated lowercase letters, return a map of the letter with the most repetition and containing the corresponding count. If several letters have the same occurrence, return all of them. Example: histogram("a b c") == {{"a", 1}, {"b", 1}, {"c", 1}} histogram("a b b a") == {{"a", 2}, {...
Write a C++ function `map<char,int> histogram(string test)` to solve the following problem: Given a string representing a space separated lowercase letters, return a map of the letter with the most repetition and containing the corresponding count. If several letters have the same occurrence, return all of them. Exampl...
cpp
#include<stdio.h> #include<math.h> #include<string> #include<map> using namespace std; #include<algorithm> #include<stdlib.h> map<char,int> histogram(string test){ map<char,int> count={},out={}; map <char,int>::iterator it; int max=0; for (int i=0;i<test.length();i++) if (test[i]!=' ') {...
CPP/121
/* Given a non-empty vector of integers, return the sum of all of the odd elements that are in even positions. Examples solution({5, 8, 7, 1}) ==> 12 solution({3, 3, 3, 3, 3}) ==> 9 solution({30, 13, 24, 321}) ==>0 */ #include<stdio.h> #include<vector> using namespace std; int solutions(vector<int> lst){
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> int solutions(vector<int> lst){
int sum=0; for (int i=0;i*2<lst.size();i++) if (lst[i*2]%2==1) sum+=lst[i*2]; return sum; }
int sum=1; for (int i=0;i*2<lst.size();i++) if (lst[i*2]%2==1) sum+=lst[i*2]; return sum; }
value misuse
incorrect output
solution
#undef NDEBUG #include<assert.h> int main(){ assert (solutions({5, 8, 7, 1}) == 12); assert (solutions({3, 3, 3, 3, 3}) == 9); assert (solutions({30, 13, 24, 321}) == 0); assert (solutions({5, 9}) == 5); assert (solutions({2, 4, 8}) == 0); assert (solutions({30, 13, 23, 32}) == 23); asser...
#undef NDEBUG #include<assert.h> int main(){ assert (solutions({5, 8, 7, 1}) == 12); assert (solutions({3, 3, 3, 3, 3}) == 9); assert (solutions({30, 13, 24, 321}) == 0); }
int solutions(vector<int> lst)
Given a non-empty vector of integers, return the sum of all of the odd elements that are in even positions. Examples solution({5, 8, 7, 1}) ==> 12 solution({3, 3, 3, 3, 3}) ==> 9 solution({30, 13, 24, 321}) ==>0
Write a C++ function `int solutions(vector<int> lst)` to solve the following problem: Given a non-empty vector of integers, return the sum of all of the odd elements that are in even positions. Examples solution({5, 8, 7, 1}) ==> 12 solution({3, 3, 3, 3, 3}) ==> 9 solution({30, 13, 24, 321}) ==>0
cpp
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> int solutions(vector<int> lst){ int sum=0; for (int i=0;i*2<lst.size();i++) if (lst[i*2]%2==1) sum+=lst[i*2]; return sum; }
CPP/123
/* Given a positive integer n, return a sorted vector that has the odd numbers in collatz sequence. The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term i...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> get_odd_collatz(int n){
vector<int> out={1}; while (n!=1) { if (n%2==1) {out.push_back(n); n=n*3+1;} else n=n/2; } sort(out.begin(),out.end()); return out; }
vector<int> out={1}; while (n!=1) { if (n%2==1) {out.push_back(n); n=n*2+1;} else n=n/2; } sort(out.begin(),out.end()); return out; }
value misuse
incorrect output
get_odd_collatz
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(get_odd_collatz(14) , {1, 5, 7, 11, 13, 17})); assert (issame(get_odd_c...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(get_odd_collatz(5) , {1, 5})); }
vector<int> get_odd_collatz(int n)
Given a positive integer n, return a sorted vector that has the odd numbers in collatz sequence. The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is eve...
Write a C++ function `vector<int> get_odd_collatz(int n)` to solve the following problem: Given a positive integer n, return a sorted vector that has the odd numbers in collatz sequence. The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows: start with any positive integer n....
cpp
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> get_odd_collatz(int n){ vector<int> out={1}; while (n!=1) { if (n%2==1) {out.push_back(n); n=n*3+1;} else n=n/2; } sort(out.begin(),out.end()); return out; ...
CPP/139
/* The Brazilian factorial is defined as: brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1! where n > 0 For example: >>> special_factorial(4) 288 The function will receive an integer as input and should return the special factorial of this integer. */ #include<stdio.h> using namespace std; long long special_fa...
#include<stdio.h> #include<math.h> #include<algorithm> using namespace std; #include<stdlib.h> long long special_factorial(int n){
long long fact=1,bfact=1; for (int i=1;i<=n;i++) { fact=fact*i; bfact=bfact*fact; } return bfact; }
long long fact=1,bfact=1; for (int i=1;i<=n;i++) { i=i*n; fact=fact*i; bfact=bfact*fact; } return bfact; }
excess logic
incorrect output
special_factorial
#undef NDEBUG #include<assert.h> int main(){ assert (special_factorial(4) == 288); assert (special_factorial(5) == 34560); assert (special_factorial(7) == 125411328000); assert (special_factorial(1) == 1); }
#undef NDEBUG #include<assert.h> int main(){ assert (special_factorial(4) == 288); }
long long special_factorial(int n)
The Brazilian factorial is defined as: brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1! where n > 0 For example: >>> special_factorial(4) 288 The function will receive an integer as input and should return the special factorial of this integer.
Write a C++ function `long long special_factorial(int n)` to solve the following problem: The Brazilian factorial is defined as: brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1! where n > 0 For example: >>> special_factorial(4) 288 The function will receive an integer as input and should return the special fact...
cpp
#include<stdio.h> #include<math.h> #include<algorithm> using namespace std; #include<stdlib.h> long long special_factorial(int n){ long long fact=1,bfact=1; for (int i=1;i<=n;i++) { fact=fact*i; bfact=bfact*fact; } return bfact; }
CPP/144
/* Your task is to implement a function that will simplify the expression x * n. The function returns true if x * n evaluates to a whole number and false otherwise. Both x and n, are string representation of a fraction, and have the following format, <numerator>/<denominator> where both numerator and denominator are po...
#include<stdio.h> #include<string> #include<algorithm> using namespace std; #include<math.h> #include<stdlib.h> bool simplify(string x,string n){
int a,b,c,d,i; for (i=0;i<x.size();i++) if (x[i]=='/') { a=atoi(x.substr(0,i).c_str()); b=atoi(x.substr(i+1).c_str()); } for (i=0;i<n.size();i++) if (n[i]=='/') { c=atoi(n.substr(0,i).c_str()); d=atoi(n.substr(i+1).c_s...
int a,b,c,d,i; for (i=0;i<x.size();i++) if (x[i]=='/') { a=atoi(x.substr(0,i).c_str()); b=atoi(x.substr(i+1).c_str()); } for (i=0;i<n.size();i++) if (n[i]=='/') { c=atoi(n.substr(0,i).c_str()); d=atoi(n.substr(i+1).c_s...
excess logic
incorrect output
simplify
#undef NDEBUG #include<assert.h> int main(){ assert (simplify("1/5", "5/1") == true); assert (simplify("1/6", "2/1") == false); assert (simplify("5/1", "3/1") == true); assert (simplify("7/10", "10/2") == false); assert (simplify("2/10", "50/10") == true); assert (simplify("7/2", "4/2") == true)...
#undef NDEBUG #include<assert.h> int main(){ assert (simplify("1/5", "5/1") == true); assert (simplify("1/6", "2/1") == false); assert (simplify("7/10", "10/2") == false); }
bool simplify(string x,string n)
Your task is to implement a function that will simplify the expression x * n. The function returns true if x * n evaluates to a whole number and false otherwise. Both x and n, are string representation of a fraction, and have the following format, <numerator>/<denominator> where both numerator and denominator are posit...
Write a C++ function `bool simplify(string x,string n)` to solve the following problem: Your task is to implement a function that will simplify the expression x * n. The function returns true if x * n evaluates to a whole number and false otherwise. Both x and n, are string representation of a fraction, and have the fo...
cpp
#include<stdio.h> #include<string> #include<algorithm> using namespace std; #include<math.h> #include<stdlib.h> bool simplify(string x,string n){ int a,b,c,d,i; for (i=0;i<x.size();i++) if (x[i]=='/') { a=atoi(x.substr(0,i).c_str()); b=atoi(x.substr(i+1).c_str()); ...
CPP/145
/* Write a function which sorts the given vector of integers in ascending order according to the sum of their digits. Note: if there are several items with similar sum of their digits, order them based on their index in original vector. For example: >>> order_by_points({1, 11, -1, -11, -12}) == {-1, -11, 1, -12, 11} >...
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> order_by_points(vector<int> nums){
vector<int> sumdigit={}; for (int i=0;i<nums.size();i++) { string w=to_string(abs(nums[i])); int sum=0; for (int j=1;j<w.length();j++) sum+=w[j]-48; if (nums[i]>0) sum+=w[0]-48; else sum-=w[0]-48; sumdigit.push_back(sum); } int m; for (...
vector<int> sumdigit={}; for (int i=0;i<nums.size();i++) { string w=to_string(abs(nums[i])); int sum=0; for (int j=1;j<w.length();j++) sum+=w[j]-48; if (nums[i]>0) sum+=w[0]-48; else sum-=w[0]-48; sumdigit.push_back(sum); } int m; for (...
excess logic
incorrect output
order_by_points
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(order_by_points({1, 11, -1, -11, -12}) , {-1, -11, 1, -12, 11})); asser...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(order_by_points({1, 11, -1, -11, -12}) , {-1, -11, 1, -12, 11})); asser...
vector<int> order_by_points(vector<int> nums)
Write a function which sorts the given vector of integers in ascending order according to the sum of their digits. Note: if there are several items with similar sum of their digits, order them based on their index in original vector. For example: >>> order_by_points({1, 11, -1, -11, -12}) == {-1, -11, 1, -12, 11} >>> o...
Write a C++ function `vector<int> order_by_points(vector<int> nums)` to solve the following problem: Write a function which sorts the given vector of integers in ascending order according to the sum of their digits. Note: if there are several items with similar sum of their digits, order them based on their index in or...
cpp
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> order_by_points(vector<int> nums){ vector<int> sumdigit={}; for (int i=0;i<nums.size();i++) { string w=to_string(abs(nums[i])); int sum=0; for ...
CPP/148
/* There are eight planets in our solar system: the closerst to the Sun is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn, Uranus, Neptune. Write a function that takes two planet names as strings planet1 and planet2. The function should return a vector containing all planets whose orbits are loca...
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> vector<string> bf(string planet1,string planet2){
vector<string> planets={"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"}; int pos1=-1,pos2=-1,m; for (m=0;m<planets.size();m++) { if (planets[m]==planet1) pos1=m; if (planets[m]==planet2) pos2=m; } if (pos1==-1 or pos2==-1) return {}; if (pos1>pos2) {m=pos1;pos...
vector<string> planets={"Mercury","Venus","Earth","Mars","Jupyter","Saturn","Uranus","Neptune"}; int pos1=-1,pos2=-1,m; for (m=0;m<planets.size();m++) { if (planets[m]==planet1) pos1=m; if (planets[m]==planet2) pos2=m; } if (pos1==-1 or pos2==-1) return {}; if (pos1>pos2) {m=pos1;pos...
value misuse
incorrect output
bf
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(bf("Jupiter", "Neptune") , {"Saturn", "Uranus"})); assert (issame...
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(bf("Jupiter", "Neptune") , {"Saturn", "Uranus"})); assert (issame...
vector<string> bf(string planet1,string planet2)
There are eight planets in our solar system: the closerst to the Sun is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn, Uranus, Neptune. Write a function that takes two planet names as strings planet1 and planet2. The function should return a vector containing all planets whose orbits are located bet...
Write a C++ function `vector<string> bf(string planet1,string planet2)` to solve the following problem: There are eight planets in our solar system: the closerst to the Sun is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn, Uranus, Neptune. Write a function that takes two planet names as strings plan...
cpp
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> vector<string> bf(string planet1,string planet2){ vector<string> planets={"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"}; int pos1=-1,pos2=-1,m; for (m=0;...
CPP/150
/* A simple program which should return the value of x if n is a prime number and should return the value of y otherwise. Examples: for x_or_y(7, 34, 12) == 34 for x_or_y(15, 8, 5) == 5 */ #include<stdio.h> using namespace std; int x_or_y(int n,int x,int y){
#include<stdio.h> #include<math.h> #include<algorithm> using namespace std; #include<stdlib.h> int x_or_y(int n,int x,int y){
bool isp=true; if (n<2) isp=false; for (int i=2;i*i<=n;i++) if (n%i==0) isp=false; if (isp) return x; return y; }
bool isp=true; if (n<2) isp=false; for (int i=2;i*i<=n;i++) if (n%i-1==0) isp=false; if (isp) return x; return y; }
excess logic
incorrect output
x_or_y
#undef NDEBUG #include<assert.h> int main(){ assert (x_or_y(7, 34, 12) == 34); assert (x_or_y(15, 8, 5) == 5); assert (x_or_y(3, 33, 5212) == 33); assert (x_or_y(1259, 3, 52) == 3); assert (x_or_y(7919, -1, 12) == -1); assert (x_or_y(3609, 1245, 583) == 583); assert (x_or_y(91, 56, 129) == 1...
#undef NDEBUG #include<assert.h> int main(){ assert (x_or_y(7, 34, 12) == 34); assert (x_or_y(15, 8, 5) == 5); }
int x_or_y(int n,int x,int y)
A simple program which should return the value of x if n is a prime number and should return the value of y otherwise. Examples: for x_or_y(7, 34, 12) == 34 for x_or_y(15, 8, 5) == 5
Write a C++ function `int x_or_y(int n,int x,int y)` to solve the following problem: A simple program which should return the value of x if n is a prime number and should return the value of y otherwise. Examples: for x_or_y(7, 34, 12) == 34 for x_or_y(15, 8, 5) == 5
cpp
#include<stdio.h> #include<math.h> #include<algorithm> using namespace std; #include<stdlib.h> int x_or_y(int n,int x,int y){ bool isp=true; if (n<2) isp=false; for (int i=2;i*i<=n;i++) if (n%i==0) isp=false; if (isp) return x; return y; }
CPP/156
/* Given a positive integer, obtain its roman numeral equivalent as a string, and return it in lowercase. Restrictions: 1 <= num <= 1000 Examples: >>> int_to_mini_roman(19) == "xix" >>> int_to_mini_roman(152) == "clii" >>> int_to_mini_roman(426) == "cdxxvi" */ #include<stdio.h> #include<vector> #include<string> using ...
#include<stdio.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> string int_to_mini_romank(int number){
string current=""; vector<string> rep={"m","cm","d","cd","c","xc","l","xl","x","ix","v","iv","i"}; vector<int> num={1000,900,500,400,100,90,50,40,10,9,5,4,1}; int pos=0; while(number>0) { while (number>=num[pos]) { current=current+rep[pos]; number-=num[pos...
string current=""; vector<string> rep={"m","cm","d","cd","c","xc","l","xl","x","ix","v","iv","i"}; vector<int> num={1000,900,500,400,100,90,50,40,10,9,5,4,1}; int pos=0; while(number>0) { while (number>=num[pos]) { current=current+rep[pos]; } if (numbe...
missing logic
infinite loop
int_to_mini_roman
#undef NDEBUG #include<assert.h> int main(){ assert (int_to_mini_romank(19) == "xix"); assert (int_to_mini_romank(152) == "clii"); assert (int_to_mini_romank(251) == "ccli"); assert (int_to_mini_romank(426) == "cdxxvi"); assert (int_to_mini_romank(500) == "d"); assert (int_to_mini_romank(1) == "...
#undef NDEBUG #include<assert.h> int main(){ assert (int_to_mini_romank(19) == "xix"); assert (int_to_mini_romank(152) == "clii"); assert (int_to_mini_romank(426) == "cdxxvi"); }
string int_to_mini_romank(int number)
Given a positive integer, obtain its roman numeral equivalent as a string, and return it in lowercase. Restrictions: 1 <= num <= 1000 Examples: >>> int_to_mini_roman(19) == "xix" >>> int_to_mini_roman(152) == "clii" >>> int_to_mini_roman(426) == "cdxxvi"
Write a C++ function `string int_to_mini_romank(int number)` to solve the following problem: Given a positive integer, obtain its roman numeral equivalent as a string, and return it in lowercase. Restrictions: 1 <= num <= 1000 Examples: >>> int_to_mini_roman(19) == "xix" >>> int_to_mini_roman(152) == "clii" >>> int_to_...
cpp
#include<stdio.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> string int_to_mini_romank(int number){ string current=""; vector<string> rep={"m","cm","d","cd","c","xc","l","xl","x","ix","v","iv","i"}; vector<int> num={1000,900,500,400,100,90,...
Go/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 list of those. // Separate groups are balanced (each open brace is properly closed) and not nested within each other // Ignore any spaces in the input str...
func SeparateParenGroups(paren_string string) []string {
result := make([]string, 0) current_string := make([]rune, 0) current_depth := 0 for _, c := range paren_string { if c == '(' { current_depth += 1 current_string = append(current_string, c) }else if c== ')'{ current_depth -= 1 current_stri...
result := make([]string, 0) current_string := make([]rune, 0) current_depth := 0 for _, c := range paren_string { if c == '(' { current_depth += 1 current_string = append(current_string, c) }else if c== ')'{ current_depth -= 1 current_stri...
operator misuse
incorrect output
SeparateParenGroups
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestSeparateParenGroups(t *testing.T) { assert := assert.New(t) assert.Equal([]string{"(()())", "((()))", "()", "((())()())"}, SeparateParenGroups("(()()) ((())) () ((())()())")) assert.Equal([]string{"()", "(())", "((()))", "(((())))"}, SeparateParenGroups("() (()) ((())) (((())))")) assert.Equal(...
func TestSeparateParenGroups(t *testing.T) { assert := assert.New(t) assert.Equal([]string{"()", "(())", "(()())"}, SeparateParenGroups("( ) (( )) (( )( ))")) }
func SeparateParenGroups(paren_string string) []string
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 list 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. >>> Sepa...
Write a Go function `func SeparateParenGroups(paren_string string) []string` to solve the following problem: 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 list of those. Separate groups are balanced (each op...
go
func SeparateParenGroups(paren_string string) []string { result := make([]string, 0) current_string := make([]rune, 0) current_depth := 0 for _, c := range paren_string { if c == '(' { current_depth += 1 current_string = append(current_string, c) }else if c== ')'...
Go/2
import ( "math" ) // 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. // >>> TruncateNumber(3.5) // 0.5 func TruncateNumber(number fl...
func TruncateNumber(number float64) float64 {
return math.Mod(number,1) }
return math.Mod(number,1)+1 }
excess logic
incorrect output
TruncateNumber
import ( "math" )
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestTruncateNumber(t *testing.T) { assert := assert.New(t) assert.Equal(0.5, TruncateNumber(3.5)) assert.Equal(true, math.Abs(TruncateNumber(1.33)-0.33) < 1e-6) assert.Equal(true, math.Abs(TruncateNumber(123.456)-0.456) < 1e-6) }
func TestTruncateNumber(t *testing.T) { assert := assert.New(t) assert.Equal(0.5, TruncateNumber(3.5)) }
func TruncateNumber(number float64) float64
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. >>> TruncateNumber(3.5) 0.5
Write a Go function `func TruncateNumber(number float64) float64` to solve the following problem: 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. >>> ...
go
func TruncateNumber(number float64) float64 { return math.Mod(number,1) }
Go/4
import ( "math" ) // For a given list 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 | // >>> MeanAbsoluteDeviation([1....
func MeanAbsoluteDeviation(numbers []float64) float64 {
sum := func(numbers []float64) float64 { sum := 0.0 for _, num := range numbers { sum += num } return sum } mean := sum(numbers) / float64(len(numbers)) numList := make([]float64, 0) for _, x := range numbers { numList = append(numList, math.Abs(x...
sum := func(numbers []float64) float64 { sum := 0.0 for _, num := range numbers { sum += num } return sum } mean := sum(numbers) / float64(len(numbers)) numList := make([]float64, 0) for _, x := range numbers { numList = append(numList, math.Abs(x...
variable misuse
incorrect output
MeanAbsoluteDeviation
import ( "math" )
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestMeanAbsoluteDeviation(t *testing.T) { assert := assert.New(t) assert.Equal(true, math.Abs(MeanAbsoluteDeviation([]float64{1.0, 2.0, 3.0})-2.0/3.0) < 1e-6) assert.Equal(true, math.Abs(MeanAbsoluteDeviation([]float64{1.0, 2.0, 3.0, 4.0})-1.0) < 1e-6) assert.Equal(true, math.Abs(MeanAbsoluteDeviat...
func TestMeanAbsoluteDeviation(t *testing.T) { assert := assert.New(t) assert.Equal(true, math.Abs(MeanAbsoluteDeviation([]float64{1.0, 2.0, 3.0, 4.0})-1.0) < 1e-6) }
func MeanAbsoluteDeviation(numbers []float64) float64
For a given list 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 | >>> MeanAbsoluteDeviation([1.0, 2.0, 3.0, 4.0]) 1.0
Write a Go function `func MeanAbsoluteDeviation(numbers []float64) float64` to solve the following problem: For a given list 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 thi...
go
func MeanAbsoluteDeviation(numbers []float64) float64 { sum := func(numbers []float64) float64 { sum := 0.0 for _, num := range numbers { sum += num } return sum } mean := sum(numbers) / float64(len(numbers)) numList := make([]float64, 0) for _, x := rang...
Go/5
// Insert a number 'delimeter' between every two consecutive elements of input list `numbers' // >>> Intersperse([], 4) // [] // >>> Intersperse([1, 2, 3], 4) // [1, 4, 2, 4, 3] func Intersperse(numbers []int, delimeter int) []int {
func Intersperse(numbers []int, delimeter int) []int {
result := make([]int, 0) if len(numbers) == 0 { return result } for i := 0; i < len(numbers)-1; i++ { n := numbers[i] result = append(result, n) result = append(result, delimeter) } result = append(result, numbers[len(numbers)-1]) return result }
result := make([]int, 0) if len(numbers) == 0 { return result } for i := 0; i < len(numbers)-1; i++ { n := numbers[i] result = append(result, n) result = append(result, delimeter) } return result }
missing logic
incorrect output
Intersperse
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestIntersperse(t *testing.T) { assert := assert.New(t) assert.Equal([]int{}, Intersperse([]int{}, 7)) assert.Equal([]int{5, 8, 6, 8, 3, 8, 2}, Intersperse([]int{5, 6, 3, 2}, 8)) assert.Equal([]int{2, 2, 2, 2, 2}, Intersperse([]int{2, 2, 2}, 2)) }
func TestIntersperse(t *testing.T) { assert := assert.New(t) assert.Equal([]int{}, Intersperse([]int{}, 4)) assert.Equal([]int{1,4,2,4,3}, Intersperse([]int{1,2,3}, 4)) }
func Intersperse(numbers []int, delimeter int) []int
Insert a number 'delimeter' between every two consecutive elements of input list `numbers' >>> Intersperse([], 4) [] >>> Intersperse([1, 2, 3], 4) [1, 4, 2, 4, 3]
Write a Go function `func Intersperse(numbers []int, delimeter int) []int` to solve the following problem: Insert a number 'delimeter' between every two consecutive elements of input list `numbers' >>> Intersperse([], 4) [] >>> Intersperse([1, 2, 3], 4) [1, 4, 2, 4, 3]
go
func Intersperse(numbers []int, delimeter int) []int { result := make([]int, 0) if len(numbers) == 0 { return result } for i := 0; i < len(numbers)-1; i++ { n := numbers[i] result = append(result, n) result = append(result, delimeter) } result = append(result, num...
Go/6
import ( "math" "strings" ) // 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. // // >>> ParseNested...
func ParseNestedParens(paren_string string) []int {
parse_paren_group := func(s string) int { depth := 0 max_depth := 0 for _, c := range s { if c == '(' { depth += 1 max_depth = int(math.Max(float64(depth), float64(max_depth))) } else { depth -= 1 } }...
parse_paren_group := func(s string) int { depth := 0 max_depth := 0 for _, c := range s { if c == '(' { depth += 1 max_depth = int(math.Max(float64(depth), float64(max_depth))) } else { max_depth -= 1 } ...
variable misuse
incorrect output
ParseNestedParens
import ( "math" "strings" )
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestParseNestedParens(t *testing.T) { assert := assert.New(t) assert.Equal([]int{2, 3, 1, 3}, ParseNestedParens("(()()) ((())) () ((())()())")) assert.Equal([]int{1, 2, 3, 4}, ParseNestedParens("() (()) ((())) (((())))")) assert.Equal([]int{4}, ParseNestedParens("(()(())((())))")) }
func TestParseNestedParens(t *testing.T) { assert := assert.New(t) assert.Equal([]int{2, 3, 1, 3}, ParseNestedParens("(()()) ((())) () ((())()())")) }
func ParseNestedParens(paren_string string) []int
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. >>> ParseNestedParens('(()()) ((())) () ((())()())') [2, 3, 1, 3]
Write a Go function `func ParseNestedParens(paren_string string) []int` to solve the following problem: 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...
go
func ParseNestedParens(paren_string string) []int { parse_paren_group := func(s string) int { depth := 0 max_depth := 0 for _, c := range s { if c == '(' { depth += 1 max_depth = int(math.Max(float64(depth), float64(max_depth))) } else ...
Go/7
import ( "strings" ) // Filter an input list of strings only for ones that contain given substring // >>> FilterBySubstring([], 'a') // [] // >>> FilterBySubstring(['abc', 'bacd', 'cde', 'array'], 'a') // ['abc', 'bacd', 'array'] func FilterBySubstring(stringList []string, substring string) []string {
func FilterBySubstring(stringList []string, substring string) []string {
result := make([]string, 0) for _, x := range stringList { if strings.Index(x, substring) != -1 { result = append(result, x) } } return result }
result := make([]string, 0) for _, x := range stringList { if strings.Index(substring, x) != -1 { result = append(result, x) } } return result }
variable misuse
incorrect output
FilterBySubstring
import ( "strings" )
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestFilterBySubstring(t *testing.T) { assert := assert.New(t) assert.Equal([]string{}, FilterBySubstring([]string{}, "john")) assert.Equal([]string{"xxx", "xxxAAA", "xxx"}, FilterBySubstring([]string{"xxx", "asd", "xxy", "john doe", "xxxAAA", "xxx"}, "xxx")) assert.Equal([]string{"xxx", "aaaxxy", "...
func TestFilterBySubstring(t *testing.T) { assert := assert.New(t) assert.Equal([]string{}, FilterBySubstring([]string{}, "a")) assert.Equal([]string{"abc", "bacd", "array"}, FilterBySubstring([]string{"abc", "bacd", "cde", "array"}, "a")) }
func FilterBySubstring(stringList []string, substring string) []string
Filter an input list of strings only for ones that contain given substring >>> FilterBySubstring([], 'a') [] >>> FilterBySubstring(['abc', 'bacd', 'cde', 'array'], 'a') ['abc', 'bacd', 'array']
Write a Go function `func FilterBySubstring(stringList []string, substring string) []string` to solve the following problem: Filter an input list of strings only for ones that contain given substring >>> FilterBySubstring([], 'a') [] >>> FilterBySubstring(['abc', 'bacd', 'cde', 'array'], 'a') ['abc', 'bacd', 'array']
go
func FilterBySubstring(stringList []string, substring string) []string { result := make([]string, 0) for _, x := range stringList { if strings.Index(x, substring) != -1 { result = append(result, x) } } return result }
Go/8
// For a given list of integers, return a tuple consisting of a sum and a product of all the integers in a list. // Empty sum should be equal to 0 and empty product should be equal to 1. // >>> SumProduct([]) // (0, 1) // >>> SumProduct([1, 2, 3, 4]) // (10, 24) func SumProduct(numbers []int) [2]int {
func SumProduct(numbers []int) [2]int {
sum_value := 0 prod_value := 1 for _, n := range numbers { sum_value += n prod_value *= n } return [2]int{sum_value, prod_value} }
sum_value := 0 prod_value := 0 for _, n := range numbers { sum_value += n prod_value *= n } return [2]int{sum_value, prod_value} }
value misuse
incorrect output
SumProduct
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestSumProduct(t *testing.T) { assert := assert.New(t) assert.Equal([2]int{0, 1}, SumProduct([]int{})) assert.Equal([2]int{3, 1}, SumProduct([]int{1, 1, 1})) assert.Equal([2]int{100, 0}, SumProduct([]int{100, 0})) assert.Equal([2]int{3 + 5 + 7, 3 * 5 * 7}, SumProduct([]int{3, 5, 7})) assert...
func TestSumProduct(t *testing.T) { assert := assert.New(t) assert.Equal([2]int{0, 1}, SumProduct([]int{})) assert.Equal([2]int{10,24}, SumProduct([]int{1, 2,3,4})) }
func SumProduct(numbers []int) [2]int
For a given list of integers, return a tuple consisting of a sum and a product of all the integers in a list. Empty sum should be equal to 0 and empty product should be equal to 1. >>> SumProduct([]) (0, 1) >>> SumProduct([1, 2, 3, 4]) (10, 24)
Write a Go function `func SumProduct(numbers []int) [2]int` to solve the following problem: For a given list of integers, return a tuple consisting of a sum and a product of all the integers in a list. Empty sum should be equal to 0 and empty product should be equal to 1. >>> SumProduct([]) (0, 1) >>> SumProduct([1, 2,...
go
func SumProduct(numbers []int) [2]int { sum_value := 0 prod_value := 1 for _, n := range numbers { sum_value += n prod_value *= n } return [2]int{sum_value, prod_value} }
Go/9
import ( "math" ) // From a given list of integers, generate a list of rolling maximum element found until given moment // in the sequence. // >>> RollingMax([1, 2, 3, 2, 3, 4, 2]) // [1, 2, 3, 3, 3, 4, 4] func RollingMax(numbers []int) []int {
func RollingMax(numbers []int) []int {
running_max := math.MinInt32 result := make([]int, 0) for _, n := range numbers { if running_max == math.MinInt32 { running_max = n } else { running_max = int(math.Max(float64(running_max), float64(n))) } result = append(result, running_max) } ...
running_max := math.MinInt32 result := make([]int, 0) for _, n := range numbers { if running_max == math.MinInt32 { running_max = n } else { running_max = int(math.Max(numbers)) } result = append(result, running_max) } return result }
variable misuse
incorrect output
RollingMax
import ( "math" )
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestRollingMax(t *testing.T) { assert := assert.New(t) assert.Equal([]int{}, RollingMax([]int{})) assert.Equal([]int{1, 2, 3, 4}, RollingMax([]int{1, 2, 3, 4})) assert.Equal([]int{4, 4, 4, 4}, RollingMax([]int{4, 3, 2, 1})) assert.Equal([]int{3, 3, 3, 100, 100}, RollingMax([]int{3, 2, 3, 100, 3...
func TestRollingMax(t *testing.T) { assert := assert.New(t) assert.Equal([]int{1, 2, 3,3, 3, 4, 4}, RollingMax([]int{1, 2, 3, 2, 3, 4, 2})) }
func RollingMax(numbers []int) []int
From a given list of integers, generate a list of rolling maximum element found until given moment in the sequence. >>> RollingMax([1, 2, 3, 2, 3, 4, 2]) [1, 2, 3, 3, 3, 4, 4]
Write a Go function `func RollingMax(numbers []int) []int` to solve the following problem: From a given list of integers, generate a list of rolling maximum element found until given moment in the sequence. >>> RollingMax([1, 2, 3, 2, 3, 4, 2]) [1, 2, 3, 3, 3, 4, 4]
go
func RollingMax(numbers []int) []int { running_max := math.MinInt32 result := make([]int, 0) for _, n := range numbers { if running_max == math.MinInt32 { running_max = n } else { running_max = int(math.Max(float64(running_max), float64(n))) } result ...
Go/13
// Return a greatest common divisor of two integers a and b // >>> GreatestCommonDivisor(3, 5) // 1 // >>> GreatestCommonDivisor(25, 15) // 5 func GreatestCommonDivisor(a int,b int) int{
func GreatestCommonDivisor(a int,b int) int{
if b < 2 { return b } var gcd int = 1 for i := 2; i < b; i++ { if a%i == 0 && b%i == 0 { gcd = i } } return gcd }
if b < 2 { return a } var gcd int = 1 for i := 2; i < b; i++ { if a%i == 0 && b%i == 0 { gcd = a } } return gcd }
variable misuse
incorrect output
GreatestCommonDivisor
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestGreatestCommonDivisor(t *testing.T) { assert := assert.New(t) assert.Equal(1, GreatestCommonDivisor(3, 7)) assert.Equal(5, GreatestCommonDivisor(10, 15)) assert.Equal(7, GreatestCommonDivisor(49, 14)) assert.Equal(12, GreatestCommonDivisor(144, 60)) }
func TestGreatestCommonDivisor(t *testing.T) { assert := assert.New(t) assert.Equal(1, GreatestCommonDivisor(3, 5)) assert.Equal(5, GreatestCommonDivisor(25, 15)) }
func GreatestCommonDivisor(a int,b int) int
Return a greatest common divisor of two integers a and b >>> GreatestCommonDivisor(3, 5) 1 >>> GreatestCommonDivisor(25, 15) 5
Write a Go function `func GreatestCommonDivisor(a int,b int) int` to solve the following problem: Return a greatest common divisor of two integers a and b >>> GreatestCommonDivisor(3, 5) 1 >>> GreatestCommonDivisor(25, 15) 5
go
func GreatestCommonDivisor(a int,b int) int{ if b < 2 { return b } var gcd int = 1 for i := 2; i < b; i++ { if a%i == 0 && b%i == 0 { gcd = i } } return gcd }
Go/14
// Return list of all prefixes from shortest to longest of the input string // >>> AllPrefixes('abc') // ['a', 'ab', 'abc'] func AllPrefixes(str string) []string{
func AllPrefixes(str string) []string{
prefixes := make([]string, 0, len(str)) for i := 0; i < len(str); i++ { prefixes = append(prefixes, str[:i+1]) } return prefixes }
prefixes := make([]string, 0, len(str)) for i := 0; i < len(str)-1; i++ { prefixes = append(prefixes, str[:i+1]) } return prefixes }
excess logic
incorrect output
AllPrefixes
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestAllPrefixes(t *testing.T) { assert := assert.New(t) assert.Equal([]string{}, AllPrefixes("")) assert.Equal([]string{"a", "as", "asd", "asdf", "asdfg", "asdfgh"}, AllPrefixes("asdfgh")) assert.Equal([]string{"W", "WW", "WWW"}, AllPrefixes("WWW")) }
func TestAllPrefixes(t *testing.T) { assert := assert.New(t) assert.Equal([]string{"a", "ab", "abc"}, AllPrefixes("abc")) }
func AllPrefixes(str string) []string
Return list of all prefixes from shortest to longest of the input string >>> AllPrefixes('abc') ['a', 'ab', 'abc']
Write a Go function `func AllPrefixes(str string) []string` to solve the following problem: Return list of all prefixes from shortest to longest of the input string >>> AllPrefixes('abc') ['a', 'ab', 'abc']
go
func AllPrefixes(str string) []string{ prefixes := make([]string, 0, len(str)) for i := 0; i < len(str); i++ { prefixes = append(prefixes, str[:i+1]) } return prefixes }
Go/18
// Find how many times a given substring can be found in the original string. Count overlaping cases. // >>> HowManyTimes('', 'a') // 0 // >>> HowManyTimes('aaa', 'a') // 3 // >>> HowManyTimes('aaaa', 'aa') // 3 func HowManyTimes(str string,substring string) int{
func HowManyTimes(str string,substring string) int{
times := 0 for i := 0; i < (len(str) - len(substring) + 1); i++ { if str[i:i+len(substring)] == substring { times += 1 } } return times }
times := 0 for i := 0; i < (len(str) - len(substring)); i++ { if str[i:i+len(substring)] == substring { times += 1 } } return times }
value misuse
incorrect output
HowManyTimes
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestHowManyTimes(t *testing.T) { assert := assert.New(t) assert.Equal(0, HowManyTimes("", "x")) assert.Equal(4, HowManyTimes("xyxyxyx", "x")) assert.Equal(4, HowManyTimes("cacacacac", "cac")) assert.Equal(1, HowManyTimes("john doe", "john")) }
func TestHowManyTimes(t *testing.T) { assert := assert.New(t) assert.Equal(0, HowManyTimes("", "a")) assert.Equal(3, HowManyTimes("aaa", "a")) assert.Equal(3, HowManyTimes("aaaa", "aa")) }
func HowManyTimes(str string,substring string) int
Find how many times a given substring can be found in the original string. Count overlaping cases. >>> HowManyTimes('', 'a') 0 >>> HowManyTimes('aaa', 'a') 3 >>> HowManyTimes('aaaa', 'aa') 3
Write a Go function `func HowManyTimes(str string,substring string) int` to solve the following problem: Find how many times a given substring can be found in the original string. Count overlaping cases. >>> HowManyTimes('', 'a') 0 >>> HowManyTimes('aaa', 'a') 3 >>> HowManyTimes('aaaa', 'aa') 3
go
func HowManyTimes(str string,substring string) int{ times := 0 for i := 0; i < (len(str) - len(substring) + 1); i++ { if str[i:i+len(substring)] == substring { times += 1 } } return times }
Go/19
import ( "sort" "strings" ) // 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 // >>> SortNumbers('three one five') // ...
func SortNumbers(numbers string) string{
valueMap := map[string]int{ "zero": 0, "one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9, } stringMap := make(map[int]string) for s, i := range valueMap { stringMap[i] = s } split := strings.Split(numbers, " ") temp := make([]int,...
valueMap := map[string]int{ "zero": 0, "one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9, } stringMap := make(map[int]string) for s, i := range valueMap { stringMap[i] = s } split := strings.Split(numbers, " ") temp := make([]int,...
missing logic
incorrect output
SortNumbers
import ( "sort" "strings" )
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestSortNumbers(t *testing.T) { assert := assert.New(t) assert.Equal("", SortNumbers("")) assert.Equal("three", SortNumbers("three")) assert.Equal("three five nine", SortNumbers("three five nine")) assert.Equal("zero four five seven eight nine", SortNumbers("five zero four seven nine eight")) ...
func TestSortNumbers(t *testing.T) { assert := assert.New(t) assert.Equal("one three five", SortNumbers("three one five")) }
func SortNumbers(numbers string) string
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 >>> SortNumbers('three one five') 'one three five'
Write a Go function `func SortNumbers(numbers string) string` to solve the following problem: 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 ...
go
func SortNumbers(numbers string) string{ valueMap := map[string]int{ "zero": 0, "one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9, } stringMap := make(map[int]string) for s, i := range valueMap { stringMap[i] = s } split := strings...
Go/22
// Filter given list of any values only for integers // >>> FilterIntegers(['a', 3.14, 5]) // [5] // >>> FilterIntegers([1, 2, 3, 'abc', {}, []]) // [1, 2, 3] func FilterIntegers(values []interface{}) []int {
func FilterIntegers(values []interface{}) []int {
result := make([]int, 0) for _, val := range values { switch i := val.(type) { case int: result = append(result, i) } } return result }
result := make([]int, 0) for _, val := range values { switch i := val.(type) { case int: values = append(values, i) } } return result }
variable misuse
incorrect output
FilterIntegers
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestFilterIntegers(t *testing.T) { assert := assert.New(t) assert.Equal([]int{}, FilterIntegers([]interface{}{})) assert.Equal([]int{4, 9}, FilterIntegers([]interface{}{4, nil, []interface{}{}, 23.2, 9, "adasd"})) assert.Equal([]int{3, 3, 3}, FilterIntegers([]interface{}{3, 'c', 3, 3, 'a', 'b'})) }...
func TestFilterIntegers(t *testing.T) { assert := assert.New(t) assert.Equal([]int{5}, FilterIntegers([]interface{}{'a', 3.14, 5})) assert.Equal([]int{1,2,3}, FilterIntegers([]interface{}{1,2,3,"abc", nil, []interface{}{}})) }
func FilterIntegers(values []interface{}) []int
Filter given list of any values only for integers >>> FilterIntegers(['a', 3.14, 5]) [5] >>> FilterIntegers([1, 2, 3, 'abc', {}, []]) [1, 2, 3]
Write a Go function `func FilterIntegers(values []interface{}) []int` to solve the following problem: Filter given list of any values only for integers >>> FilterIntegers(['a', 3.14, 5]) [5] >>> FilterIntegers([1, 2, 3, 'abc', {}, []]) [1, 2, 3]
go
func FilterIntegers(values []interface{}) []int { result := make([]int, 0) for _, val := range values { switch i := val.(type) { case int: result = append(result, i) } } return result }
Go/23
// Return length of given string // >>> Strlen('') // 0 // >>> Strlen('abc') // 3 func Strlen(str string) int {
func Strlen(str string) int {
return len(str) }
return len(str) - 1 }
value misuse
incorrect output
Strlen
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestStrlen(t *testing.T) { assert := assert.New(t) assert.Equal(0, Strlen("")) assert.Equal(1, Strlen("x")) assert.Equal(9, Strlen("asdasnakj")) }
func TestStrlen(t *testing.T) { assert := assert.New(t) assert.Equal(0, Strlen("")) assert.Equal(3, Strlen("abc")) }
func Strlen(str string) int
Return length of given string >>> Strlen('') 0 >>> Strlen('abc') 3
Write a Go function `func Strlen(str string) int` to solve the following problem: Return length of given string >>> Strlen('') 0 >>> Strlen('abc') 3
go
func Strlen(str string) int { return len(str) }
Go/24
// For a given number n, find the largest number that divides n evenly, smaller than n // >>> LargestDivisor(15) // 5 func LargestDivisor(n int) int {
func LargestDivisor(n int) int {
for i := n - 1; i > 0; i-- { if n % i == 0 { return i } } return 0 }
for i := n - 1; i > 0; i-- { if n - i == 0 { return i } } return 0 }
operator misuse
incorrect output
LargestDivisor
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestLargestDivisor(t *testing.T) { assert := assert.New(t) assert.Equal(1, LargestDivisor(3)) assert.Equal(1, LargestDivisor(7)) assert.Equal(5, LargestDivisor(10)) assert.Equal(50, LargestDivisor(100)) assert.Equal(7, LargestDivisor(49)) }
func TestLargestDivisor(t *testing.T) { assert := assert.New(t) assert.Equal(5, LargestDivisor(15)) }
func LargestDivisor(n int) int
For a given number n, find the largest number that divides n evenly, smaller than n >>> LargestDivisor(15) 5
Write a Go function `func LargestDivisor(n int) int` to solve the following problem: For a given number n, find the largest number that divides n evenly, smaller than n >>> LargestDivisor(15) 5
go
func LargestDivisor(n int) int { for i := n - 1; i > 0; i-- { if n % i == 0 { return i } } return 0 }
Go/25
import ( "math" ) // Return list of prime factors of given integer in the order from smallest to largest. // Each of the factors should be listed 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,...
func Factorize(n int) []int {
fact := make([]int, 0) for i := 2; i <= int(math.Sqrt(float64(n))+1); { if n%i == 0 { fact = append(fact, i) n = n / i } else { i++ } } if n > 1 { fact = append(fact, n) } return fact }
fact := make([]int, 0) for i := 0; i <= int(math.Sqrt(float64(n))+1); { if n%i == 0 { fact = append(fact, i) n = n / i } else { i++ } } if n > 1 { fact = append(fact, n) } return fact }
value misuse
incorrect output
Factorize
import ( "math" )
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestFactorize(t *testing.T) { assert := assert.New(t) assert.Equal([]int{2}, Factorize(2)) assert.Equal([]int{2, 2}, Factorize(4)) assert.Equal([]int{2, 2, 2}, Factorize(8)) assert.Equal([]int{3, 19}, Factorize(3 * 19)) assert.Equal([]int{3, 3, 19, 19}, Factorize(3 * 19 * 3 * 19)) asser...
func TestFactorize(t *testing.T) { assert := assert.New(t) assert.Equal([]int{2, 2, 2}, Factorize(8)) assert.Equal([]int{5,5}, Factorize(25)) assert.Equal([]int{2,5,7}, Factorize(70)) }
func Factorize(n int) []int
Return list of prime factors of given integer in the order from smallest to largest. Each of the factors should be listed 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] >>> Facto...
Write a Go function `func Factorize(n int) []int` to solve the following problem: Return list of prime factors of given integer in the order from smallest to largest. Each of the factors should be listed number of times corresponding to how many times it appeares in factorization. Input number should be equal to the pr...
go
func Factorize(n int) []int { fact := make([]int, 0) for i := 2; i <= int(math.Sqrt(float64(n))+1); { if n%i == 0 { fact = append(fact, i) n = n / i } else { i++ } } if n > 1 { fact = append(fact, n) } return fact }
Go/26
// From a list of integers, remove all elements that occur more than once. // Keep order of elements left the same as in the input. // >>> RemoveDuplicates([1, 2, 3, 2, 4]) // [1, 3, 4] func RemoveDuplicates(numbers []int) []int {
func RemoveDuplicates(numbers []int) []int {
c := make(map[int] int) for _, number := range numbers { if i, ok := c[number]; ok { c[number] = i + 1 } else { c[number] = 1 } } result := make([]int, 0) for _, number := range numbers { if c[number] <= 1 { result = append(result, number) } } return result }
c := make(map[int] int) for _, number := range numbers { if i, ok := c[number]; ok { c[number] = i + 1 } else { c[number] = 1 } } result := make([]int, 0) for _, number := range numbers { if c[number] < 1 { result = append(result, number) } } return result }
operator misuse
incorrect output
RemoveDuplicates
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestRemoveDuplicates(t *testing.T) { assert := assert.New(t) assert.Equal([]int{}, RemoveDuplicates([]int{})) assert.Equal([]int{1, 2, 3, 4}, RemoveDuplicates([]int{1, 2, 3,4})) assert.Equal([]int{1, 4, 5}, RemoveDuplicates([]int{1, 2, 3, 2,4, 3, 5})) }
func TestRemoveDuplicates(t *testing.T) { assert := assert.New(t) assert.Equal([]int{1, 3, 4}, RemoveDuplicates([]int{1,2, 3,2,4})) }
func RemoveDuplicates(numbers []int) []int
From a list of integers, remove all elements that occur more than once. Keep order of elements left the same as in the input. >>> RemoveDuplicates([1, 2, 3, 2, 4]) [1, 3, 4]
Write a Go function `func RemoveDuplicates(numbers []int) []int` to solve the following problem: From a list of integers, remove all elements that occur more than once. Keep order of elements left the same as in the input. >>> RemoveDuplicates([1, 2, 3, 2, 4]) [1, 3, 4]
go
func RemoveDuplicates(numbers []int) []int { c := make(map[int] int) for _, number := range numbers { if i, ok := c[number]; ok { c[number] = i + 1 } else { c[number] = 1 } } result := make([]int, 0) for _, number := range numbers { if c[number] <= 1 { result = append(result, number) } } re...
Go/27
import ( "strings" ) // For a given string, flip lowercase characters to uppercase and uppercase to lowercase. // >>> FlipCase('Hello') // 'hELLO' func FlipCase(str string) string {
func FlipCase(str string) string {
result := []rune{} for _, c := range str { if c >= 'A' && c <= 'Z' { result = append(result, 'a' + ((c - 'A' + 26) % 26)) } else if c >= 'a' && c <= 'z' { result = append(result, 'A' + ((c - 'a' + 26) % 26)) } else { result = append(result, c) ...
result := []rune{} for _, c := range str { if c >= 'a' && c <= 'z' { result = append(result, 'a' + ((c - 'A' + 26) % 26)) } else if c >= 'A' && c <= 'Z' { result = append(result, 'A' + ((c - 'a' + 26) % 26)) } else { result = append(result, c) ...
function misuse
incorrect output
FlipCase
import ( "strings" )
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestFlipCase(t *testing.T) { assert := assert.New(t) assert.Equal("", FlipCase("")) assert.Equal("hELLO!", FlipCase("Hello!")) assert.Equal("tHESE VIOLENT DELIGHTS HAVE VIOLENT ENDS",FlipCase("These violent delights have violent ends")) }
func TestFlipCase(t *testing.T) { assert := assert.New(t) assert.Equal("hELLO", FlipCase("Hello")) }
func FlipCase(str string) string
For a given string, flip lowercase characters to uppercase and uppercase to lowercase. >>> FlipCase('Hello') 'hELLO'
Write a Go function `func FlipCase(str string) string` to solve the following problem: For a given string, flip lowercase characters to uppercase and uppercase to lowercase. >>> FlipCase('Hello') 'hELLO'
go
func FlipCase(str string) string { result := []rune{} for _, c := range str { if c >= 'A' && c <= 'Z' { result = append(result, 'a' + ((c - 'A' + 26) % 26)) } else if c >= 'a' && c <= 'z' { result = append(result, 'A' + ((c - 'a' + 26) % 26)) } else { ...
Go/28
// Concatenate list of strings into a single string // >>> Concatenate([]) // '' // >>> Concatenate(['a', 'b', 'c']) // 'abc' func Concatenate(strings []string) string {
func Concatenate(strings []string) string {
if len(strings) == 0 { return "" } return strings[0] + Concatenate(strings[1:]) }
if len(strings) == 0 { return "" } return Concatenate(strings[1:]) }
excess logic
incorrect output
Concatenate
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestConcatenate(t *testing.T) { assert := assert.New(t) assert.Equal("", Concatenate([]string{})) assert.Equal("xyz", Concatenate([]string{"x", "y", "z"})) assert.Equal("xyzwk", Concatenate([]string{"x", "y","z", "w", "k"})) }
func TestConcatenate(t *testing.T) { assert := assert.New(t) assert.Equal("", Concatenate([]string{})) assert.Equal("abc", Concatenate([]string{"a", "b", "c"})) }
func Concatenate(strings []string) string
Concatenate list of strings into a single string >>> Concatenate([]) '' >>> Concatenate(['a', 'b', 'c']) 'abc'
Write a Go function `func Concatenate(strings []string) string` to solve the following problem: Concatenate list of strings into a single string >>> Concatenate([]) '' >>> Concatenate(['a', 'b', 'c']) 'abc'
go
func Concatenate(strings []string) string { if len(strings) == 0 { return "" } return strings[0] + Concatenate(strings[1:]) }
Go/30
// Return only positive numbers in the list. // >>> GetPositive([-1, 2, -4, 5, 6]) // [2, 5, 6] // >>> GetPositive([5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10]) // [5, 3, 2, 3, 9, 123, 1] func GetPositive(l []int) []int {
func GetPositive(l []int) []int {
res := make([]int, 0) for _, x := range l { if x > 0 { res = append(res, x) } } return res }
res := make([]int, 0) for _, x := range l { if x < 0 { res = append(res, x) } } return res }
operator misuse
incorrect output
GetPositive
package main import ( "testing" "github.com/stretchr/testify/assert" )
func TestGetPositive(t *testing.T) { assert := assert.New(t) assert.Equal([]int{4, 5, 6}, GetPositive([]int{-1, -2, 4,5, 6})) assert.Equal([]int{5, 3, 2, 3, 3, 9, 123, 1}, GetPositive([]int{5, 3, -5, 2, 3, 3, 9, 0, 123, 1, -10})) assert.Equal([]int{}, GetPositive([]int{-1, -2})) assert.Equal([]int{}...
func TestGetPositive(t *testing.T) { assert := assert.New(t) assert.Equal([]int{2, 5, 6}, GetPositive([]int{-1, 2, -4,5, 6})) assert.Equal([]int{5, 3, 2, 3, 9, 123, 1}, GetPositive([]int{5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10})) }
func GetPositive(l []int) []int
Return only positive numbers in the list. >>> GetPositive([-1, 2, -4, 5, 6]) [2, 5, 6] >>> GetPositive([5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10]) [5, 3, 2, 3, 9, 123, 1]
Write a Go function `func GetPositive(l []int) []int` to solve the following problem: Return only positive numbers in the list. >>> GetPositive([-1, 2, -4, 5, 6]) [2, 5, 6] >>> GetPositive([5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10]) [5, 3, 2, 3, 9, 123, 1]
go
func GetPositive(l []int) []int { res := make([]int, 0) for _, x := range l { if x > 0 { res = append(res, x) } } return res }
End of preview. Expand in Data Studio
README.md exists but content is empty.
Downloads last month
5