Dataset Viewer
Auto-converted to Parquet Duplicate
task_id
stringlengths
4
14
prompt
stringlengths
96
1.41k
declaration
stringlengths
10
791
canonical_solution
stringlengths
16
1.33k
buggy_solution
stringlengths
13
1.34k
bug_type
stringclasses
6 values
failure_symptoms
stringclasses
3 values
entry_point
stringlengths
1
30
import
stringclasses
19 values
test_setup
stringclasses
3 values
test
stringlengths
111
2.83k
example_test
stringlengths
0
1.39k
signature
stringlengths
4
80
docstring
stringlengths
23
1.21k
instruction
stringlengths
103
1.32k
language
stringclasses
6 values
reference
stringlengths
41
1.46k
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/1
/* Input to this function is a string containing multiple groups of nested parentheses. Your goal is to separate those group into separate strings and return the vector of those. Separate groups are balanced (each open brace is properly closed) and not nested within each other Ignore any spaces in the input string. >>>...
#include<stdio.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<string> separate_paren_groups(string paren_string){
vector<string> all_parens; string current_paren; int level=0; char chr; int i; for (i=0;i<paren_string.length();i++) { chr=paren_string[i]; if (chr=='(') { level+=1; current_paren+=chr; } if (chr==')') { level-=1; ...
vector<string> all_parens; string current_paren; int level=0; char chr; int i; for (i=0;i<paren_string.length();i++) { chr=paren_string[i]; if (chr=='(') { level+=1; current_paren+=chr; } if (chr==')') { level-=1; ...
operator misuse
incorrect output
separate_paren_groups
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(separate_paren_groups("(()()) ((())) () ((())()())"),{"(()())", "((...
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(separate_paren_groups("( ) (( )) (( )( ))") ,{"()", "(())", "(()())...
vector<string> separate_paren_groups(string paren_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 vector of those. Separate groups are balanced (each open brace is properly closed) and not nested within each other Ignore any spaces in the input string. >>> se...
Write a C++ function `vector<string> separate_paren_groups(string paren_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 vector of those. Separate groups are balanced (e...
cpp
#include<stdio.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<string> separate_paren_groups(string paren_string){ vector<string> all_parens; string current_paren; int level=0; char chr; int i; for (i=0;i<paren_string.lengt...
CPP/2
/* Given a positive floating point number, it can be decomposed into and integer part (largest integer smaller than given number) and decimals (leftover part always smaller than 1). Return the decimal part of the number. >>> truncate_number(3.5) 0.5 */ #include<stdio.h> #include<math.h> using namespace std; float trun...
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> float truncate_number(float number){
return number-int(number); }
return number-int(number)+1; }
excess logic
incorrect output
truncate_number
#undef NDEBUG #include<assert.h> int main(){ assert (truncate_number(3.5) == 0.5); assert (abs(truncate_number(1.33) - 0.33) < 1e-4); assert (abs(truncate_number(123.456) - 0.456) < 1e-4); }
#undef NDEBUG #include<assert.h> int main(){ assert (truncate_number(3.5) == 0.5); }
float truncate_number(float number)
Given a positive floating point number, it can be decomposed into and integer part (largest integer smaller than given number) and decimals (leftover part always smaller than 1). Return the decimal part of the number. >>> truncate_number(3.5) 0.5
Write a C++ function `float truncate_number(float number)` 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. >>> truncat...
cpp
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> float truncate_number(float number){ return number-int(number); }
CPP/3
/* You"re given a vector of deposit and withdrawal operations on a bank account that starts with zero balance. Your task is to detect if at any point the balance of account falls below zero, and at that point function should return true. Otherwise it should return false. >>> below_zero({1, 2, 3}) false >>> below_zero({...
#include<stdio.h> #include<vector> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> bool below_zero(vector<int> operations){
int num=0; for (int i=0;i<operations.size();i++) { num+=operations[i]; if (num<0) return true; } return false; }
int num=0; for (int i=0;i<operations.size();i++) { num+=operations[i]; if (num==0) return true; } return false; }
operator misuse
incorrect output
below_zero
#undef NDEBUG #include<assert.h> int main(){ assert (below_zero({}) == false); assert (below_zero({1, 2, -3, 1, 2, -3}) == false); assert (below_zero({1, 2, -4, 5, 6}) == true); assert (below_zero({1, -1, 2, -2, 5, -5, 4, -4}) == false); assert (below_zero({1, -1, 2, -2, 5, -5, 4, -5}) == true); ...
#undef NDEBUG #include<assert.h> int main(){ assert (below_zero({1, 2, 3}) == false); assert (below_zero({1, 2, -4, 5}) == true); }
bool below_zero(vector<int> operations)
You"re given a vector of deposit and withdrawal operations on a bank account that starts with zero balance. Your task is to detect if at any point the balance of account falls below zero, and at that point function should return true. Otherwise it should return false. >>> below_zero({1, 2, 3}) false >>> below_zero({1, ...
Write a C++ function `bool below_zero(vector<int> operations)` to solve the following problem: You"re given a vector of deposit and withdrawal operations on a bank account that starts with zero balance. Your task is to detect if at any point the balance of account falls below zero, and at that point function should ret...
cpp
#include<stdio.h> #include<vector> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> bool below_zero(vector<int> operations){ int num=0; for (int i=0;i<operations.size();i++) { num+=operations[i]; if (num<0) return true; } return false; }
CPP/4
/* For a given vector of input numbers, calculate Mean Absolute Deviation around the mean of this dataset. Mean Absolute Deviation is the average absolute difference between each element and a centerpoint (mean in this case): MAD = average | x - x_mean | >>> mean_absolute_deviation({1.0, 2.0, 3.0, 4.0}) 1.0 */ #include...
#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/6
/* Input to this function is a string represented multiple groups for nested parentheses separated by spaces. For each of the group, output the deepest level of nesting of parentheses. E.g. (()()) has maximum two levels of nesting while ((())) has three. >>> parse_nested_parens("(()()) ((())) () ((())()())") {2, 3, 1,...
#include<stdio.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<int> parse_nested_parens(string paren_string){
vector<int> all_levels; string current_paren; int level=0,max_level=0; char chr; int i; for (i=0;i<paren_string.length();i++) { chr=paren_string[i]; if (chr=='(') { level+=1; if (level>max_level) max_level=level; current_paren+=chr; } ...
vector<int> all_levels; string current_paren; int level=0,max_level=0; char chr; int i; for (i=0;i<paren_string.length();i++) { chr=paren_string[i]; if (chr=='(') { level+=1; if (level>max_level) max_level=level; current_paren+=chr; } ...
variable misuse
incorrect output
parse_nested_parens
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(parse_nested_parens("(()()) ((())) () ((())()())"),{2, 3, 1, 3})); ...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(parse_nested_parens("(()()) ((())) () ((())()())"),{2, 3, 1, 3})); }
vector<int> parse_nested_parens(string paren_string)
Input to this function is a string represented multiple groups for nested parentheses separated by spaces. For each of the group, output the deepest level of nesting of parentheses. E.g. (()()) has maximum two levels of nesting while ((())) has three. >>> parse_nested_parens("(()()) ((())) () ((())()())") {2, 3, 1, 3}
Write a C++ function `vector<int> parse_nested_parens(string paren_string)` 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 le...
cpp
#include<stdio.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<int> parse_nested_parens(string paren_string){ vector<int> all_levels; string current_paren; int level=0,max_level=0; char chr; int i; for (i=0;i<paren_string.l...
CPP/7
/* Filter an input vector of strings only for ones that contain given substring >>> filter_by_substring({}, "a") {} >>> filter_by_substring({"abc", "bacd", "cde", "vector"}, "a") {"abc", "bacd", "vector"} */ #include<stdio.h> #include<vector> #include<string> using namespace std; vector<string> filter_by_substring(vect...
#include<stdio.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<string> filter_by_substring(vector<string> strings, string substring){
vector<string> out; for (int i=0;i<strings.size();i++) { if (strings[i].find(substring)!=strings[i].npos) out.push_back(strings[i]); } return out; }
vector<string> out; for (int i=0;i<strings.size();i++) { if (substring.find(strings[i])!=strings[i].npos) out.push_back(strings[i]); } return out; }
variable misuse
incorrect output
filter_by_substring
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(filter_by_substring({}, "john"),{})); assert (issame(filter_by_s...
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(filter_by_substring({}, "a"),{})); assert (issame(filter_by_subs...
vector<string> filter_by_substring(vector<string> strings, string substring)
Filter an input vector of strings only for ones that contain given substring >>> filter_by_substring({}, "a") {} >>> filter_by_substring({"abc", "bacd", "cde", "vector"}, "a") {"abc", "bacd", "vector"}
Write a C++ function `vector<string> filter_by_substring(vector<string> strings, string substring)` to solve the following problem: Filter an input vector of strings only for ones that contain given substring >>> filter_by_substring({}, "a") {} >>> filter_by_substring({"abc", "bacd", "cde", "vector"}, "a") {"abc", "bac...
cpp
#include<stdio.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<string> filter_by_substring(vector<string> strings, string substring){ vector<string> out; for (int i=0;i<strings.size();i++) { if (strings[i].find(substring)!=stri...
CPP/8
/* For a given vector of integers, return a vector consisting of a sum and a product of all the integers in a vector. Empty sum should be equal to 0 and empty product should be equal to 1. >>> sum_product({}) (0, 1) >>> sum_product({1, 2, 3, 4}) (10, 24) */ #include<stdio.h> #include<vector> using namespace std; vector...
#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/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/16
/* Given a string, find out how many distinct characters (regardless of case) does it consist of >>> count_distinct_characters("xyzXYZ") 3 >>> count_distinct_characters("Jerry") 4 */ #include<stdio.h> #include<vector> #include<string> #include<algorithm> using namespace std; int count_distinct_characters(string str){
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> int count_distinct_characters(string str){
vector<char> distinct={}; transform(str.begin(),str.end(),str.begin(),::tolower); for (int i=0;i<str.size();i++) { bool isin=false; for (int j=0;j<distinct.size();j++) if (distinct[j]==str[i]) isin=true; if (isin==false) distinct.push_back(str[i]); ...
vector<char> distinct={}; for (int i=0;i<str.size();i++) { bool isin=false; for (int j=0;j<distinct.size();j++) if (distinct[j]==str[i]) isin=true; if (isin==false) distinct.push_back(str[i]); } return distinct.size(); }
missing logic
incorrect output
count_distinct_characters
#undef NDEBUG #include<assert.h> int main(){ assert (count_distinct_characters("") == 0); assert (count_distinct_characters("abcde") == 5); assert (count_distinct_characters("abcdecadeCADE") == 5); assert (count_distinct_characters("aaaaAAAAaaaa") == 1); assert (count_distinct_characters("Jerry jERR...
#undef NDEBUG #include<assert.h> int main(){ assert (count_distinct_characters("xyzXYZ") == 3); assert (count_distinct_characters("Jerry") == 4); }
int count_distinct_characters(string str)
Given a string, find out how many distinct characters (regardless of case) does it consist of >>> count_distinct_characters("xyzXYZ") 3 >>> count_distinct_characters("Jerry") 4
Write a C++ function `int count_distinct_characters(string str)` to solve the following problem: Given a string, find out how many distinct characters (regardless of case) does it consist of >>> count_distinct_characters("xyzXYZ") 3 >>> count_distinct_characters("Jerry") 4
cpp
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> int count_distinct_characters(string str){ vector<char> distinct={}; transform(str.begin(),str.end(),str.begin(),::tolower); for (int i=0;i<str.size();i++) { bool isi...
CPP/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/24
/* For a given number n, find the largest number that divides n evenly, smaller than n >>> largest_divisor(15) 5 */ #include<stdio.h> using namespace std; int largest_divisor(int n){
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> int largest_divisor(int n){
for (int i=2;i*i<=n;i++) if (n%i==0) return n/i; return 1; }
for (int i=2;i*i<=n;i++) if (n-i==0) return n/i; return 1; }
operator misuse
incorrect output
largest_divisor
#undef NDEBUG #include<assert.h> int main(){ assert (largest_divisor(3) == 1); assert (largest_divisor(7) == 1); assert (largest_divisor(10) == 5); assert (largest_divisor(100) == 50); assert (largest_divisor(49) == 7); }
#undef NDEBUG #include<assert.h> int main(){ assert (largest_divisor(15) == 5); }
int largest_divisor(int n)
For a given number n, find the largest number that divides n evenly, smaller than n >>> largest_divisor(15) 5
Write a C++ function `int largest_divisor(int n)` to solve the following problem: For a given number n, find the largest number that divides n evenly, smaller than n >>> largest_divisor(15) 5
cpp
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> int largest_divisor(int n){ for (int i=2;i*i<=n;i++) if (n%i==0) return n/i; return 1; }
CPP/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/33
/* This function takes a vector l and returns a vector l' such that l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal to the values of the corresponding indicies of l, but sorted. >>> sort_third({1, 2, 3}) {1, 2, 3} >>> sort_thir...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> sort_third(vector<int> l){
vector<int> third={}; int i; for (i=0;i*3<l.size();i++) third.push_back(l[i*3]); sort(third.begin(),third.end()); vector<int> out={}; for (i=0;i<l.size();i++) { if (i%3==0) {out.push_back(third[i/3]);} else out.push_back(l[i]); } return out; }
vector<int> third={}; int i; for (i=0;i*3<l.size();i++) third.push_back(l[i*3]); vector<int> out={}; for (i=0;i<l.size();i++) { if (i%3==0) {out.push_back(third[i/3]);} else out.push_back(l[i]); } return out; }
missing logic
incorrect output
sort_third
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(sort_third({1, 2, 3}) , sort_third({1, 2, 3}))); assert (issame(sor...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(sort_third({1, 2, 3}) , {1, 2, 3})); assert (issame(sort_third({5, ...
vector<int> sort_third(vector<int> l)
This function takes a vector l and returns a vector l' such that l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal to the values of the corresponding indicies of l, but sorted. >>> sort_third({1, 2, 3}) {1, 2, 3} >>> sort_third({...
Write a C++ function `vector<int> sort_third(vector<int> l)` to solve the following problem: This function takes a vector l and returns a vector l' such that l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal to the values of the ...
cpp
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> sort_third(vector<int> l){ vector<int> third={}; int i; for (i=0;i*3<l.size();i++) third.push_back(l[i*3]); sort(third.begin(),third.end()); vector<int> out={}; ...
CPP/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/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/46
/* The Fib4 number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: fib4(0) -> 0 fib4(1) -> 0 fib4(2) -> 2 fib4(3) -> 0 fib4(n) -> fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4). Please write a function to efficiently compute the n-th element of the fib4 number sequence. Do not use r...
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> int fib4(int n){
int f[100]; f[0]=0; f[1]=0; f[2]=2; f[3]=0; for (int i=4;i<=n;i++) { f[i]=f[i-1]+f[i-2]+f[i-3]+f[i-4]; } return f[n]; }
int f[100]; f[0]=0; f[1]=0; f[2]=2; f[3]=0; for (int i=4;i<=n;i++) { f[i]=f[i-1]+f[i-2]+f[i-3]+f[i-2]; } return f[n]; }
value misuse
incorrect output
fib4
#undef NDEBUG #include<assert.h> int main(){ assert (fib4(5) == 4); assert (fib4(8) == 28); assert (fib4(10) == 104); assert (fib4(12) == 386); }
#undef NDEBUG #include<assert.h> int main(){ assert (fib4(5) == 4); assert (fib4(6) == 8); assert (fib4(7) == 14); }
int fib4(int n)
The Fib4 number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: fib4(0) -> 0 fib4(1) -> 0 fib4(2) -> 2 fib4(3) -> 0 fib4(n) -> fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4). Please write a function to efficiently compute the n-th element of the fib4 number sequence. Do not use recu...
Write a C++ function `int fib4(int n)` to solve the following problem: The Fib4 number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: fib4(0) -> 0 fib4(1) -> 0 fib4(2) -> 2 fib4(3) -> 0 fib4(n) -> fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4). Please write a function to efficiently...
cpp
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> int fib4(int n){ int f[100]; f[0]=0; f[1]=0; f[2]=2; f[3]=0; for (int i=4;i<=n;i++) { f[i]=f[i-1]+f[i-2]+f[i-3]+f[i-4]; } return f[n]; }
CPP/47
/* Return median of elements in the vector l. >>> median({3, 1, 2, 4, 5}) 3 >>> median({-10, 4, 6, 1000, 10, 20}) 15.0 */ #include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; float median(vector<float> l){
#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/49
/* Return 2^n modulo p (be aware of numerics). >>> modp(3, 5) 3 >>> modp(1101, 101) 2 >>> modp(0, 101) 1 >>> modp(3, 11) 8 >>> modp(100, 101) 1 */ #include<stdio.h> using namespace std; int modp(int n,int p){
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> int modp(int n,int p){
int out=1; for (int i=0;i<n;i++) out=(out*2)%p; return out; }
int out=0; for (int i=0;i<n;i++) out=(out*2)%p; return out; }
value misuse
incorrect output
modp
#undef NDEBUG #include<assert.h> int main(){ assert (modp(3, 5) == 3); assert (modp(1101, 101) == 2); assert (modp(0, 101) == 1); assert (modp(3, 11) == 8); assert (modp(100, 101) == 1); assert (modp(30, 5) == 4); assert (modp(31, 5) == 3); }
#undef NDEBUG #include<assert.h> int main(){ assert (modp(3, 5) == 3); assert (modp(1101, 101) == 2); assert (modp(0, 101) == 1); assert (modp(3, 11) == 8); assert (modp(100, 101) == 1); }
int modp(int n,int p)
Return 2^n modulo p (be aware of numerics). >>> modp(3, 5) 3 >>> modp(1101, 101) 2 >>> modp(0, 101) 1 >>> modp(3, 11) 8 >>> modp(100, 101) 1
Write a C++ function `int modp(int n,int p)` to solve the following problem: Return 2^n modulo p (be aware of numerics). >>> modp(3, 5) 3 >>> modp(1101, 101) 2 >>> modp(0, 101) 1 >>> modp(3, 11) 8 >>> modp(100, 101) 1
cpp
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> int modp(int n,int p){ int out=1; for (int i=0;i<n;i++) out=(out*2)%p; return out; }
CPP/55
/* Return n-th Fibonacci number. >>> fib(10) 55 >>> fib(1) 1 >>> fib(8) 21 */ #include<stdio.h> using namespace std; int fib(int n){
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> int fib(int n){
int f[1000]; f[0]=0;f[1]=1; for (int i=2;i<=n; i++) f[i]=f[i-1]+f[i-2]; return f[n]; }
int f[1000]; f[0]=0;f[1]=1;f[2]=2; for (int i=3;i<=n; i++) f[i]=f[i-1]+f[i-2]; return f[n]; }
excess logic
incorrect output
fib
#undef NDEBUG #include<assert.h> int main(){ assert (fib(10) == 55); assert (fib(1) == 1); assert (fib(8) == 21); assert (fib(11) == 89); assert (fib(12) == 144); }
#undef NDEBUG #include<assert.h> int main(){ assert (fib(10) == 55); assert (fib(1) == 1); assert (fib(8) == 21); }
int fib(int n)
Return n-th Fibonacci number. >>> fib(10) 55 >>> fib(1) 1 >>> fib(8) 21
Write a C++ function `int fib(int n)` to solve the following problem: Return n-th Fibonacci number. >>> fib(10) 55 >>> fib(1) 1 >>> fib(8) 21
cpp
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> int fib(int n){ int f[1000]; f[0]=0;f[1]=1; for (int i=2;i<=n; i++) f[i]=f[i-1]+f[i-2]; return f[n]; }
CPP/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/62
/* xs represent coefficients of a polynomial. xs{0} + xs{1} * x + xs{2} * x^2 + .... Return derivative of this polynomial in the same form. >>> derivative({3, 1, 2, 4, 5}) {1, 4, 12, 20} >>> derivative({1, 2, 3}) {2, 6} */ #include<stdio.h> #include<math.h> #include<vector> using namespace std; vector<float> derivativ...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> vector<float> derivative(vector<float> xs){
vector<float> out={}; for (int i=1;i<xs.size();i++) out.push_back(i*xs[i]); return out; }
vector<float> out={}; for (int i=0;i<xs.size();i++) out.push_back(i*xs[i]); return out; }
value misuse
incorrect output
derivative
#undef NDEBUG #include<assert.h> bool issame(vector<float> a,vector<float>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (abs(a[i]-b[i])>1e-4) return false; } return true; } int main(){ assert (issame(derivative({3, 1, 2, 4, 5}) , {1, 4, 12, 20})); assert...
#undef NDEBUG #include<assert.h> bool issame(vector<float> a,vector<float>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (abs(a[i]-b[i])>1e-4) return false; } return true; } int main(){ assert (issame(derivative({3, 1, 2, 4, 5}) , {1, 4, 12, 20})); assert...
vector<float> derivative(vector<float> xs)
xs represent coefficients of a polynomial. xs{0} + xs{1} * x + xs{2} * x^2 + .... Return derivative of this polynomial in the same form. >>> derivative({3, 1, 2, 4, 5}) {1, 4, 12, 20} >>> derivative({1, 2, 3}) {2, 6}
Write a C++ function `vector<float> derivative(vector<float> xs)` to solve the following problem: xs represent coefficients of a polynomial. xs{0} + xs{1} * x + xs{2} * x^2 + .... Return derivative of this polynomial in the same form. >>> derivative({3, 1, 2, 4, 5}) {1, 4, 12, 20} >>> derivative({1, 2, 3}) {2, 6}
cpp
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> vector<float> derivative(vector<float> xs){ vector<float> out={}; for (int i=1;i<xs.size();i++) out.push_back(i*xs[i]); return out; }
CPP/65
/* Circular shift the digits of the integer x, shift the digits right by shift and return the result as a string. If shift > number of digits, return digits reversed. >>> circular_shift(12, 1) "21" >>> circular_shift(12, 2) "12" */ #include<stdio.h> #include<string> using namespace std; string circular_shift(int x,int ...
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> string circular_shift(int x,int shift){
string xs; xs=to_string(x); if (xs.length()<shift) { string s(xs.rbegin(),xs.rend()); return s; } xs=xs.substr(xs.length()-shift)+xs.substr(0,xs.length()-shift); return xs; }
string xs; xs=to_string(x); if (xs.length()<shift) { string s(xs.rbegin(),xs.rend()); return s; } xs=xs.substr(0,xs.length()-shift)+xs.substr(xs.length()-shift); return xs; }
variable misuse
incorrect output
circular_shift
#undef NDEBUG #include<assert.h> int main(){ assert (circular_shift(100, 2) == "001"); assert (circular_shift(12, 2) == "12"); assert (circular_shift(97, 8) == "79"); assert (circular_shift(12, 1) == "21"); assert (circular_shift(11, 101) == "11"); }
#undef NDEBUG #include<assert.h> int main(){ assert (circular_shift(12, 2) == "12"); assert (circular_shift(12, 1) == "21"); }
string circular_shift(int x,int shift)
Circular shift the digits of the integer x, shift the digits right by shift and return the result as a string. If shift > number of digits, return digits reversed. >>> circular_shift(12, 1) "21" >>> circular_shift(12, 2) "12"
Write a C++ function `string circular_shift(int x,int shift)` to solve the following problem: Circular shift the digits of the integer x, shift the digits right by shift and return the result as a string. If shift > number of digits, return digits reversed. >>> circular_shift(12, 1) "21" >>> circular_shift(12, 2) "12"
cpp
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> string circular_shift(int x,int shift){ string xs; xs=to_string(x); if (xs.length()<shift) { string s(xs.rbegin(),xs.rend()); return s; } xs=xs.substr(xs.length()-shift...
CPP/66
/* Task Write a function that takes a string as input and returns the sum of the upper characters only's ASCII codes. Examples: digitSum("") => 0 digitSum("abAB") => 131 digitSum("abcCd") => 67 digitSum("helloE") => 69 digitSum("woArBld") => 131 digitSum("aAaaaXa") => 153 */ #include<stdio.h> #...
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> int digitSum(string s){
int sum=0; for (int i=0;i<s.length();i++) if (s[i]>=65 and s[i]<=90) sum+=s[i]; return sum; }
int sum=0; for (int i=0;i<s.length();i++) if (s[i]>=65 and s[i]<=100) sum+=s[i]; return sum; }
function misuse
incorrect output
digitSum
#undef NDEBUG #include<assert.h> int main(){ assert (digitSum("") == 0); assert (digitSum("abAB") == 131); assert (digitSum("abcCd") == 67); assert (digitSum("helloE") == 69); assert (digitSum("woArBld") == 131); assert (digitSum("aAaaaXa") == 153); assert (digitSum(" How are yOu?") == 151);...
#undef NDEBUG #include<assert.h> int main(){ assert (digitSum("") == 0); assert (digitSum("abAB") == 131); assert (digitSum("abcCd") == 67); assert (digitSum("helloE") == 69); assert (digitSum("woArBld") == 131); assert (digitSum("aAaaaXa") == 153); }
int digitSum(string s)
Task Write a function that takes a string as input and returns the sum of the upper characters only's ASCII codes. Examples: digitSum("") => 0 digitSum("abAB") => 131 digitSum("abcCd") => 67 digitSum("helloE") => 69 digitSum("woArBld") => 131 digitSum("aAaaaXa") => 153
Write a C++ function `int digitSum(string s)` to solve the following problem: Task Write a function that takes a string as input and returns the sum of the upper characters only's ASCII codes. Examples: digitSum("") => 0 digitSum("abAB") => 131 digitSum("abcCd") => 67 digitSum("helloE") => 69 digitSum("woArBld") => 131...
cpp
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> int digitSum(string s){ int sum=0; for (int i=0;i<s.length();i++) if (s[i]>=65 and s[i]<=90) sum+=s[i]; return sum; }
CPP/70
/* Given vector of integers, return vector in strange order. Strange sorting, is when you start with the minimum value, then maximum of the remaining integers, then minimum and so on. Examples: strange_sort_vector({1, 2, 3, 4}) == {1, 4, 2, 3} strange_sort_vector({5, 5, 5, 5}) == {5, 5, 5, 5} strange_sort_vector({}) =...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> strange_sort_list(vector<int> lst){
vector<int> out={}; sort(lst.begin(),lst.end()); int l=0,r=lst.size()-1; while (l<r) { out.push_back(lst[l]); l+=1; out.push_back(lst[r]); r-=1; } if (l==r) out.push_back(lst[l]); return out; }
vector<int> out={}; sort(lst.begin(),lst.end()); int l=0,r=lst.size()-1; while (l<r) { out.push_back(lst[l]); l+=2; out.push_back(lst[r]); r-=2; } if (l==r) out.push_back(lst[l]); return out; }
operator misuse
incorrect output
strange_sort_list
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(strange_sort_list({1, 2, 3, 4}) , {1, 4, 2, 3})); assert (issame(st...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(strange_sort_list({1, 2, 3, 4}) , {1, 4, 2, 3})); assert (issame(st...
vector<int> strange_sort_list(vector<int> lst)
Given vector of integers, return vector in strange order. Strange sorting, is when you start with the minimum value, then maximum of the remaining integers, then minimum and so on. Examples: strange_sort_vector({1, 2, 3, 4}) == {1, 4, 2, 3} strange_sort_vector({5, 5, 5, 5}) == {5, 5, 5, 5} strange_sort_vector({}) == {}
Write a C++ function `vector<int> strange_sort_list(vector<int> lst)` to solve the following problem: Given vector of integers, return vector in strange order. Strange sorting, is when you start with the minimum value, then maximum of the remaining integers, then minimum and so on. Examples: strange_sort_vector({1, 2, ...
cpp
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> strange_sort_list(vector<int> lst){ vector<int> out={}; sort(lst.begin(),lst.end()); int l=0,r=lst.size()-1; while (l<r) { out.push_back(lst[l]); l+=1; ...
CPP/71
/* Given the lengths of the three sides of a triangle. Return the area of the triangle rounded to 2 decimal points if the three sides form a valid triangle. Otherwise return -1 Three sides make a valid triangle when the sum of any two sides is greater than the third side. Example: triangle_area(3, 4, 5) == 6.00 trian...
#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/72
/* Write a function that returns true if the object q will fly, and false otherwise. The object q will fly if it's balanced (it is a palindromic vector) and the sum of its elements is less than or equal the maximum possible weight w. Example: will_it_fly({1, 2}, 5) ➞ false // 1+2 is less than the maximum possible wei...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> bool will_it_fly(vector<int> q,int w){
int sum=0; for (int i=0;i<q.size();i++) { if (q[i]!=q[q.size()-1-i]) return false; sum+=q[i]; } if (sum>w) return false; return true; }
int sum=0; for (int i=0;i<q.size();i++) { if (q[i]==q[q.size()-1-i]) return false; sum+=q[i]; } if (sum>w) return false; return true; }
operator misuse
incorrect output
will_it_fly
#undef NDEBUG #include<assert.h> int main(){ assert (will_it_fly({3, 2, 3}, 9)==true); assert (will_it_fly({1, 2}, 5) == false); assert (will_it_fly({3}, 5) == true); assert (will_it_fly({3, 2, 3}, 1) == false); assert (will_it_fly({1, 2, 3}, 6) ==false); assert (will_it_fly({5}, 5) == true); }
#undef NDEBUG #include<assert.h> int main(){ assert (will_it_fly({3, 2, 3}, 9)==true); assert (will_it_fly({1, 2}, 5) == false); assert (will_it_fly({3}, 5) == true); assert (will_it_fly({3, 2, 3}, 1) == false); }
bool will_it_fly(vector<int> q,int w)
Write a function that returns true if the object q will fly, and false otherwise. The object q will fly if it's balanced (it is a palindromic vector) and the sum of its elements is less than or equal the maximum possible weight w. Example: will_it_fly({1, 2}, 5) ➞ false // 1+2 is less than the maximum possible weight, ...
Write a C++ function `bool will_it_fly(vector<int> q,int w)` to solve the following problem: Write a function that returns true if the object q will fly, and false otherwise. The object q will fly if it's balanced (it is a palindromic vector) and the sum of its elements is less than or equal the maximum possible weight...
cpp
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> bool will_it_fly(vector<int> q,int w){ int sum=0; for (int i=0;i<q.size();i++) { if (q[i]!=q[q.size()-1-i]) return false; sum+=q[i]; } if (sum>w) return false; return t...
CPP/75
/* Write a function that returns true if the given number is the multiplication of 3 prime numbers and false otherwise. Knowing that (a) is less then 100. Example: is_multiply_prime(30) == true 30 = 2 * 3 * 5 */ #include<stdio.h> using namespace std; bool is_multiply_prime(int a){
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> bool is_multiply_prime(int a){
int num=0; for (int i=2;i*i<=a;i++) while (a%i==0 and a>i) { a=a/i; num+=1; } if (num==2) return true; return false; }
int num=0; for (int i=0;i*i<=a;i++) while (a%i==0 and a>i) { a=a/i; num+=1; } if (num==2) return true; return false; }
value misuse
incorrect output
is_multiply_prime
#undef NDEBUG #include<assert.h> int main(){ assert (is_multiply_prime(5) == false); assert (is_multiply_prime(30) == true); assert (is_multiply_prime(8) == true); assert (is_multiply_prime(10) == false); assert (is_multiply_prime(125) == true); assert (is_multiply_prime(3 * 5 * 7) == true); ...
#undef NDEBUG #include<assert.h> int main(){ assert (is_multiply_prime(30) == true); }
bool is_multiply_prime(int a)
Write a function that returns true if the given number is the multiplication of 3 prime numbers and false otherwise. Knowing that (a) is less then 100. Example: is_multiply_prime(30) == true 30 = 2 * 3 * 5
Write a C++ function `bool is_multiply_prime(int a)` to solve the following problem: Write a function that returns true if the given number is the multiplication of 3 prime numbers and false otherwise. Knowing that (a) is less then 100. Example: is_multiply_prime(30) == true 30 = 2 * 3 * 5
cpp
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> bool is_multiply_prime(int a){ int num=0; for (int i=2;i*i<=a;i++) while (a%i==0 and a>i) { a=a/i; num+=1; } if (num==2) return true; return false; }
CPP/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/82
/* Write a function that takes a string and returns true if the string length is a prime number or false otherwise Examples prime_length("Hello") == true prime_length("abcdcba") == true prime_length("kittens") == true prime_length("orange") == false */ #include<stdio.h> #include<string> using namespace std; bool prime_...
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> bool prime_length(string str){
int l,i; l=str.length(); if (l<2) return false; for (i=2;i*i<=l;i++) if (l%i==0) return false; return true; }
int l,i; l=str.length(); if (l<2) return false; for (i=3;i*i<=l;i++) if (l%i==0) return false; return true; }
value misuse
incorrect output
prime_length
#undef NDEBUG #include<assert.h> int main(){ assert (prime_length("Hello") == true); assert (prime_length("abcdcba") == true); assert (prime_length("kittens") == true); assert (prime_length("orange") == false); assert (prime_length("wow") == true); assert (prime_length("world") == true); ass...
#undef NDEBUG #include<assert.h> int main(){ assert (prime_length("Hello") == true); assert (prime_length("abcdcba") == true); assert (prime_length("kittens") == true); assert (prime_length("orange") == false); }
bool prime_length(string str)
Write a function that takes a string and returns true if the string length is a prime number or false otherwise Examples prime_length("Hello") == true prime_length("abcdcba") == true prime_length("kittens") == true prime_length("orange") == false
Write a C++ function `bool prime_length(string str)` to solve the following problem: Write a function that takes a string and returns true if the string length is a prime number or false otherwise Examples prime_length("Hello") == true prime_length("abcdcba") == true prime_length("kittens") == true prime_length("orange...
cpp
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> bool prime_length(string str){ int l,i; l=str.length(); if (l<2) return false; for (i=2;i*i<=l;i++) if (l%i==0) return false; return true; }
CPP/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/89
/* Create a function encrypt that takes a string as an argument and returns a string encrypted with the alphabet being rotated. The alphabet should be rotated in a manner such that the letters shift down by two multiplied to two places. For example: encrypt("hi") returns "lm" encrypt("asdfghjkl") returns "ewhjklnop" ...
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> string encrypt(string s){
string out; int i; for (i=0;i<s.length();i++) { int w=((int)s[i]+4-(int)'a')%26+(int)'a'; out=out+(char)w; } return out; }
string out; int i; for (i=0;i<s.length();i++) { int w=((int)s[i]+4-(int)'a')%24+(int)'a'; out=out+(char)w; } return out; }
value misuse
incorrect output
encrypt
#undef NDEBUG #include<assert.h> int main(){ assert (encrypt("hi") == "lm"); assert (encrypt("asdfghjkl") == "ewhjklnop"); assert (encrypt("gf") == "kj"); assert (encrypt("et") == "ix"); assert (encrypt("faewfawefaewg")=="jeiajeaijeiak"); assert (encrypt("hellomyfriend")=="lippsqcjvmirh"); a...
#undef NDEBUG #include<assert.h> int main(){ assert (encrypt("hi") == "lm"); assert (encrypt("asdfghjkl") == "ewhjklnop"); assert (encrypt("gf") == "kj"); assert (encrypt("et") == "ix"); }
string encrypt(string s)
Create a function encrypt that takes a string as an argument and returns a string encrypted with the alphabet being rotated. The alphabet should be rotated in a manner such that the letters shift down by two multiplied to two places. For example: encrypt("hi") returns "lm" encrypt("asdfghjkl") returns "ewhjklnop" encry...
Write a C++ function `string encrypt(string s)` to solve the following problem: Create a function encrypt that takes a string as an argument and returns a string encrypted with the alphabet being rotated. The alphabet should be rotated in a manner such that the letters shift down by two multiplied to two places. For ex...
cpp
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> string encrypt(string s){ string out; int i; for (i=0;i<s.length();i++) { int w=((int)s[i]+4-(int)'a')%26+(int)'a'; out=out+(char)w; } return out; }
CPP/92
/* Create a function that takes 3 numbers. Returns true if one of the numbers is equal to the sum of the other two, and all numbers are integers. Returns false in any other cases. Examples any_int(5, 2, 7) ➞ true any_int(3, 2, 2) ➞ false any_int(3, -2, 1) ➞ true any_int(3.6, -2.2, 2) ➞ false */ #include<stdio.h>...
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> bool any_int(float a,float b,float c){
if (round(a)!=a) return false; if (round(b)!=b) return false; if (round(c)!=c) return false; if (a+b==c or a+c==b or b+c==a) return true; return false; }
if (round(a)!=a) return false; if (round(b)!=b) return false; if (round(c)!=c) return false; if (a+b==c or b+c==a) return true; return false; }
missing logic
incorrect output
any_int
#undef NDEBUG #include<assert.h> int main(){ assert (any_int(2, 3, 1)==true); assert (any_int(2.5, 2, 3)==false); assert (any_int(1.5, 5, 3.5)==false); assert (any_int(2, 6, 2)==false); assert (any_int(4, 2, 2)==true); assert (any_int(2.2, 2.2, 2.2)==false); assert (any_int(-4, 6, 2)==true);...
#undef NDEBUG #include<assert.h> int main(){ assert (any_int(5, 2, 7)==true); assert (any_int(3, 2, 2)==false); assert (any_int(3, -2, 1)==true); assert (any_int(3.6, -2.2, 2)==false); }
bool any_int(float a,float b,float c)
Create a function that takes 3 numbers. Returns true if one of the numbers is equal to the sum of the other two, and all numbers are integers. Returns false in any other cases. Examples any_int(5, 2, 7) ➞ true any_int(3, 2, 2) ➞ false any_int(3, -2, 1) ➞ true any_int(3.6, -2.2, 2) ➞ false
Write a C++ function `bool any_int(float a,float b,float c)` to solve the following problem: Create a function that takes 3 numbers. Returns true if one of the numbers is equal to the sum of the other two, and all numbers are integers. Returns false in any other cases. Examples any_int(5, 2, 7) ➞ true any_int(3, 2, 2) ...
cpp
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> bool any_int(float a,float b,float c){ if (round(a)!=a) return false; if (round(b)!=b) return false; if (round(c)!=c) return false; if (a+b==c or a+c==b or b+c==a) return true; return false; }
CPP/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/96
/* Implement a function that takes an non-negative integer and returns a vector of the first n integers that are prime numbers and less than n. for example: count_up_to(5) => {2,3} count_up_to(11) => {2,3,5,7} count_up_to(0) => {} count_up_to(20) => {2,3,5,7,11,13,17,19} count_up_to(1) => {} count_up_to(18) => {2,3,5,7...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> vector<int> count_up_to(int n){
vector<int> out={}; int i,j; for (i=2;i<n;i++) if (out.size()==0) {out.push_back(i);} else { bool isp=true; for (j=0;out[j]*out[j]<=i;j++) if (i%out[j]==0) isp=false; if (isp) out.push_back(i); } return out; }
vector<int> out={}; int i,j; for (i=2;i<n;i++) if (out.size()==0) {out.push_back(i);} else { bool isp=true; for (j=0;out[j]*out[j]<=i;j++) if (i%j==0) isp=false; if (isp) out.push_back(i); } return out; }
variable misuse
incorrect output
count_up_to
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(count_up_to(5) , {2,3})); assert (issame(count_up_to(6) , {2,3,5})); ...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(count_up_to(5) , {2,3})); assert (issame(count_up_to(11) , {2,3,5,7}));...
vector<int> count_up_to(int n)
Implement a function that takes an non-negative integer and returns a vector of the first n integers that are prime numbers and less than n. for example: count_up_to(5) => {2,3} count_up_to(11) => {2,3,5,7} count_up_to(0) => {} count_up_to(20) => {2,3,5,7,11,13,17,19} count_up_to(1) => {} count_up_to(18) => {2,3,5,7,11...
Write a C++ function `vector<int> count_up_to(int n)` to solve the following problem: Implement a function that takes an non-negative integer and returns a vector of the first n integers that are prime numbers and less than n. for example: count_up_to(5) => {2,3} count_up_to(11) => {2,3,5,7} count_up_to(0) => {} count_...
cpp
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> vector<int> count_up_to(int n){ vector<int> out={}; int i,j; for (i=2;i<n;i++) if (out.size()==0) {out.push_back(i);} else { bool isp=true; for (j=0...
CPP/99
/* Create a function that takes a value (string) representing a number and returns the closest integer to it. If the number is equidistant from two integers, round it away from zero. Examples >>> closest_integer("10") 10 >>> closest_integer("15.3") 15 Note: Rounding away from zero means that if the given number is eq...
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> int closest_integer(string value){
double w; w=atof(value.c_str()); return round(w); }
double w; w=atof(value.c_str()); return floor(w); }
function misuse
incorrect output
closest_integer
#undef NDEBUG #include<assert.h> int main(){ assert (closest_integer("10") == 10); assert (closest_integer("14.5") == 15); assert (closest_integer("-15.5") == -16); assert (closest_integer("15.3") == 15); assert (closest_integer("0") == 0); }
#undef NDEBUG #include<assert.h> int main(){ assert (closest_integer("10") == 10); assert (closest_integer("15.3") == 15); }
int closest_integer(string value)
Create a function that takes a value (string) representing a number and returns the closest integer to it. If the number is equidistant from two integers, round it away from zero. Examples >>> closest_integer("10") 10 >>> closest_integer("15.3") 15 Note: Rounding away from zero means that if the given number is equidis...
Write a C++ function `int closest_integer(string value)` to solve the following problem: Create a function that takes a value (string) representing a number and returns the closest integer to it. If the number is equidistant from two integers, round it away from zero. Examples >>> closest_integer("10") 10 >>> closest_i...
cpp
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> int closest_integer(string value){ double w; w=atof(value.c_str()); return round(w); }
CPP/101
/* You will be given a string of words separated by commas or spaces. Your task is to split the string into words and return a vector of the words. For example: words_string("Hi, my name is John") == {"Hi", "my", "name", "is", "John"} words_string("One, two, three, four, five, six") == {"One", 'two", 'three", "four", ...
#include<stdio.h> #include<math.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> vector<string> words_string(string s){
string current=""; vector<string> out={}; s=s+' '; for (int i=0;i<s.length();i++) if (s[i]==' ' or s[i]==',') { if (current.length()>0) { out.push_back(current); current=""; } } else current=current+s[i]; return out; }
string current=","; vector<string> out={}; s=s+' '; for (int i=0;i<s.length();i++) if (s[i]==' ' or s[i]==',') { if (current.length()>0) { out.push_back(current); current=","; } } else current=current+s[i]; return out; }
value misuse
incorrect output
words_string
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(words_string("Hi, my name is John") , {"Hi", "my", "name", "is", "Joh...
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(words_string("Hi, my name is John") , {"Hi", "my", "name", "is", "Joh...
vector<string> words_string(string s)
You will be given a string of words separated by commas or spaces. Your task is to split the string into words and return a vector of the words. For example: words_string("Hi, my name is John") == {"Hi", "my", "name", "is", "John"} words_string("One, two, three, four, five, six") == {"One", 'two", 'three", "four", "fiv...
Write a C++ function `vector<string> words_string(string s)` to solve the following problem: You will be given a string of words separated by commas or spaces. Your task is to split the string into words and return a vector of the words. For example: words_string("Hi, my name is John") == {"Hi", "my", "name", "is", "Jo...
cpp
#include<stdio.h> #include<math.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> vector<string> words_string(string s){ string current=""; vector<string> out={}; s=s+' '; for (int i=0;i<s.length();i++) if (s[i]==' ' or s[i]==',') { if (cu...
CPP/104
/* Given a vector of positive integers x. return a sorted vector of all elements that hasn't any even digit. Note: Returned vector should be sorted in increasing order. For example: >>> unique_digits({15, 33, 1422, 1}) {1, 15, 33} >>> unique_digits({152, 323, 1422, 10}) {} */ #include<stdio.h> #include<vector> #incl...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> unique_digits(vector<int> x){
vector<int> out={}; for (int i=0;i<x.size();i++) { int num=x[i]; bool u=true; if (num==0) u=false; while (num>0 and u) { if (num%2==0) u=false; num=num/10; } if (u) out.push_back(x[i]); ...
vector<int> out={}; for (int i=0;i<x.size();i++) { int num=x[i]; bool u=true; if (num==0) u=false; while (num>0 and u) { if (num%2==0) u=false; num=num/10; } if (u) out.push_back(x[i]); ...
excess logic
incorrect output
unique_digits
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(unique_digits({15, 33, 1422, 1}) , {1, 15, 33})); assert (issame(unique...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(unique_digits({15, 33, 1422, 1}) , {1, 15, 33})); assert (issame(unique...
vector<int> unique_digits(vector<int> x)
Given a vector of positive integers x. return a sorted vector of all elements that hasn't any even digit. Note: Returned vector should be sorted in increasing order. For example: >>> unique_digits({15, 33, 1422, 1}) {1, 15, 33} >>> unique_digits({152, 323, 1422, 10}) {}
Write a C++ function `vector<int> unique_digits(vector<int> x)` to solve the following problem: Given a vector of positive integers x. return a sorted vector of all elements that hasn't any even digit. Note: Returned vector should be sorted in increasing order. For example: >>> unique_digits({15, 33, 1422, 1}) {1, 15, ...
cpp
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> unique_digits(vector<int> x){ vector<int> out={}; for (int i=0;i<x.size();i++) { int num=x[i]; bool u=true; if (num==0) u=false; whi...
CPP/107
/* Given a positive integer n, return a vector that has the number of even and odd integer palindromes that fall within the range(1, n), inclusive. Example 1: Input: 3 Output: (1, 2) Explanation: Integer palindrome are 1, 2, 3. one of them is even, and two of them are odd. Example 2: Input: 12 ...
#include<stdio.h> #include<math.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> vector<int> even_odd_palindrome(int n){
int num1=0,num2=0; for (int i=1;i<=n;i++) { string w=to_string(i); string p(w.rbegin(),w.rend()); if (w==p and i%2==1) num1+=1; if (w==p and i%2==0) num2+=1; } return {num2,num1}; }
int num1=0,num2=0; for (int i=1;i<=n;i++) { string w=to_string(i); string p(w.rbegin(),w.rend()); if (w==p and i%2==1) num1+=1; if (w==p and i%2==0) num2+=2; } return {num2,num1}; }
value misuse
incorrect output
even_odd_palindrome
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(even_odd_palindrome(123) , {8, 13})); assert (issame(even_odd_palindrom...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(even_odd_palindrome(12) , {4, 6})); assert (issame(even_odd_palindrome(...
vector<int> even_odd_palindrome(int n)
Given a positive integer n, return a vector that has the number of even and odd integer palindromes that fall within the range(1, n), inclusive. Example 1: Input: 3 Output: (1, 2) Explanation: Integer palindrome are 1, 2, 3. one of them is even, and two of them are odd. Example 2: Input: 12 Output: (4, 6) Explanation: ...
Write a C++ function `vector<int> even_odd_palindrome(int n)` to solve the following problem: Given a positive integer n, return a vector that has the number of even and odd integer palindromes that fall within the range(1, n), inclusive. Example 1: Input: 3 Output: (1, 2) Explanation: Integer palindrome are 1, 2, 3. o...
cpp
#include<stdio.h> #include<math.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> vector<int> even_odd_palindrome(int n){ int num1=0,num2=0; for (int i=1;i<=n;i++) { string w=to_string(i); string p(w.rbegin(),w.rend()); if (w==p and i%2=...
CPP/110
/* In this problem, you will implement a function that takes two vectors of numbers, and determines whether it is possible to perform an exchange of elements between them to make lst1 a vector of only even numbers. There is no limit on the number of exchanged elements between lst1 and lst2. If it is possible to exchang...
#include<stdio.h> #include<math.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> string exchange(vector<int> lst1,vector<int> lst2){
int num=0; for (int i=0;i<lst1.size();i++) if (lst1[i]%2==0) num+=1; for (int i=0;i<lst2.size();i++) if (lst2[i]%2==0) num+=1; if (num>=lst1.size()) return "YES"; return "NO"; }
int num=0; for (int i=0;i<lst1.size();i++) if (lst1[i]%2==0) num+=1; for (int i=0;i<lst2.size();i++) if (lst2[i]%2==0) num+=1; if (num<lst1.size()) return "YES"; return "NO"; }
variable misuse
incorrect output
exchange
#undef NDEBUG #include<assert.h> int main(){ assert (exchange({1, 2, 3, 4}, {1, 2, 3, 4}) == "YES"); assert (exchange({1, 2, 3, 4}, {1, 5, 3, 4}) == "NO"); assert (exchange({1, 2, 3, 4}, {2, 1, 4, 3}) == "YES" ); assert (exchange({5, 7, 3}, {2, 6, 4}) == "YES"); assert (exchange({5, 7, 3}, {2, 6, 3}...
#undef NDEBUG #include<assert.h> int main(){ assert (exchange({1, 2, 3, 4}, {1, 2, 3, 4}) == "YES"); assert (exchange({1, 2, 3, 4}, {1, 5, 3, 4}) == "NO"); }
string exchange(vector<int> lst1,vector<int> lst2)
In this problem, you will implement a function that takes two vectors of numbers, and determines whether it is possible to perform an exchange of elements between them to make lst1 a vector of only even numbers. There is no limit on the number of exchanged elements between lst1 and lst2. If it is possible to exchange e...
Write a C++ function `string exchange(vector<int> lst1,vector<int> lst2)` to solve the following problem: In this problem, you will implement a function that takes two vectors of numbers, and determines whether it is possible to perform an exchange of elements between them to make lst1 a vector of only even numbers. Th...
cpp
#include<stdio.h> #include<math.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> string exchange(vector<int> lst1,vector<int> lst2){ int num=0; for (int i=0;i<lst1.size();i++) if (lst1[i]%2==0) num+=1; for (int i=0;i<lst2.size();i++) if (lst2[i]%2==0) ...
CPP/114
/* Given a vector of integers nums, find the minimum sum of any non-empty sub-vector of nums. Example minSubArraySum({2, 3, 4, 1, 2, 4}) == 1 minSubArraySum({-1, -2, -3}) == -6 */ #include<stdio.h> #include<vector> using namespace std; long long minSubArraySum(vector<long long> nums){
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> long long minSubArraySum(vector<long long> nums){
long long current,min; current=nums[0]; min=nums[0]; for (int i=1;i<nums.size();i++) { if (current<0) current=current+nums[i]; else current=nums[i]; if (current<min) min=current; } return min; }
long long current,min; current=nums[0]; min=nums[0]; for (int i=1;i<nums.size();i++) { if (current<0) current=current+nums.size(); else current=nums[i]; if (current<min) min=current; } return min; }
function misuse
incorrect output
minSubArraySum
#undef NDEBUG #include<assert.h> int main(){ assert (minSubArraySum({2, 3, 4, 1, 2, 4}) == 1); assert (minSubArraySum({-1, -2, -3}) == -6); assert (minSubArraySum({-1, -2, -3, 2, -10}) == -14); assert (minSubArraySum({-9999999999999999}) == -9999999999999999); assert (minSubArraySum({0, 10, 20, 1000...
#undef NDEBUG #include<assert.h> int main(){ assert (minSubArraySum({2, 3, 4, 1, 2, 4}) == 1); assert (minSubArraySum({-1, -2, -3}) == -6); }
long long minSubArraySum(vector<long long> nums)
Given a vector of integers nums, find the minimum sum of any non-empty sub-vector of nums. Example minSubArraySum({2, 3, 4, 1, 2, 4}) == 1 minSubArraySum({-1, -2, -3}) == -6
Write a C++ function `long long minSubArraySum(vector<long long> nums)` to solve the following problem: Given a vector of integers nums, find the minimum sum of any non-empty sub-vector of nums. Example minSubArraySum({2, 3, 4, 1, 2, 4}) == 1 minSubArraySum({-1, -2, -3}) == -6
cpp
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> long long minSubArraySum(vector<long long> nums){ long long current,min; current=nums[0]; min=nums[0]; for (int i=1;i<nums.size();i++) { if (current<0) current=current+nums[i]; ...
CPP/115
/* You are given a rectangular grid of wells. Each row represents a single well, and each 1 in a row represents a single unit of water. Each well has a corresponding bucket that can be used to extract water from it, and all buckets have the same capacity. Your task is to use the buckets to empty the wells. Output the ...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> int max_fill(vector<vector<int>> grid,int capacity){
int out=0; for (int i=0;i<grid.size();i++) { int sum=0; for (int j=0;j<grid[i].size();j++) sum+=grid[i][j]; if (sum>0) out+=(sum-1)/capacity+1; } return out; }
int out=0; for (int i=0;i<grid.size();i++) { int sum=0; for (int j=0;j<grid[i].size();j++) sum+=grid[i][j]; if (sum>0) out+=sum/capacity+1; } return out; }
function misuse
incorrect output
max_fill
#undef NDEBUG #include<assert.h> int main(){ assert (max_fill({{0,0,1,0}, {0,1,0,0}, {1,1,1,1}}, 1) == 6); assert (max_fill({{0,0,1,1}, {0,0,0,0}, {1,1,1,1}, {0,1,1,1}}, 2) == 5); assert (max_fill({{0,0,0}, {0,0,0}}, 5) == 0); assert (max_fill({{1,1,1,1}, {1,1,1,1}}, 2) == 4); assert (max_fill({{1,1...
#undef NDEBUG #include<assert.h> int main(){ assert (max_fill({{0,0,1,0}, {0,1,0,0}, {1,1,1,1}}, 1) == 6); assert (max_fill({{0,0,1,1}, {0,0,0,0}, {1,1,1,1}, {0,1,1,1}}, 2) == 5); assert (max_fill({{0,0,0}, {0,0,0}}, 5) == 0); }
int max_fill(vector<vector<int>> grid,int capacity)
You are given a rectangular grid of wells. Each row represents a single well, and each 1 in a row represents a single unit of water. Each well has a corresponding bucket that can be used to extract water from it, and all buckets have the same capacity. Your task is to use the buckets to empty the wells. Output the numb...
Write a C++ function `int max_fill(vector<vector<int>> grid,int capacity)` to solve the following problem: You are given a rectangular grid of wells. Each row represents a single well, and each 1 in a row represents a single unit of water. Each well has a corresponding bucket that can be used to extract water from it, ...
cpp
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> int max_fill(vector<vector<int>> grid,int capacity){ int out=0; for (int i=0;i<grid.size();i++) { int sum=0; for (int j=0;j<grid[i].size();j++) sum+=grid[i][j]; ...
CPP/116
/* In this Kata, you have to sort a vector of non-negative integers according to number of ones in their binary representation in ascending order. For similar number of ones, sort based on decimal value. It must be implemented like this: >>> sort_vector({1, 5, 2, 3, 4}) == {1, 2, 3, 4, 5} >>> sort_vector({-2, -3, -4, ...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> sort_array(vector<int> arr){
vector<int> bin={}; int m; for (int i=0;i<arr.size();i++) { int b=0,n=abs(arr[i]); while (n>0) { b+=n%2;n=n/2; } bin.push_back(b); } for (int i=0;i<arr.size();i++) for (int j=1;j<arr.size();j++) if (bin[j]<bin[j-1] or (bin[j]==bin[j-1]...
vector<int> bin={}; int m; for (int i=0;i<arr.size();i++) { int b=0,n=abs(arr[i]); while (n>0) { b+=n%2;n=n/2; } bin.push_back(b); } for (int i=0;i<arr.size();i++) for (int j=1;j<arr.size();j++) if (bin[j]<bin[j-1] or (bin[j]==bin[j-1]...
variable 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({1,5,2,3,4}) , {1, 2, 4, 3, 5})); assert (issame(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({1,5,2,3,4}) , {1, 2, 4, 3, 5})); assert (issame(sort_array(...
vector<int> sort_array(vector<int> arr)
In this Kata, you have to sort a vector of non-negative integers according to number of ones in their binary representation in ascending order. For similar number of ones, sort based on decimal value. It must be implemented like this: >>> sort_vector({1, 5, 2, 3, 4}) == {1, 2, 3, 4, 5} >>> sort_vector({-2, -3, -4, -5, ...
Write a C++ function `vector<int> sort_array(vector<int> arr)` to solve the following problem: In this Kata, you have to sort a vector of non-negative integers according to number of ones in their binary representation in ascending order. For similar number of ones, sort based on decimal value. It must be implemented l...
cpp
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> sort_array(vector<int> arr){ vector<int> bin={}; int m; for (int i=0;i<arr.size();i++) { int b=0,n=abs(arr[i]); while (n>0) { b+=n%2;n=n/2; ...
CPP/120
/* Given a vector arr of integers and a positive integer k, return a sorted vector of length k with the maximum k numbers in arr. Example 1: Input: arr = {-3, -4, 5}, k = 3 Output: {-4, -3, 5} Example 2: Input: arr = {4, -4, 4}, k = 2 Output: {4, 4} Example 3: Input: arr = {-3, 2, 1, 2, -1, -...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> maximum(vector<int> arr,int k){
sort(arr.begin(),arr.end()); vector<int> out(arr.end()-k,arr.end()); return out; }
sort(arr.begin(),arr.end()); vector<int> out(arr.end()-k,arr.end()); sort(out.end(),out.begin()); return out; }
excess logic
incorrect output
maximum
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(maximum({-3, -4, 5}, 3) , {-4, -3, 5})); assert (issame(maximum({4, -4,...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(maximum({-3, -4, 5}, 3) , {-4, -3, 5})); assert (issame(maximum({4, -4,...
vector<int> maximum(vector<int> arr,int k)
Given a vector arr of integers and a positive integer k, return a sorted vector of length k with the maximum k numbers in arr. Example 1: Input: arr = {-3, -4, 5}, k = 3 Output: {-4, -3, 5} Example 2: Input: arr = {4, -4, 4}, k = 2 Output: {4, 4} Example 3: Input: arr = {-3, 2, 1, 2, -1, -2, 1}, k = 1 Output: {2} Note:...
Write a C++ function `vector<int> maximum(vector<int> arr,int k)` to solve the following problem: Given a vector arr of integers and a positive integer k, return a sorted vector of length k with the maximum k numbers in arr. Example 1: Input: arr = {-3, -4, 5}, k = 3 Output: {-4, -3, 5} Example 2: Input: arr = {4, -4, ...
cpp
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> maximum(vector<int> arr,int k){ sort(arr.begin(),arr.end()); vector<int> out(arr.end()-k,arr.end()); return out; }
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/129
/* Given a grid with N rows and N columns (N >= 2) and a positive integer k, each cell of the grid contains a value. Every integer in the range {1, N * N} inclusive appears exactly once on the cells of the grid. You have to find the minimum path of length k in the grid. You can start from any cell, and in each step y...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> minPath(vector<vector<int>> grid, int k){
int i,j,x,y,min; for (i=0;i<grid.size();i++) for (j=0;j<grid[i].size();j++) if (grid[i][j]==1) { x=i;y=j; } min=grid.size()*grid.size(); if (x>0 and grid[x-1][y]<min) min=grid[x-1][y]; if (x<grid.size()-1 and grid[x+1][y]<min) min=grid[x+1][y]; if ...
int i,j,x,y,min; for (i=0;i<grid.size();i++) for (j=0;j<grid[i].size();j++) if (grid[i][j]==1) { x=i;y=j; } min=grid.size()*grid.size(); if (x>0 and grid[x-1][y]<min) min=grid[x-1][y]; if (x<grid.size()-1 and grid[x+1][y]<min) min=grid[x][y]; if (y...
value misuse
incorrect output
minPath
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(minPath({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, 3) , {1, 2, 1})); assert (is...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(minPath({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, 3) , {1, 2, 1})); assert (is...
vector<int> minPath(vector<vector<int>> grid, int k)
Given a grid with N rows and N columns (N >= 2) and a positive integer k, each cell of the grid contains a value. Every integer in the range {1, N * N} inclusive appears exactly once on the cells of the grid. You have to find the minimum path of length k in the grid. You can start from any cell, and in each step you ca...
Write a C++ function `vector<int> minPath(vector<vector<int>> grid, int k)` to solve the following problem: Given a grid with N rows and N columns (N >= 2) and a positive integer k, each cell of the grid contains a value. Every integer in the range {1, N * N} inclusive appears exactly once on the cells of the grid. You...
cpp
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> minPath(vector<vector<int>> grid, int k){ int i,j,x,y,min; for (i=0;i<grid.size();i++) for (j=0;j<grid[i].size();j++) if (grid[i][j]==1) { x=i;y=j; ...
CPP/131
/* Given a positive integer n, return the product of the odd digits. Return 0 if all digits are even. For example: digits(1) == 1 digits(4) == 0 digits(235) == 15 */ #include<stdio.h> #include<string> using namespace std; int digits(int n){
#include<stdio.h> #include<math.h> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> int digits(int n){
int prod=1,has=0; string s=to_string(n); for (int i=0;i<s.length();i++) if (s[i]%2==1) { has=1; prod=prod*(s[i]-48); } if (has==0) return 0; return prod; }
int prod=1,has=0; string s=to_string(n); for (int i=0;i<s.length();i++) if (s[i]%2==1) { has=1; prod=48*prod*(s[i]-48); } if (has==0) return 0; return prod; }
excess logic
incorrect output
digits
#undef NDEBUG #include<assert.h> int main(){ assert (digits(5) == 5); assert (digits(54) == 5); assert (digits(120) ==1); assert (digits(5014) == 5); assert (digits(98765) == 315); assert (digits(5576543) == 2625); assert (digits(2468) == 0); }
#undef NDEBUG #include<assert.h> int main(){ assert (digits(1) == 1); assert (digits(4) == 0); assert (digits(235) ==15); }
int digits(int n)
Given a positive integer n, return the product of the odd digits. Return 0 if all digits are even. For example: digits(1) == 1 digits(4) == 0 digits(235) == 15
Write a C++ function `int digits(int n)` to solve the following problem: Given a positive integer n, return the product of the odd digits. Return 0 if all digits are even. For example: digits(1) == 1 digits(4) == 0 digits(235) == 15
cpp
#include<stdio.h> #include<math.h> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> int digits(int n){ int prod=1,has=0; string s=to_string(n); for (int i=0;i<s.length();i++) if (s[i]%2==1) { has=1; prod=prod*(s[i]-48); } if (h...
End of preview. Expand in Data Studio
README.md exists but content is empty.
Downloads last month
5