task_id
stringlengths
1
30
buggy_code
stringlengths
162
2.31k
fixed_code
stringlengths
162
2.31k
prompt
stringlengths
134
1.35k
unit_tests
stringlengths
317
8.03M
is_palindrome
/* Checks if given string is a palindrome >>> is_palindrome("") true >>> is_palindrome("aba") true >>> is_palindrome("aaaaa") true >>> is_palindrome("zbcd") false */ #include<stdio.h> #include<string> using namespace std; bool is_palindrome(string text){ string pr(text.rbegin(),text.rend()-1); return pr==text; ...
/* Checks if given string is a palindrome >>> is_palindrome("") true >>> is_palindrome("aba") true >>> is_palindrome("aaaaa") true >>> is_palindrome("zbcd") false */ #include<stdio.h> #include<string> using namespace std; bool is_palindrome(string text){ string pr(text.rbegin(),text.rend()); return pr==text; } ...
/* 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){
#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 ...
car_race_collision
/* Imagine a road that's a perfectly straight infinitely long line. n cars are driving left to right; simultaneously, a different set of n cars are driving right to left. The two sets of cars start out being very far from each other. All cars move in the same speed. Two cars are said to collide when a car that's m...
/* Imagine a road that's a perfectly straight infinitely long line. n cars are driving left to right; simultaneously, a different set of n cars are driving right to left. The two sets of cars start out being very far from each other. All cars move in the same speed. Two cars are said to collide when a car that's m...
/* Imagine a road that's a perfectly straight infinitely long line. n cars are driving left to right; simultaneously, a different set of n cars are driving right to left. The two sets of cars start out being very far from each other. All cars move in the same speed. Two cars are said to collide when a car that's m...
#undef NDEBUG #include<assert.h> int main(){ assert (car_race_collision(2) == 4); assert (car_race_collision(3) == 9); assert (car_race_collision(4) == 16); assert (car_race_collision(8) == 64); assert (car_race_collision(10) == 100); assert (car_race_collision(2) == 4); assert (car_race_col...
solution
/* 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){ int sum=...
/* 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){ int sum=...
/* 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){
#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...
starts_one_ends
/* Given a positive integer n, return the count of the numbers of n-digit positive integers that start or end with 1. */ #include<stdio.h> using namespace std; int starts_one_ends(int n){ if (n<1) return 0; if (n==1) return 1; int out=19; for (int i=2;i<n;i++) out=out*10; return out; }
/* Given a positive integer n, return the count of the numbers of n-digit positive integers that start or end with 1. */ #include<stdio.h> using namespace std; int starts_one_ends(int n){ if (n<1) return 0; if (n==1) return 1; int out=18; for (int i=2;i<n;i++) out=out*10; return out; }
/* Given a positive integer n, return the count of the numbers of n-digit positive integers that start or end with 1. */ #include<stdio.h> using namespace std; int starts_one_ends(int n){
#undef NDEBUG #include<assert.h> int main(){ assert (starts_one_ends(1) == 1); assert (starts_one_ends(2) == 18); assert (starts_one_ends(3) == 180); assert (starts_one_ends(4) == 1800); assert (starts_one_ends(5) == 18000); assert(starts_one_ends(1) == 1); assert(starts_one_ends(2) == 18); ...
is_prime
/* Return true if a given number is prime, and false otherwise. >>> is_prime(6) false >>> is_prime(101) true >>> is_prime(11) true >>> is_prime(13441) true >>> is_prime(61) true >>> is_prime(4) false >>> is_prime(1) false */ #include<stdio.h> using namespace std; bool is_prime(long long n){ if (n<2) return false; ...
/* Return true if a given number is prime, and false otherwise. >>> is_prime(6) false >>> is_prime(101) true >>> is_prime(11) true >>> is_prime(13441) true >>> is_prime(61) true >>> is_prime(4) false >>> is_prime(1) false */ #include<stdio.h> using namespace std; bool is_prime(long long n){ if (n<2) return false; ...
/* 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){
#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) ==...
digits
/* 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){ int prod=1,has=0; string s=to_string(n); for (int i=0;i<s.length(...
/* 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){ int prod=1,has=0; string s=to_string(n); for (int i=0;i<s.length(...
/* 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){
#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); assert (digits(5) == 5); assert (digits(5...
flip_case
/* 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){ string out=""; for (int i=0;i<str.length();i++) { char w=str[i]; if (w>=97 and w<=...
/* 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){ string out=""; for (int i=0;i<str.length();i++) { char w=str[i]; if (w>=97 and w<=...
/* 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){
#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"); assert (flip_case("") == ""); assert (flip_case("Hello!") == "hELLO!"); ...
largest_smallest_integers
/* Create a function that returns a vector (a, b), where "a" is the largest of negative integers, and "b" is the smallest of positive integers in a vector. If there is no negative or positive integers, return them as 0. Examples: largest_smallest_integers({2, 4, 1, 3, 5, 7}) == {0, 1} largest_smallest_integers({}) == ...
/* Create a function that returns a vector (a, b), where "a" is the largest of negative integers, and "b" is the smallest of positive integers in a vector. If there is no negative or positive integers, return them as 0. Examples: largest_smallest_integers({2, 4, 1, 3, 5, 7}) == {0, 1} largest_smallest_integers({}) == ...
/* Create a function that returns a vector (a, b), where "a" is the largest of negative integers, and "b" is the smallest of positive integers in a vector. If there is no negative or positive integers, return them as 0. Examples: largest_smallest_integers({2, 4, 1, 3, 5, 7}) == {0, 1} largest_smallest_integers({}) == ...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(largest_smallest_integers({2, 4, 1, 3, 5, 7}) , {0, 1})); assert (issam...
file_name_check
/* Create a function which takes a string representing a file's name, and returns "Yes" if the the file's name is valid, and returns "No" otherwise. A file's name is considered to be valid if and only if all the following conditions are met: - There should not be more than three digits ('0'-'9') in the file's name. - ...
/* Create a function which takes a string representing a file's name, and returns "Yes" if the the file's name is valid, and returns "No" otherwise. A file's name is considered to be valid if and only if all the following conditions are met: - There should not be more than three digits ('0'-'9') in the file's name. - ...
/* Create a function which takes a string representing a file's name, and returns "Yes" if the the file's name is valid, and returns "No" otherwise. A file's name is considered to be valid if and only if all the following conditions are met: - There should not be more than three digits ('0'-'9') in the file's name. - ...
#undef NDEBUG #include<assert.h> int main(){ assert (file_name_check("example.txt") == "Yes"); assert (file_name_check("1example.dll") == "No"); assert (file_name_check("s1sdf3.asd") == "No"); assert (file_name_check("K.dll") == "Yes"); assert (file_name_check("MY16FILE3.exe") == "Yes"); assert ...
sort_array
/* 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, ...
/* 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, ...
/* 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, ...
#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(...
is_sorted
/* Given a vector of numbers, return whether or not they are sorted in ascending order. If vector has more than 1 duplicate of the same number, return false. Assume no negative numbers and only integers. Examples is_sorted({5}) ➞ true is_sorted({1, 2, 3, 4, 5}) ➞ true is_sorted({1, 3, 2, 4, 5}) ➞ false is_sorted({1, 2...
/* Given a vector of numbers, return whether or not they are sorted in ascending order. If vector has more than 1 duplicate of the same number, return false. Assume no negative numbers and only integers. Examples is_sorted({5}) ➞ true is_sorted({1, 2, 3, 4, 5}) ➞ true is_sorted({1, 3, 2, 4, 5}) ➞ false is_sorted({1, 2...
/* Given a vector of numbers, return whether or not they are sorted in ascending order. If vector has more than 1 duplicate of the same number, return false. Assume no negative numbers and only integers. Examples is_sorted({5}) ➞ true is_sorted({1, 2, 3, 4, 5}) ➞ true is_sorted({1, 3, 2, 4, 5}) ➞ false is_sorted({1, 2...
#undef NDEBUG #include<assert.h> int main(){ assert (is_sorted({5}) == true); assert (is_sorted({1, 2, 3, 4, 5}) == true); assert (is_sorted({1, 3, 2, 4, 5}) == false); assert (is_sorted({1, 2, 3, 4, 5, 6}) == true); assert (is_sorted({1, 2, 3, 4, 5, 6, 7}) == true); assert (is_sorted({1, 3, 2, ...
maximum
/* 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, -...
/* 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, -...
/* 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, -...
#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,...
cycpattern_check
/* You are given 2 words. You need to return true if the second word or any of its rotations is a substring in the first word cycpattern_check("abcd","abd") => false cycpattern_check("hello","ell") => true cycpattern_check("whassup","psus") => false cycpattern_check("abab","baa") => true cycpattern_check("efef","eeff")...
/* You are given 2 words. You need to return true if the second word or any of its rotations is a substring in the first word cycpattern_check("abcd","abd") => false cycpattern_check("hello","ell") => true cycpattern_check("whassup","psus") => false cycpattern_check("abab","baa") => true cycpattern_check("efef","eeff")...
/* You are given 2 words. You need to return true if the second word or any of its rotations is a substring in the first word cycpattern_check("abcd","abd") => false cycpattern_check("hello","ell") => true cycpattern_check("whassup","psus") => false cycpattern_check("abab","baa") => true cycpattern_check("efef","eeff")...
#undef NDEBUG #include<assert.h> int main(){ assert(cycpattern_check("xyzw","xyw") == false ); assert(cycpattern_check("yello","ell") == true ); assert(cycpattern_check("whattup","ptut") == false ); assert(cycpattern_check("efef","fee") == true ); assert(cycpattern_check("abab","aabb") == false ); ...
below_zero
/* 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({...
/* 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({...
/* 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({...
#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); ...
incr_list
/* Return vector with elements incremented by 1. >>> incr_vector({1, 2, 3}) {2, 3, 4} >>> incr_vector({5, 3, 5, 2, 3, 3, 9, 0, 123}) {6, 4, 6, 3, 4, 4, 10, 1, 124} */ #include<stdio.h> #include<vector> using namespace std; vector<int> incr_list(vector<int> l){ for (int i=0;i<l.size();i++) l[i]-=1; retur...
/* Return vector with elements incremented by 1. >>> incr_vector({1, 2, 3}) {2, 3, 4} >>> incr_vector({5, 3, 5, 2, 3, 3, 9, 0, 123}) {6, 4, 6, 3, 4, 4, 10, 1, 124} */ #include<stdio.h> #include<vector> using namespace std; vector<int> incr_list(vector<int> l){ for (int i=0;i<l.size();i++) l[i]+=1; retur...
/* Return vector with elements incremented by 1. >>> incr_vector({1, 2, 3}) {2, 3, 4} >>> incr_vector({5, 3, 5, 2, 3, 3, 9, 0, 123}) {6, 4, 6, 3, 4, 4, 10, 1, 124} */ #include<stdio.h> #include<vector> using namespace std; vector<int> incr_list(vector<int> l){
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(incr_list({}) , {})); assert (issame(incr_list({3, 2, 1}) , {4, 3, ...
sort_even
/* This function takes a vector l and returns a vector l' such that l' is identical to l in the odd indicies, while its values at the even indicies are equal to the values of the even indicies of l, but sorted. >>> sort_even({1, 2, 3}) {1, 2, 3} >>> sort_even({5, 6, 3, 4}) {3, 6, 5, 4} */ #include<stdio.h> #include<mat...
/* This function takes a vector l and returns a vector l' such that l' is identical to l in the odd indicies, while its values at the even indicies are equal to the values of the even indicies of l, but sorted. >>> sort_even({1, 2, 3}) {1, 2, 3} >>> sort_even({5, 6, 3, 4}) {3, 6, 5, 4} */ #include<stdio.h> #include<mat...
/* This function takes a vector l and returns a vector l' such that l' is identical to l in the odd indicies, while its values at the even indicies are equal to the values of the even indicies of l, but sorted. >>> sort_even({1, 2, 3}) {1, 2, 3} >>> sort_even({5, 6, 3, 4}) {3, 6, 5, 4} */ #include<stdio.h> #include<mat...
#undef NDEBUG #include<assert.h> bool issame(vector<float> a,vector<float>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (abs(a[i]-b[i])>1e-4) return false; } return true; } int main(){ assert (issame(sort_even({1, 2, 3}), {1, 2, 3})); assert (issame(sort...
will_it_fly
/* 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...
/* 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...
/* 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...
#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); ...
double_the_difference
/* Given a vector of numbers, return the sum of squares of the numbers in the vector that are odd. Ignore numbers that are negative or not integers. double_the_difference({1, 3, 2, 0}) == 1 + 9 + 0 + 0 = 10 double_the_difference({-1, -2, 0}) == 0 double_the_difference({9, -2}) == 81 double_the_difference({0}) == 0 ...
/* Given a vector of numbers, return the sum of squares of the numbers in the vector that are odd. Ignore numbers that are negative or not integers. double_the_difference({1, 3, 2, 0}) == 1 + 9 + 0 + 0 = 10 double_the_difference({-1, -2, 0}) == 0 double_the_difference({9, -2}) == 81 double_the_difference({0}) == 0 ...
/* Given a vector of numbers, return the sum of squares of the numbers in the vector that are odd. Ignore numbers that are negative or not integers. double_the_difference({1, 3, 2, 0}) == 1 + 9 + 0 + 0 = 10 double_the_difference({-1, -2, 0}) == 0 double_the_difference({9, -2}) == 81 double_the_difference({0}) == 0 ...
#undef NDEBUG #include<assert.h> int main(){ assert (double_the_difference({}) == 0); assert (double_the_difference({5, 4}) == 25); assert (double_the_difference({0.1, 0.2, 0.3}) == 0 ); assert (double_the_difference({-10, -20, -30}) == 0 ); assert (double_the_difference({-1, -2, 8}) == 0); asse...
filter_integers
/* 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<...
/* 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<...
/* 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<...
#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...
split_words
/* Given a string of words, return a vector of words split on whitespace, if no whitespaces exists in the text you should split on commas ',' if no commas exists you should return a vector with one element, the number of lower-case letters with odd order in the alphabet, ord("a") = 0, ord("b") = 1, ... ord("z") = 25 Ex...
/* Given a string of words, return a vector of words split on whitespace, if no whitespaces exists in the text you should split on commas ',' if no commas exists you should return a vector with one element, the number of lower-case letters with odd order in the alphabet, ord("a") = 0, ord("b") = 1, ... ord("z") = 25 Ex...
/* Given a string of words, return a vector of words split on whitespace, if no whitespaces exists in the text you should split on commas ',' if no commas exists you should return a vector with one element, the number of lower-case letters with odd order in the alphabet, ord("a") = 0, ord("b") = 1, ... ord("z") = 25 Ex...
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(split_words("Hello world!") , {"Hello","world!"})); assert (issam...
select_words
/* Given a string s and a natural number n, you have been tasked to implement a function that returns a vector of all words from string s that contain exactly n consonants, in order these words appear in the string s. If the string s is empty then the function should return an empty vector. Note: you may assume the i...
/* Given a string s and a natural number n, you have been tasked to implement a function that returns a vector of all words from string s that contain exactly n consonants, in order these words appear in the string s. If the string s is empty then the function should return an empty vector. Note: you may assume the i...
/* Given a string s and a natural number n, you have been tasked to implement a function that returns a vector of all words from string s that contain exactly n consonants, in order these words appear in the string s. If the string s is empty then the function should return an empty vector. Note: you may assume the i...
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(select_words("Mary had a little lamb", 4) , {"little"} )); asse...
find_zero
#include<stdio.h> #include<math.h> #include<vector> using namespace std; double poly(vector<double> xs, double x){ /* Evaluates polynomial with coefficients xs at point x. return xs[0] + xs[1] * x + xs[1] * x^2 + .... xs[n] * x^n */ double sum=0; int i; for (i=0;i<xs.size();i++) { ...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; double poly(vector<double> xs, double x){ /* Evaluates polynomial with coefficients xs at point x. return xs[0] + xs[1] * x + xs[1] * x^2 + .... xs[n] * x^n */ double sum=0; int i; for (i=0;i<xs.size();i++) { ...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; double poly(vector<double> xs, double x){ /* Evaluates polynomial with coefficients xs at point x. return xs[0] + xs[1] * x + xs[1] * x^2 + .... xs[n] * x^n */ double sum=0; int i; for (i=0;i<xs.size();i++) { ...
#undef NDEBUG #include<assert.h> int main(){ double solution; int ncoeff; for (int i=0;i<100;i++) { ncoeff = 2 * (1+rand()%4); vector<double> coeffs = {}; for (int j=0;j<ncoeff;j++) { double coeff = -10+rand()%21; if (coeff == 0) coeff = 1; ...
get_max_triples
/* You are given a positive integer n. You have to create an integer vector a of length n. For each i (1 ≤ i ≤ n), the value of a{i} = i * i - i + 1. Return the number of triples (a{i}, a{j}, a{k}) of a where i < j < k, and a[i] + a[j] + a[k] is a multiple of 3. Example : Input: n = 5 Output: 1 Ex...
/* You are given a positive integer n. You have to create an integer vector a of length n. For each i (1 ≤ i ≤ n), the value of a{i} = i * i - i + 1. Return the number of triples (a{i}, a{j}, a{k}) of a where i < j < k, and a[i] + a[j] + a[k] is a multiple of 3. Example : Input: n = 5 Output: 1 Ex...
/* You are given a positive integer n. You have to create an integer vector a of length n. For each i (1 ≤ i ≤ n), the value of a{i} = i * i - i + 1. Return the number of triples (a{i}, a{j}, a{k}) of a where i < j < k, and a[i] + a[j] + a[k] is a multiple of 3. Example : Input: n = 5 Output: 1 Ex...
#undef NDEBUG #include<assert.h> int main(){ assert (get_matrix_triples(5) == 1); assert (get_matrix_triples(6) == 4); assert (get_matrix_triples(10) == 36); assert (get_matrix_triples(100) == 53361); assert (get_matrix_triples(5) == 1); assert (get_matrix_triples(6) == 4); assert (get_matri...
generate_integers
/* Given two positive integers a and b, return the even digits between a and b, in ascending order. For example: generate_integers(2, 8) => {2, 4, 6, 8} generate_integers(8, 2) => {2, 4, 6, 8} generate_integers(10, 14) => {} */ #include<stdio.h> #include<vector> using namespace std; vector<int> generate_integers(int a...
/* Given two positive integers a and b, return the even digits between a and b, in ascending order. For example: generate_integers(2, 8) => {2, 4, 6, 8} generate_integers(8, 2) => {2, 4, 6, 8} generate_integers(10, 14) => {} */ #include<stdio.h> #include<vector> using namespace std; vector<int> generate_integers(int a...
/* Given two positive integers a and b, return the even digits between a and b, in ascending order. For example: generate_integers(2, 8) => {2, 4, 6, 8} generate_integers(8, 2) => {2, 4, 6, 8} generate_integers(10, 14) => {} */ #include<stdio.h> #include<vector> using namespace std; vector<int> generate_integers(int a...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(generate_integers(2, 10) , {2, 4, 6, 8})); assert (issame(generate_inte...
multiply
/* Complete the function that takes two integers and returns the product of their unit digits. Assume the input is always valid. Examples: multiply(148, 412) should return 16. multiply(19, 28) should return 72. multiply(2020, 1851) should return 0. multiply(14,-15) should return 20. */ #include<stdio.h> #include<math....
/* Complete the function that takes two integers and returns the product of their unit digits. Assume the input is always valid. Examples: multiply(148, 412) should return 16. multiply(19, 28) should return 72. multiply(2020, 1851) should return 0. multiply(14,-15) should return 20. */ #include<stdio.h> #include<math....
/* Complete the function that takes two integers and returns the product of their unit digits. Assume the input is always valid. Examples: multiply(148, 412) should return 16. multiply(19, 28) should return 72. multiply(2020, 1851) should return 0. multiply(14,-15) should return 20. */ #include<stdio.h> #include<math....
#undef NDEBUG #include<assert.h> int main(){ assert (multiply(148, 412) == 16 ); assert (multiply(19, 28) == 72 ); assert (multiply(2020, 1851) == 0); assert (multiply(14,-15) == 20 ); assert (multiply(76, 67) == 42 ); assert (multiply(17, 27) == 49 ); assert ...
move_one_ball
/* We have a vector "arr" of N integers arr[1], arr[2], ..., arr[N].The numbers in the vector will be randomly ordered. Your task is to determine if it is possible to get a vector sorted in non-decreasing order by performing the following operation on the given vector: You are allowed to perform right shift operat...
/* We have a vector "arr" of N integers arr[1], arr[2], ..., arr[N].The numbers in the vector will be randomly ordered. Your task is to determine if it is possible to get a vector sorted in non-decreasing order by performing the following operation on the given vector: You are allowed to perform right shift operat...
/* We have a vector "arr" of N integers arr[1], arr[2], ..., arr[N].The numbers in the vector will be randomly ordered. Your task is to determine if it is possible to get a vector sorted in non-decreasing order by performing the following operation on the given vector: You are allowed to perform right shift operat...
#undef NDEBUG #include<assert.h> int main(){ assert (move_one_ball({3, 4, 5, 1, 2})==true); assert (move_one_ball({3, 5, 10, 1, 2})==true); assert (move_one_ball({4, 3, 1, 2})==false); assert (move_one_ball({3, 5, 4, 1, 2})==false); assert (move_one_ball({})==true); assert(move_one_ball({3,4,5,1...
modp
/* Return 2^n modulo p (be aware of numerics). >>> modp(3, 5) 3 >>> modp(1101, 101) 2 >>> modp(0, 101) 1 >>> modp(3, 11) 8 >>> modp(100, 101) 1 */ #include<stdio.h> using namespace std; int modp(int n,int p){ int out=1; for (int i=0;i<n;i++) out=out*2; return out%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 */ #include<stdio.h> using namespace std; int modp(int n,int p){ int out=1; for (int i=0;i<n;i++) out=(out*2)%p; return out%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 */ #include<stdio.h> using namespace std; int modp(int n,int p){
#undef NDEBUG #include<assert.h> int main(){ assert (modp(3, 5) == 3); assert (modp(1101, 101) == 2); assert (modp(0, 101) == 1); assert (modp(3, 11) == 8); assert (modp(100, 101) == 1); assert (modp(30, 5) == 4); assert (modp(31, 5) == 3); assert (modp(3, 5) == 3); assert (modp(1101...
factorize
/* Return vector of prime factors of given integer in the order from smallest to largest. Each of the factors should be vectored number of times corresponding to how many times it appeares in factorization. Input number should be equal to the product of all factors >>> factorize(8) {2, 2, 2} >>> factorize(25) {5, 5} >>...
/* Return vector of prime factors of given integer in the order from smallest to largest. Each of the factors should be vectored number of times corresponding to how many times it appeares in factorization. Input number should be equal to the product of all factors >>> factorize(8) {2, 2, 2} >>> factorize(25) {5, 5} >>...
/* Return vector of prime factors of given integer in the order from smallest to largest. Each of the factors should be vectored number of times corresponding to how many times it appeares in factorization. Input number should be equal to the product of all factors >>> factorize(8) {2, 2, 2} >>> factorize(25) {5, 5} >>...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(factorize(2) , {2})); assert (issame(factorize(4) , {2, 2})); a...
get_odd_collatz
/* 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...
/* 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...
/* 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...
#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...
x_or_y
/* A simple program which should return the value of x if n is a prime number and should return the value of y otherwise. Examples: for x_or_y(7, 34, 12) == 34 for x_or_y(15, 8, 5) == 5 */ #include<stdio.h> using namespace std; int x_or_y(int n,int x,int y){ bool isp=true; if (n<2) isp=false; for (int i=...
/* A simple program which should return the value of x if n is a prime number and should return the value of y otherwise. Examples: for x_or_y(7, 34, 12) == 34 for x_or_y(15, 8, 5) == 5 */ #include<stdio.h> using namespace std; int x_or_y(int n,int x,int y){ bool isp=true; if (n<2) isp=false; for (int i=...
/* A simple program which should return the value of x if n is a prime number and should return the value of y otherwise. Examples: for x_or_y(7, 34, 12) == 34 for x_or_y(15, 8, 5) == 5 */ #include<stdio.h> using namespace std; int x_or_y(int n,int x,int y){
#undef NDEBUG #include<assert.h> int main(){ assert (x_or_y(7, 34, 12) == 34); assert (x_or_y(15, 8, 5) == 5); assert (x_or_y(3, 33, 5212) == 33); assert (x_or_y(1259, 3, 52) == 3); assert (x_or_y(7919, -1, 12) == -1); assert (x_or_y(3609, 1245, 583) == 583); assert (x_or_y(91, 56, 129) == 1...
prime_fib
/* prime_fib returns n-th number that is a Fibonacci number and it's also prime. >>> prime_fib(1) 2 >>> prime_fib(2) 3 >>> prime_fib(3) 5 >>> prime_fib(4) 13 >>> prime_fib(5) 89 */ #include<stdio.h> using namespace std; int prime_fib(int n){ int f1,f2,m; f1=1;f2=2; int count=0; while (count<n) { ...
/* prime_fib returns n-th number that is a Fibonacci number and it's also prime. >>> prime_fib(1) 2 >>> prime_fib(2) 3 >>> prime_fib(3) 5 >>> prime_fib(4) 13 >>> prime_fib(5) 89 */ #include<stdio.h> using namespace std; int prime_fib(int n){ int f1,f2,m; f1=1;f2=2; int count=0; while (count<n) { ...
/* prime_fib returns n-th number that is a Fibonacci number and it's also prime. >>> prime_fib(1) 2 >>> prime_fib(2) 3 >>> prime_fib(3) 5 >>> prime_fib(4) 13 >>> prime_fib(5) 89 */ #include<stdio.h> using namespace std; int prime_fib(int n){
#undef NDEBUG #include<assert.h> int main(){ assert (prime_fib(1) == 2); assert (prime_fib(2) == 3); assert (prime_fib(3) == 5); assert (prime_fib(4) == 13); assert (prime_fib(5) == 89); assert (prime_fib(6) == 233); assert (prime_fib(7) == 1597); assert (prime_fib(8) == 28657); asse...
words_in_sentence
/* You are given a string representing a sentence, the sentence contains some words separated by a space, and you have to return a string that contains the words from the original sentence, whose lengths are prime numbers, the order of the words in the new string should be the same as the original one. Example 1: ...
/* You are given a string representing a sentence, the sentence contains some words separated by a space, and you have to return a string that contains the words from the original sentence, whose lengths are prime numbers, the order of the words in the new string should be the same as the original one. Example 1: ...
/* You are given a string representing a sentence, the sentence contains some words separated by a space, and you have to return a string that contains the words from the original sentence, whose lengths are prime numbers, the order of the words in the new string should be the same as the original one. Example 1: ...
#undef NDEBUG #include<assert.h> int main(){ assert (words_in_sentence("This is a test") == "is"); assert (words_in_sentence("lets go for swimming") == "go for"); assert (words_in_sentence("there is no place available here") == "there is no place"); assert (words_in_sentence("Hi I am Hussein") == "Hi am...
add2
/* Add two numbers x and y >>> add(2, 3) 5 >>> add(5, 7) 12 */ #include<stdio.h> #include<stdlib.h> using namespace std; int add(int x,int y){ return x-y; }
/* Add two numbers x and y >>> add(2, 3) 5 >>> add(5, 7) 12 */ #include<stdio.h> #include<stdlib.h> using namespace std; int add(int x,int y){ return x+y; }
/* Add two numbers x and y >>> add(2, 3) 5 >>> add(5, 7) 12 */ #include<stdio.h> #include<stdlib.h> using namespace std; int add(int x,int y){
#undef NDEBUG #include<assert.h> int main(){ assert (add(0, 1) == 1); assert (add(1, 0) == 1); assert (add(2, 3) == 5); assert (add(5, 7) == 12); assert (add(7, 5) == 12); for (int i=0;i<100;i+=1) { int x=rand()%1000; int y=rand()%1000; assert (add(x, y) == x + y); ...
check_if_last_char_is_a_letter
/* Create a function that returns true if the last character of a given string is an alphabetical character and is not a part of a word, and false otherwise. Note: "word" is a group of characters separated by space. Examples: check_if_last_char_is_a_letter("apple pie") ➞ false check_if_last_char_is_a_letter("apple pi ...
/* Create a function that returns true if the last character of a given string is an alphabetical character and is not a part of a word, and false otherwise. Note: "word" is a group of characters separated by space. Examples: check_if_last_char_is_a_letter("apple pie") ➞ false check_if_last_char_is_a_letter("apple pi ...
/* Create a function that returns true if the last character of a given string is an alphabetical character and is not a part of a word, and false otherwise. Note: "word" is a group of characters separated by space. Examples: check_if_last_char_is_a_letter("apple pie") ➞ false check_if_last_char_is_a_letter("apple pi ...
#undef NDEBUG #include<assert.h> int main(){ assert (check_if_last_char_is_a_letter("apple") == false); assert (check_if_last_char_is_a_letter("apple pi e") == true); assert (check_if_last_char_is_a_letter("eeeee") == false); assert (check_if_last_char_is_a_letter("A") == true); assert (check_if_las...
simplify
/* Your task is to implement a function that will simplify the expression x * n. The function returns true if x * n evaluates to a whole number and false otherwise. Both x and n, are string representation of a fraction, and have the following format, <numerator>/<denominator> where both numerator and denominator are po...
/* Your task is to implement a function that will simplify the expression x * n. The function returns true if x * n evaluates to a whole number and false otherwise. Both x and n, are string representation of a fraction, and have the following format, <numerator>/<denominator> where both numerator and denominator are po...
/* Your task is to implement a function that will simplify the expression x * n. The function returns true if x * n evaluates to a whole number and false otherwise. Both x and n, are string representation of a fraction, and have the following format, <numerator>/<denominator> where both numerator and denominator are po...
#undef NDEBUG #include<assert.h> int main(){ assert (simplify("1/5", "5/1") == true); assert (simplify("1/6", "2/1") == false); assert (simplify("5/1", "3/1") == true); assert (simplify("7/10", "10/2") == false); assert (simplify("2/10", "50/10") == true); assert (simplify("7/2", "4/2") == true)...
even_odd_count
/* Given an integer. return a vector that has the number of even and odd digits respectively. Example: even_odd_count(-12) ==> {1, 1} even_odd_count(123) ==> {1, 2} */ #include<stdio.h> #include<math.h> #include<string> #include<vector> using namespace std; vector<int> even_odd_count(int num){ string w=to...
/* Given an integer. return a vector that has the number of even and odd digits respectively. Example: even_odd_count(-12) ==> {1, 1} even_odd_count(123) ==> {1, 2} */ #include<stdio.h> #include<math.h> #include<string> #include<vector> using namespace std; vector<int> even_odd_count(int num){ string w=to...
/* Given an integer. return a vector that has the number of even and odd digits respectively. Example: even_odd_count(-12) ==> {1, 1} even_odd_count(123) ==> {1, 2} */ #include<stdio.h> #include<math.h> #include<string> #include<vector> using namespace std; vector<int> even_odd_count(int num){
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(even_odd_count(7) , {0, 1})); assert (issame(even_odd_count(-78) , {1, ...
sum_squares
/* " This function will take a vector of integers. For all entries in the vector, the function shall square the integer entry if its index is a multiple of 3 and will cube the integer entry if its index is a multiple of 4 and not a multiple of 3. The function will not change the entries in the vector whose indexes ar...
/* " This function will take a vector of integers. For all entries in the vector, the function shall square the integer entry if its index is a multiple of 3 and will cube the integer entry if its index is a multiple of 4 and not a multiple of 3. The function will not change the entries in the vector whose indexes ar...
/* " This function will take a vector of integers. For all entries in the vector, the function shall square the integer entry if its index is a multiple of 3 and will cube the integer entry if its index is a multiple of 4 and not a multiple of 3. The function will not change the entries in the vector whose indexes ar...
#undef NDEBUG #include<assert.h> int main(){ assert (sum_squares({1,2,3}) == 6); assert (sum_squares({1,4,9}) == 14); assert (sum_squares({}) == 0); assert (sum_squares({1,1,1,1,1,1,1,1,1}) == 9); assert (sum_squares({-1,-1,-1,-1,-1,-1,-1,-1,-1}) == -3); assert (sum_squares({0}) == 0); asser...
match_parens
/* You are given a vector of two strings, both strings consist of open parentheses '(' or close parentheses ')' only. Your job is to check if it is possible to concatenate the two strings in some order, that the resulting string will be good. A string S is considered to be good if and only if all parentheses in S are b...
/* You are given a vector of two strings, both strings consist of open parentheses '(' or close parentheses ')' only. Your job is to check if it is possible to concatenate the two strings in some order, that the resulting string will be good. A string S is considered to be good if and only if all parentheses in S are b...
/* You are given a vector of two strings, both strings consist of open parentheses '(' or close parentheses ')' only. Your job is to check if it is possible to concatenate the two strings in some order, that the resulting string will be good. A string S is considered to be good if and only if all parentheses in S are b...
#undef NDEBUG #include<assert.h> int main(){ assert (match_parens({"()(", ")"}) == "Yes"); assert (match_parens({")", ")"}) == "No"); assert (match_parens({"(()(())", "())())"}) == "No"); assert (match_parens({")())", "(()()("}) == "Yes"); assert (match_parens({"(())))", "(()())(("}) == "Yes"); ...
sort_third
/* 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...
/* 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...
/* 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...
#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...
how_many_times
/* Find how many times a given substring can be found in the original string. Count overlaping cases. >>> how_many_times("", "a") 0 >>> how_many_times("aaa", "a") 3 >>> how_many_times("aaaa", "aa") 3 */ #include<stdio.h> #include<string> using namespace std; int how_many_times(string str,string substring){ int out=...
/* Find how many times a given substring can be found in the original string. Count overlaping cases. >>> how_many_times("", "a") 0 >>> how_many_times("aaa", "a") 3 >>> how_many_times("aaaa", "aa") 3 */ #include<stdio.h> #include<string> using namespace std; int how_many_times(string str,string substring){ int out=...
/* 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){
#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); assert (how_many_times("", "x") == 0); assert (how_many_times("xy...
triangle_area2
/* 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; double triangle_area(double a,double h){ return a*(h/0.5); }
/* 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; double triangle_area(double a,double h){ return (a*h)*0.5; }
/* 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; double triangle_area(double a,double h){
#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); assert(abs(triangle_area(10.5, 3.5) - 18.375)<1e-4); assert(abs(triangle_area(5.5, 5.5) - 15.125)<1e-4); assert...
specialfilter
/* Write a function that takes a vector of numbers as input and returns the number of elements in the vector that are greater than 10 and both first and last digits of a number are odd (1, 3, 5, 7, 9). For example: specialFilter({15, -73, 14, -15}) => 1 specialFilter({33, -2, -3, 45, 21, 109}) => 2 */ #include<stdio...
/* Write a function that takes a vector of numbers as input and returns the number of elements in the vector that are greater than 10 and both first and last digits of a number are odd (1, 3, 5, 7, 9). For example: specialFilter({15, -73, 14, -15}) => 1 specialFilter({33, -2, -3, 45, 21, 109}) => 2 */ #include<stdio...
/* Write a function that takes a vector of numbers as input and returns the number of elements in the vector that are greater than 10 and both first and last digits of a number are odd (1, 3, 5, 7, 9). For example: specialFilter({15, -73, 14, -15}) => 1 specialFilter({33, -2, -3, 45, 21, 109}) => 2 */ #include<stdio...
#undef NDEBUG #include<assert.h> int main(){ assert (specialFilter({5, -2, 1, -5}) == 0 ); assert (specialFilter({15, -73, 14, -15}) == 1); assert (specialFilter({33, -2, -3, 45, 21, 109}) == 2); assert (specialFilter({43, -12, 93, 125, 121, 109}) == 4); assert (specialFilter({71, -2, -33, 75, 21, ...
rescale_to_unit
/* 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;...
/* 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;...
/* 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;...
#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 (...
remove_vowels
/* remove_vowels is a function that takes string and returns string without vowels. >>> remove_vowels("") "" >>> remove_vowels("abcdef\nghijklm") "bcdf\nghjklm" >>> remove_vowels("abcdef") "bcdf" >>> remove_vowels("aaaaa") "" >>> remove_vowels("aaBAA") "B" >>> remove_vowels("zbcd") "zbcd" */ #include<stdio.h> #include<...
/* remove_vowels is a function that takes string and returns string without vowels. >>> remove_vowels("") "" >>> remove_vowels("abcdef\nghijklm") "bcdf\nghjklm" >>> remove_vowels("abcdef") "bcdf" >>> remove_vowels("aaaaa") "" >>> remove_vowels("aaBAA") "B" >>> remove_vowels("zbcd") "zbcd" */ #include<stdio.h> #include<...
/* remove_vowels is a function that takes string and returns string without vowels. >>> remove_vowels("") "" >>> remove_vowels("abcdef\nghijklm") "bcdf\nghjklm" >>> remove_vowels("abcdef") "bcdf" >>> remove_vowels("aaaaa") "" >>> remove_vowels("aaBAA") "B" >>> remove_vowels("zbcd") "zbcd" */ #include<stdio.h> #include<...
#undef NDEBUG #include<assert.h> int main(){ assert (remove_vowels("") == ""); assert (remove_vowels("abcdef\nghijklm") == "bcdf\nghjklm"); assert (remove_vowels("fedcba") == "fdcb"); assert (remove_vowels("eeeee") == ""); assert (remove_vowels("acBAA") == "cB"); assert (remove_vowels("EcBOO") =...
bf
/* There are eight planets in our solar system: the closerst to the Sun is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn, Uranus, Neptune. Write a function that takes two planet names as strings planet1 and planet2. The function should return a vector containing all planets whose orbits are loca...
/* There are eight planets in our solar system: the closerst to the Sun is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn, Uranus, Neptune. Write a function that takes two planet names as strings planet1 and planet2. The function should return a vector containing all planets whose orbits are loca...
/* There are eight planets in our solar system: the closerst to the Sun is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn, Uranus, Neptune. Write a function that takes two planet names as strings planet1 and planet2. The function should return a vector containing all planets whose orbits are loca...
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(bf("Jupiter", "Neptune") , {"Saturn", "Uranus"})); assert (issame...
sum_squares2
/* You are given a vector of numbers. You need to return the sum of squared numbers in the given vector, round each element in the vector to the upper int(Ceiling) first. Examples: For lst = {1,2,3} the output should be 14 For lst = {1,4,9} the output should be 98 For lst = {1,3,5,7} the output should be 84 For lst = {...
/* You are given a vector of numbers. You need to return the sum of squared numbers in the given vector, round each element in the vector to the upper int(Ceiling) first. Examples: For lst = {1,2,3} the output should be 14 For lst = {1,4,9} the output should be 98 For lst = {1,3,5,7} the output should be 84 For lst = {...
/* You are given a vector of numbers. You need to return the sum of squared numbers in the given vector, round each element in the vector to the upper int(Ceiling) first. Examples: For lst = {1,2,3} the output should be 14 For lst = {1,4,9} the output should be 98 For lst = {1,3,5,7} the output should be 84 For lst = {...
#undef NDEBUG #include<assert.h> int main(){ assert (sum_squares({1,2,3})==14); assert (sum_squares({1.0,2,3})==14); assert (sum_squares({1,3,5,7})==84); assert (sum_squares({1.4,4.2,0})==29); assert (sum_squares({-2.4,1,1})==6); assert (sum_squares({100,1,15,2})==10230); assert (sum_squares...
count_up_to
/* 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...
/* 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...
/* 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...
#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})); ...
compare_one
/* Create a function that takes integers, floats, or strings representing real numbers, and returns the larger variable in its given variable type. Return "None" if the values are equal. Note: If a real number is represented as a string, the floating point might be . or , compare_one(1, 2.5) ➞ 2.5 compare_one(1, "2,3"...
/* Create a function that takes integers, floats, or strings representing real numbers, and returns the larger variable in its given variable type. Return "None" if the values are equal. Note: If a real number is represented as a string, the floating point might be . or , compare_one(1, 2.5) ➞ 2.5 compare_one(1, "2,3"...
/* Create a function that takes integers, floats, or strings representing real numbers, and returns the larger variable in its given variable type. Return "None" if the values are equal. Note: If a real number is represented as a string, the floating point might be . or , compare_one(1, 2.5) ➞ 2.5 compare_one(1, "2,3"...
#undef NDEBUG #include<assert.h> int main(){ assert (boost::any_cast<int>(compare_one(1, 2)) == 2); assert (boost::any_cast<double>(compare_one(1, 2.5))== 2.5); assert (boost::any_cast<int>(compare_one(2, 3)) == 3); assert (boost::any_cast<int>(compare_one(5, 6)) == 6); assert (boost::any_cast<strin...
do_algebra
/* Given two vectors operator, and operand. The first vector has basic algebra operations, and the second vector is a vector of integers. Use the two given vectors to build the algebric expression and return the evaluation of this expression. The basic algebra operations: Addition ( + ) Subtraction ( - ) Multiplic...
/* Given two vectors operator, and operand. The first vector has basic algebra operations, and the second vector is a vector of integers. Use the two given vectors to build the algebric expression and return the evaluation of this expression. The basic algebra operations: Addition ( + ) Subtraction ( - ) Multiplic...
/* Given two vectors operator, and operand. The first vector has basic algebra operations, and the second vector is a vector of integers. Use the two given vectors to build the algebric expression and return the evaluation of this expression. The basic algebra operations: Addition ( + ) Subtraction ( - ) Multiplic...
#undef NDEBUG #include<assert.h> int main(){ assert (do_algebra({"**", "*", "+"}, {2, 3, 4, 5}) == 37); assert (do_algebra({"+", "*", "-"}, {2, 3, 4, 5}) == 9); assert (do_algebra({"//", "*"}, {7, 3, 4}) == 8); assert (do_algebra({"**","*","+"}, {2,3,4,5}) == 37); assert (do_algebra({"+","*","-"}, {...
even_odd_palindrome
/* 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 ...
/* 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 ...
/* 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 ...
#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...
derivative
/* 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...
/* 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...
/* 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...
#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...
by_length
/* Given a vector of integers, sort the integers that are between 1 and 9 inclusive, reverse the resulting vector, and then replace each digit by its corresponding name from "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine". For example: arr = {2, 1, 1, 4, 5, 8, 2, 3} -> sort arr -> {...
/* Given a vector of integers, sort the integers that are between 1 and 9 inclusive, reverse the resulting vector, and then replace each digit by its corresponding name from "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine". For example: arr = {2, 1, 1, 4, 5, 8, 2, 3} -> sort arr -> {...
/* Given a vector of integers, sort the integers that are between 1 and 9 inclusive, reverse the resulting vector, and then replace each digit by its corresponding name from "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine". For example: arr = {2, 1, 1, 4, 5, 8, 2, 3} -> sort arr -> {...
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(by_length({2, 1, 1, 4, 5, 8, 2, 3}) , {"Eight", "Five", "Four", "Thre...
fruit_distribution
/* In this task, you will be given a string that represents a number of apples and oranges that are distributed in a basket of fruit this basket contains apples, oranges, and mango fruits. Given the string that represents the total number of the oranges and apples and an integer that represent the total number of th...
/* In this task, you will be given a string that represents a number of apples and oranges that are distributed in a basket of fruit this basket contains apples, oranges, and mango fruits. Given the string that represents the total number of the oranges and apples and an integer that represent the total number of th...
/* In this task, you will be given a string that represents a number of apples and oranges that are distributed in a basket of fruit this basket contains apples, oranges, and mango fruits. Given the string that represents the total number of the oranges and apples and an integer that represent the total number of th...
#undef NDEBUG #include<assert.h> int main(){ assert (fruit_distribution("5 apples and 6 oranges",19) == 8); assert (fruit_distribution("5 apples and 6 oranges",21) == 10); assert (fruit_distribution("0 apples and 1 oranges",3) == 2); assert (fruit_distribution("1 apples and 0 oranges",3) == 2); asse...
greatest_common_divisor
/* Return a greatest common divisor of two integers a and b >>> greatest_common_divisor(3, 5) 1 >>> greatest_common_divisor(25, 15) 5 */ #include<stdio.h> using namespace std; int greatest_common_divisor(int a, int b){ int out,m; while (true){ if (a<b) { m=a;a=b;b=m; } ...
/* Return a greatest common divisor of two integers a and b >>> greatest_common_divisor(3, 5) 1 >>> greatest_common_divisor(25, 15) 5 */ #include<stdio.h> using namespace std; int greatest_common_divisor(int a, int b){ int out,m; while (true){ if (a<b) { m=a;a=b;b=m; } ...
/* Return a greatest common divisor of two integers a and b >>> greatest_common_divisor(3, 5) 1 >>> greatest_common_divisor(25, 15) 5 */ #include<stdio.h> using namespace std; int greatest_common_divisor(int a, int b){
#undef NDEBUG #include<assert.h> int main(){ assert (greatest_common_divisor(3, 7) == 1); assert (greatest_common_divisor(10, 15) == 5); assert (greatest_common_divisor(49, 14) == 7); assert (greatest_common_divisor(144, 60) == 12); assert (greatest_common_divisor(3, 7) == 1); assert (greate...
fibfib
/* The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: fibfib(0) == 0 fibfib(1) == 0 fibfib(2) == 1 fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3). Please write a function to efficiently compute the n-th element of the fibfib number sequence. >>> fibfib(1) 0 >>> ...
/* The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: fibfib(0) == 0 fibfib(1) == 0 fibfib(2) == 1 fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3). Please write a function to efficiently compute the n-th element of the fibfib number sequence. >>> fibfib(1) 0 >>> ...
/* The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: fibfib(0) == 0 fibfib(1) == 0 fibfib(2) == 1 fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3). Please write a function to efficiently compute the n-th element of the fibfib number sequence. >>> fibfib(1) 0 >>> ...
#undef NDEBUG #include<assert.h> int main(){ assert (fibfib(2) == 1); assert (fibfib(1) == 0); assert (fibfib(5) == 4); assert (fibfib(8) == 24); assert (fibfib(10) == 81); assert (fibfib(12) == 274); assert (fibfib(14) == 927); assert (fibfib(2) == 1); assert (fibfib(1) == 0); a...
fib4
/* 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...
/* 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...
/* 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...
#undef NDEBUG #include<assert.h> int main(){ assert (fib4(5) == 4); assert (fib4(8) == 28); assert (fib4(10) == 104); assert (fib4(12) == 386); assert (fib4(5) == 4); assert (fib4(8) == 28); assert (fib4(10) == 104); assert (fib4(12) == 386); assert (fib4(1) == 0); assert (fib4(2...
find_closest_elements
/* From a supplied vector of numbers (of length at least two) select and return two that are the closest to each other and return them in order (smaller number, larger number). >>> find_closest_elements({1.0, 2.0, 3.0, 4.0, 5.0, 2.2}) (2.0, 2.2) >>> find_closest_elements({1.0, 2.0, 3.0, 4.0, 5.0, 2.0}) (2.0, 2.0) */ #i...
/* From a supplied vector of numbers (of length at least two) select and return two that are the closest to each other and return them in order (smaller number, larger number). >>> find_closest_elements({1.0, 2.0, 3.0, 4.0, 5.0, 2.2}) (2.0, 2.2) >>> find_closest_elements({1.0, 2.0, 3.0, 4.0, 5.0, 2.0}) (2.0, 2.0) */ #i...
/* From a supplied vector of numbers (of length at least two) select and return two that are the closest to each other and return them in order (smaller number, larger number). >>> find_closest_elements({1.0, 2.0, 3.0, 4.0, 5.0, 2.2}) (2.0, 2.2) >>> find_closest_elements({1.0, 2.0, 3.0, 4.0, 5.0, 2.0}) (2.0, 2.0) */ #i...
#undef NDEBUG #include<assert.h> bool issame(vector<float> a,vector<float>b){ if (a.size()!=b.size()) return false; if (abs(abs(a[0] - a[1])-abs(b[0]-b[1]))>1e-4) return false; return true; } int main(){ assert (issame(find_closest_elements({1.0, 2.0, 3.9, 4.0, 5.0, 2.2}) , {3.9, 4.0})); assert (iss...
add
/* Given a non-empty vector of integers lst. add the even elements that are at odd indices.. Examples: add({4, 2, 6, 7}) ==> 2 */ #include<stdio.h> #include<vector> using namespace std; int add(vector<int> lst){ int sum=0; for (int i=0;i*2+1<lst.size();i++) if (lst[i*2+1]%2==1) sum+=lst[i*2+1]; ...
/* Given a non-empty vector of integers lst. add the even elements that are at odd indices.. Examples: add({4, 2, 6, 7}) ==> 2 */ #include<stdio.h> #include<vector> using namespace std; int add(vector<int> lst){ int sum=0; for (int i=0;i*2+1<lst.size();i++) if (lst[i*2+1]%2==0) sum+=lst[i*2+1]; ...
/* Given a non-empty vector of integers lst. add the even elements that are at odd indices.. Examples: add({4, 2, 6, 7}) ==> 2 */ #include<stdio.h> #include<vector> using namespace std; int add(vector<int> lst){
#undef NDEBUG #include<assert.h> int main(){ assert (add({4, 88}) == 88); assert (add({4, 5, 6, 7, 2, 122}) == 122); assert (add({4, 0, 6, 7}) == 0); assert (add({4, 4, 6, 8}) == 12); assert (add({4,88}) == 88); assert (add({4,5,6,7,2,122}) == 122); assert (add({4,0,6,7}) == 0); assert (...
prod_signs
/* You are given a vector arr of integers and you need to return sum of magnitudes of integers multiplied by product of all signs of each number in the vector, represented by 1, -1 or 0. Note: return -32768 for empty arr. Example: >>> prod_signs({1, 2, 2, -4}) == -9 >>> prod_signs({0, 1}) == 0 >>> prod_signs({}) == -3...
/* You are given a vector arr of integers and you need to return sum of magnitudes of integers multiplied by product of all signs of each number in the vector, represented by 1, -1 or 0. Note: return -32768 for empty arr. Example: >>> prod_signs({1, 2, 2, -4}) == -9 >>> prod_signs({0, 1}) == 0 >>> prod_signs({}) == -3...
/* You are given a vector arr of integers and you need to return sum of magnitudes of integers multiplied by product of all signs of each number in the vector, represented by 1, -1 or 0. Note: return -32768 for empty arr. Example: >>> prod_signs({1, 2, 2, -4}) == -9 >>> prod_signs({0, 1}) == 0 >>> prod_signs({}) == -3...
#undef NDEBUG #include<assert.h> int main(){ assert (prod_signs({1, 2, 2, -4}) == -9); assert (prod_signs({0, 1}) == 0); assert (prod_signs({1, 1, 1, 2, 3, -1, 1}) == -10); assert (prod_signs({}) == -32768); assert (prod_signs({2, 4,1, 2, -1, -1, 9}) == 20); assert (prod_signs({-1, 1, -1, 1}) ==...
is_happy
/* You are given a string s. Your task is to check if the string is happy or not. A string is happy if its length is at least 3 and every 3 consecutive letters are distinct For example: is_happy("a") => false is_happy("aa") => false is_happy("abcd") => true is_happy("aabb") => false is_happy("adb") => true is_happy("xy...
/* You are given a string s. Your task is to check if the string is happy or not. A string is happy if its length is at least 3 and every 3 consecutive letters are distinct For example: is_happy("a") => false is_happy("aa") => false is_happy("abcd") => true is_happy("aabb") => false is_happy("adb") => true is_happy("xy...
/* You are given a string s. Your task is to check if the string is happy or not. A string is happy if its length is at least 3 and every 3 consecutive letters are distinct For example: is_happy("a") => false is_happy("aa") => false is_happy("abcd") => true is_happy("aabb") => false is_happy("adb") => true is_happy("xy...
#undef NDEBUG #include<assert.h> int main(){ assert (is_happy("a") == false ); assert (is_happy("aa") == false ); assert (is_happy("abcd") == true ); assert (is_happy("aabb") == false ); assert (is_happy("adb") == true ); assert (is_happy("xyy") == false ); assert (is_happy("iopaxpoi") == tr...
anti_shuffle
/* Write a function that takes a string and returns an ordered version of it. Ordered version of string, is a string where all words (separated by space) are replaced by a new word where all the characters arranged in ascending order based on ascii value. Note: You should keep the order of words and blank spaces in the...
/* Write a function that takes a string and returns an ordered version of it. Ordered version of string, is a string where all words (separated by space) are replaced by a new word where all the characters arranged in ascending order based on ascii value. Note: You should keep the order of words and blank spaces in the...
/* Write a function that takes a string and returns an ordered version of it. Ordered version of string, is a string where all words (separated by space) are replaced by a new word where all the characters arranged in ascending order based on ascii value. Note: You should keep the order of words and blank spaces in the...
#undef NDEBUG #include<assert.h> int main(){ assert (anti_shuffle("Hi") == "Hi"); assert (anti_shuffle("hello") == "ehllo"); assert (anti_shuffle("number") == "bemnru"); assert (anti_shuffle("abcd") == "abcd"); assert (anti_shuffle("Hello World!!!") == "Hello !!!Wdlor"); assert (anti_shuffle("")...
hex_key
/* 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,...
/* 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,...
/* 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,...
#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 ); ...
rounded_avg
/* You are given two positive integers n and m, and your task is to compute the average of the integers from n through m (including n and m). Round the answer to the nearest integer(smaller one) and convert that to binary. If n is greater than m, return "-1". Example: rounded_avg(1, 5) => "11" rounded_avg(7, 5) => "-1...
/* You are given two positive integers n and m, and your task is to compute the average of the integers from n through m (including n and m). Round the answer to the nearest integer(smaller one) and convert that to binary. If n is greater than m, return "-1". Example: rounded_avg(1, 5) => "11" rounded_avg(7, 5) => "-1...
/* You are given two positive integers n and m, and your task is to compute the average of the integers from n through m (including n and m). Round the answer to the nearest integer(smaller one) and convert that to binary. If n is greater than m, return "-1". Example: rounded_avg(1, 5) => "11" rounded_avg(7, 5) => "-1...
#undef NDEBUG #include<assert.h> int main(){ assert (rounded_avg(1, 5) == "11"); assert (rounded_avg(7, 13) == "1010"); assert (rounded_avg(964,977) == "1111001010"); assert (rounded_avg(996,997) == "1111100100"); assert (rounded_avg(560,851) == "1011000001"); assert (rounded_avg(185,546) == "...
fib
/* Return n-th Fibonacci number. >>> fib(10) 55 >>> fib(1) 1 >>> fib(8) 21 */ #include<stdio.h> using namespace std; int fib(int n){ int f[1000]; f[0]=0;f[1]=0; for (int i=2;i<=n; i++) f[i]=f[i-1]+f[i-2]; return f[n]; }
/* Return n-th Fibonacci number. >>> fib(10) 55 >>> fib(1) 1 >>> fib(8) 21 */ #include<stdio.h> using namespace std; int fib(int n){ int f[1000]; f[0]=0;f[1]=1; for (int i=2;i<=n; i++) f[i]=f[i-1]+f[i-2]; return f[n]; }
/* Return n-th Fibonacci number. >>> fib(10) 55 >>> fib(1) 1 >>> fib(8) 21 */ #include<stdio.h> using namespace std; int fib(int n){
#undef NDEBUG #include<assert.h> int main(){ assert (fib(10) == 55); assert (fib(1) == 1); assert (fib(8) == 21); assert (fib(11) == 89); assert (fib(12) == 144); assert (fib(10) == 55); assert (fib(1) == 1); assert (fib(8) == 21); assert (fib(11) == 89); assert (fib(12) == 144);...
filter_by_prefix
/* Filter an input vector of strings only for ones that start with a given prefix. >>> filter_by_prefix({}, "a") {} >>> filter_by_prefix({"abc", "bcd", "cde", "array"}, "a") {"abc", "array"} */ #include<stdio.h> #include<vector> #include<string> using namespace std; vector<string> filter_by_prefix(vector<string> string...
/* Filter an input vector of strings only for ones that start with a given prefix. >>> filter_by_prefix({}, "a") {} >>> filter_by_prefix({"abc", "bcd", "cde", "array"}, "a") {"abc", "array"} */ #include<stdio.h> #include<vector> #include<string> using namespace std; vector<string> filter_by_prefix(vector<string> string...
/* Filter an input array of strings only for ones that start with a given prefix. >>> filter_by_prefix({}, "a") {} >>> filter_by_prefix({"abc", "bcd", "cde", "vector"}, "a") {"abc", "vector"} */ #include<stdio.h> #include<vector> #include<string> using namespace std; vector<string> filter_by_prefix(vector<string> strin...
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(filter_by_prefix({}, "john") , {})); assert (issame(filter_by_pre...
triples_sum_to_zero
/* triples_sum_to_zero takes a vector of integers as an input. it returns true if there are three distinct elements in the vector that sum to zero, and false otherwise. >>> triples_sum_to_zero({1, 3, 5, 0}) false >>> triples_sum_to_zero({1, 3, -2, 1}) true >>> triples_sum_to_zero({1, 2, 3, 7}) false >>> triples_sum_to...
/* triples_sum_to_zero takes a vector of integers as an input. it returns true if there are three distinct elements in the vector that sum to zero, and false otherwise. >>> triples_sum_to_zero({1, 3, 5, 0}) false >>> triples_sum_to_zero({1, 3, -2, 1}) true >>> triples_sum_to_zero({1, 2, 3, 7}) false >>> triples_sum_to...
/* triples_sum_to_zero takes a vector of integers as an input. it returns true if there are three distinct elements in the vector that sum to zero, and false otherwise. >>> triples_sum_to_zero({1, 3, 5, 0}) false >>> triples_sum_to_zero({1, 3, -2, 1}) true >>> triples_sum_to_zero({1, 2, 3, 7}) false >>> triples_sum_to...
#undef NDEBUG #include<assert.h> int main(){ assert (triples_sum_to_zero({1, 3, 5, 0}) == false); assert (triples_sum_to_zero({1, 3, 5, -1}) == false); assert (triples_sum_to_zero({1, 3, -2, 1}) == true); assert (triples_sum_to_zero({1, 2, 3, 7}) == false); assert (triples_sum_to_zero({1, 2, 5, 7}) ...
sort_array2
/* Given a vector of non-negative integers, return a copy of the given vector after sorting, you will sort the given vector in ascending order if the sum( first index value, last index value) is odd, or sort it in descending order if the sum( first index value, last index value) is even. Note: * don't change the given...
/* Given a vector of non-negative integers, return a copy of the given vector after sorting, you will sort the given vector in ascending order if the sum( first index value, last index value) is odd, or sort it in descending order if the sum( first index value, last index value) is even. Note: * don't change the given...
/* Given a vector of non-negative integers, return a copy of the given vector after sorting, you will sort the given vector in ascending order if the sum( first index value, last index value) is odd, or sort it in descending order if the sum( first index value, last index value) is even. Note: * don't change the given...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(sort_array({}) , {})); assert (issame(sort_array({5}) , {5})); asse...
string_to_md5
/* Given a string 'text", return its md5 hash equivalent string. If 'text" is an empty string, return None. >>> string_to_md5("Hello world") == "3e25960a79dbc69b674cd4ec67a72c62" */ #include<stdio.h> #include<string> #include<openssl/md5.h> using namespace std; string string_to_md5(string text){ unsigned char md[1...
/* Given a string 'text", return its md5 hash equivalent string. If 'text" is an empty string, return None. >>> string_to_md5("Hello world") == "3e25960a79dbc69b674cd4ec67a72c62" */ #include<stdio.h> #include<string> #include<openssl/md5.h> using namespace std; string string_to_md5(string text){ unsigned char md[1...
/* Given a string 'text", return its md5 hash equivalent string. If 'text" is an empty string, return None. >>> string_to_md5("Hello world") == "3e25960a79dbc69b674cd4ec67a72c62" */ #include<stdio.h> #include<string> #include<openssl/md5.h> using namespace std; string string_to_md5(string text){
#undef NDEBUG #include<assert.h> int main(){ assert (string_to_md5("Hello world") == "3e25960a79dbc69b674cd4ec67a72c62"); assert (string_to_md5("") == "None"); assert (string_to_md5("A B C") == "0ef78513b0cb8cef12743f5aeb35f888"); assert (string_to_md5("password") == "5f4dcc3b5aa765d61d8327deb882cf99");...
total_match
/* Write a function that accepts two vectors of strings and returns the vector that has total number of chars in the all strings of the vector less than the other vector. if the two vectors have the same number of chars, return the first vector. Examples total_match({}, {}) ➞ {} total_match({"hi", "admin"}, {"hI", "...
/* Write a function that accepts two vectors of strings and returns the vector that has total number of chars in the all strings of the vector less than the other vector. if the two vectors have the same number of chars, return the first vector. Examples total_match({}, {}) ➞ {} total_match({"hi", "admin"}, {"hI", "...
/* Write a function that accepts two vectors of strings and returns the vector that has total number of chars in the all strings of the vector less than the other vector. if the two vectors have the same number of chars, return the first vector. Examples total_match({}, {}) ➞ {} total_match({"hi", "admin"}, {"hI", "...
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(total_match({}, {}) , {})); assert (issame(total_match({"hi", "ad...
words_string
/* 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", ...
/* 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", ...
/* 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", ...
#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...
make_a_pile
/* Given a positive integer n, you have to make a pile of n levels of stones. The first level has n stones. The number of stones in the next level is: - the next odd number if n is odd. - the next even number if n is even. Return the number of stones in each level in a vector, where element at index i represent...
/* Given a positive integer n, you have to make a pile of n levels of stones. The first level has n stones. The number of stones in the next level is: - the next odd number if n is odd. - the next even number if n is even. Return the number of stones in each level in a vector, where element at index i represent...
/* Given a positive integer n, you have to make a pile of n levels of stones. The first level has n stones. The number of stones in the next level is: - the next odd number if n is odd. - the next even number if n is even. Return the number of stones in each level in a vector, where element at index i represent...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(make_a_pile(3) , {3, 5, 7})); assert (issame(make_a_pile(4) , {4,6,8,10...
get_closest_vowel
/* You are given a word. Your task is to find the closest vowel that stands between two consonants from the right side of the word (case sensitive). Vowels in the beginning and ending doesn't count. Return empty string if you didn't find any vowel met the above condition. You may assume that the given string contai...
/* You are given a word. Your task is to find the closest vowel that stands between two consonants from the right side of the word (case sensitive). Vowels in the beginning and ending doesn't count. Return empty string if you didn't find any vowel met the above condition. You may assume that the given string contai...
/* You are given a word. Your task is to find the closest vowel that stands between two consonants from the right side of the word (case sensitive). Vowels in the beginning and ending doesn't count. Return empty string if you didn't find any vowel met the above condition. You may assume that the given string contai...
#undef NDEBUG #include<assert.h> int main(){ assert (get_closest_vowel("yogurt") == "u"); assert (get_closest_vowel("full") == "u"); assert (get_closest_vowel("easy") == ""); assert (get_closest_vowel("eAsy") == ""); assert (get_closest_vowel("ali") == ""); assert (get_closest_vowel("bad") == "a...
string_xor
/* Input are two strings a and b consisting only of 1s and 0s. Perform binary XOR on these inputs and return result also as a string. >>> string_xor("010", "110") "100" */ #include<stdio.h> #include<string> using namespace std; string string_xor(string a,string b){ string output=""; for (int i=0;(i<a.length() a...
/* Input are two strings a and b consisting only of 1s and 0s. Perform binary XOR on these inputs and return result also as a string. >>> string_xor("010", "110") "100" */ #include<stdio.h> #include<string> using namespace std; string string_xor(string a,string b){ string output=""; for (int i=0;(i<a.length() a...
/* Input are two strings a and b consisting only of 1s and 0s. Perform binary XOR on these inputs and return result also as a string. >>> string_xor("010", "110") "100" */ #include<stdio.h> #include<string> using namespace std; string string_xor(string a,string b){
#undef NDEBUG #include<assert.h> int main(){ assert (string_xor("111000", "101010") == "010010"); assert (string_xor("1", "1") == "0"); assert (string_xor("0101", "0000") == "0101"); assert(string_xor("111000", "101010") == "010010"); assert(string_xor("1", "1") == "0"); assert(string_xor("010...
eat
/* You"re a hungry rabbit, and you already have eaten a certain number of carrots, but now you need to eat more carrots to complete the day's meals. you should return a vector of { total number of eaten carrots after your meals, the number of carrots left after your meals } if there are ...
/* You"re a hungry rabbit, and you already have eaten a certain number of carrots, but now you need to eat more carrots to complete the day's meals. you should return a vector of { total number of eaten carrots after your meals, the number of carrots left after your meals } if there are ...
/* You"re a hungry rabbit, and you already have eaten a certain number of carrots, but now you need to eat more carrots to complete the day's meals. you should return a vector of { total number of eaten carrots after your meals, the number of carrots left after your meals } if there are ...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(eat(5, 6, 10) , {11, 4})); assert (issame(eat(4, 8, 9) , {12, 1})); ...
below_threshold
/* Return true if all numbers in the vector l are below threshold t. >>> below_threshold({1, 2, 4, 10}, 100) true >>> below_threshold({1, 20, 4, 10}, 5) false */ #include<stdio.h> #include<vector> using namespace std; bool below_threshold(vector<int>l, int t){ for (int i=0;i<l.size();i++) if (l[i]<=t) retur...
/* Return true if all numbers in the vector l are below threshold t. >>> below_threshold({1, 2, 4, 10}, 100) true >>> below_threshold({1, 20, 4, 10}, 5) false */ #include<stdio.h> #include<vector> using namespace std; bool below_threshold(vector<int>l, int t){ for (int i=0;i<l.size();i++) if (l[i]>=t) retur...
/* Return true if all numbers in the vector l are below threshold t. >>> below_threshold({1, 2, 4, 10}, 100) true >>> below_threshold({1, 20, 4, 10}, 5) false */ #include<stdio.h> #include<vector> using namespace std; bool below_threshold(vector<int>l, int t){
#undef NDEBUG #include<assert.h> int main(){ assert (below_threshold({1, 2, 4, 10}, 100)); assert (not(below_threshold({1, 20, 4, 10}, 5))); assert (below_threshold({1, 20, 4, 10}, 21)); assert (below_threshold({1, 20, 4, 10}, 22)); assert (below_threshold({1, 8, 4, 10}, 11)); assert (not(below_...
encrypt
/* 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" ...
/* 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" ...
/* 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" ...
#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...
is_bored
/* You'll be given a string of words, and your task is to count the number of boredoms. A boredom is a sentence that starts with the word "I". Sentences are delimited by '.', '?' or '!'. For example: >>> is_bored("Hello world") 0 >>> is_bored("The sky is blue. The sun is shining. I love this weather") 1 */ #include<st...
/* You'll be given a string of words, and your task is to count the number of boredoms. A boredom is a sentence that starts with the word "I". Sentences are delimited by '.', '?' or '!'. For example: >>> is_bored("Hello world") 0 >>> is_bored("The sky is blue. The sun is shining. I love this weather") 1 */ #include<st...
/* You'll be given a string of words, and your task is to count the number of boredoms. A boredom is a sentence that starts with the word "I". Sentences are delimited by '.', '?' or '!'. For example: >>> is_bored("Hello world") 0 >>> is_bored("The sky is blue. The sun is shining. I love this weather") 1 */ #include<st...
#undef NDEBUG #include<assert.h> int main(){ assert (is_bored("Hello world") == 0); assert (is_bored("Is the sky blue?") == 0); assert (is_bored("I love It !") == 1); assert (is_bored("bIt") == 0); assert (is_bored("I feel good today. I will be productive. will kill It") == 2); assert (is_bored(...
common
/* Return sorted unique common elements for two vectors. >>> common({1, 4, 3, 34, 653, 2, 5}, {5, 7, 1, 5, 9, 653, 121}) {1, 5, 653} >>> common({5, 3, 2, 8}, {3, 2}) {2, 3} */ #include<stdio.h> #include<vector> #include<algorithm> using namespace std; vector<int> common(vector<int> l1,vector<int> l2){ vector<int> ...
/* Return sorted unique common elements for two vectors. >>> common({1, 4, 3, 34, 653, 2, 5}, {5, 7, 1, 5, 9, 653, 121}) {1, 5, 653} >>> common({5, 3, 2, 8}, {3, 2}) {2, 3} */ #include<stdio.h> #include<vector> #include<algorithm> using namespace std; vector<int> common(vector<int> l1,vector<int> l2){ vector<int> ...
/* Return sorted unique common elements for two vectors. >>> common({1, 4, 3, 34, 653, 2, 5}, {5, 7, 1, 5, 9, 653, 121}) {1, 5, 653} >>> common({5, 3, 2, 8}, {3, 2}) {2, 3} */ #include<stdio.h> #include<vector> #include<algorithm> using namespace std; vector<int> common(vector<int> l1,vector<int> l2){
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(common({1, 4, 3, 34, 653, 2, 5}, {5, 7, 1, 5, 9, 653, 121}) , {1, 5, 65...
correct_bracketing2
/* 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...
/* 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...
/* 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...
#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...
skjkasdkd
/* 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...
/* 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...
/* 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...
#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...
median
/* Return median of elements in the vector l. >>> median({3, 1, 2, 4, 5}) 3 >>> median({-10, 4, 6, 1000, 10, 20}) 15.0 */ #include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; float median(vector<float> l){ sort(l.begin(),l.end()); if (l.size()%2==1) return l[l.size()/2]; ...
/* Return median of elements in the vector l. >>> median({3, 1, 2, 4, 5}) 3 >>> median({-10, 4, 6, 1000, 10, 20}) 15.0 */ #include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; float median(vector<float> l){ sort(l.begin(),l.end()); if (l.size()%2==1) return l[l.size()/2]; ...
/* Return 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){
#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 ); assert (abs(...
make_palindrome
#include<stdio.h> #include<string> using namespace std; bool is_palindrome(string str){ //Test if given string is a palindrome string s(str.rbegin(),str.rend()); return s==str; } string make_palindrome(string str){ /* Find the shortest palindrome that begins with a supplied string. Algorithm i...
#include<stdio.h> #include<string> using namespace std; bool is_palindrome(string str){ //Test if given string is a palindrome string s(str.rbegin(),str.rend()); return s==str; } string make_palindrome(string str){ /* Find the shortest palindrome that begins with a supplied string. Algorithm i...
#include<stdio.h> #include<string> using namespace std; bool is_palindrome(string str){ //Test if given string is a palindrome string s(str.rbegin(),str.rend()); return s==str; } string make_palindrome(string str){ /* Find the shortest palindrome that begins with a supplied string. Algorithm i...
#undef NDEBUG #include<assert.h> int main(){ assert (make_palindrome("") == ""); assert (make_palindrome("x") == "x"); assert (make_palindrome("xyz") == "xyzyx"); assert (make_palindrome("xyx") == "xyx") ; assert (make_palindrome("jerry") == "jerryrrej"); assert (make_palindrome("") == ""); ...
is_nested
/* Create a function that takes a string as input which contains only square brackets. The function should return true if and only if there is a valid subsequence of brackets where at least one bracket in the subsequence is nested. is_nested("[[]]") ➞ true is_nested("[]]]]]]][[[[[]") ➞ false is_nested("[][]") ➞ false ...
/* Create a function that takes a string as input which contains only square brackets. The function should return true if and only if there is a valid subsequence of brackets where at least one bracket in the subsequence is nested. is_nested("[[]]") ➞ true is_nested("[]]]]]]][[[[[]") ➞ false is_nested("[][]") ➞ false ...
/* Create a function that takes a string as input which contains only square brackets. The function should return true if and only if there is a valid subsequence of brackets where at least one bracket in the subsequence is nested. is_nested("[[]]") ➞ true is_nested("[]]]]]]][[[[[]") ➞ false is_nested("[][]") ➞ false ...
#undef NDEBUG #include<assert.h> int main(){ assert (is_nested("[[]]") == true); assert (is_nested("[]]]]]]][[[[[]") == false); assert (is_nested("[][]") == false); assert (is_nested(("[]")) == false); assert (is_nested("[[[[]]]]") == true); assert (is_nested("[]]]]]]]]]]") == false); assert...
parse_music
/* Input to this function is a string representing musical notes in a special ASCII format. Your task is to parse this string and return vector of integers corresponding to how many beats does each not last. Here is a legend: "o" - whole note, lasts four beats "o|" - half note, lasts two beats ".|" - quater note, last...
/* Input to this function is a string representing musical notes in a special ASCII format. Your task is to parse this string and return vector of integers corresponding to how many beats does each not last. Here is a legend: "o" - whole note, lasts four beats "o|" - half note, lasts two beats ".|" - quater note, last...
/* Input to this function is a string representing musical notes in a special ASCII format. Your task is to parse this string and return vector of integers corresponding to how many beats does each not last. Here is a legend: "o" - whole note, lasts four beats "o|" - half note, lasts two beats ".|" - quater note, last...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(parse_music("") , {})); assert (issame(parse_music("o o o o") ,{4,...
iscube
/* 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<...
/* 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<...
/* 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<...
#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); }
odd_count
/* Given a vector of strings, where each string consists of only digits, return a vector. Each element i of the output should be 'the number of odd elements in the string i of the input." where all the i's should be replaced by the number of odd digits in the i'th string of the input. >>> odd_count({"1234567"}) {'the ...
/* Given a vector of strings, where each string consists of only digits, return a vector. Each element i of the output should be 'the number of odd elements in the string i of the input." where all the i's should be replaced by the number of odd digits in the i'th string of the input. >>> odd_count({"1234567"}) {'the ...
/* Given a vector of strings, where each string consists of only digits, return a vector. Each element i of the output should be 'the number of odd elements in the string i of the input." where all the i's should be replaced by the number of odd digits in the i'th string of the input. >>> odd_count({"1234567"}) {'the ...
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(odd_count({"1234567"}) , {"the number of odd elements 4n the str4ng 4...
correct_bracketing
/* 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...
/* 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...
/* 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...
#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...
separate_paren_groups
/* 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. >>>...
/* 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. >>>...
/* 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. >>>...
#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("(()()) ((())) () ((())()())"),{"(()())", "((...
reverse_delete
/* Task We are given two strings s and c, you have to deleted all the characters in s that are equal to any character in c then check if the result string is palindrome. A string is called palindrome if it reads the same backward as forward. You should return a vector containing the result string and "True"/"False" for...
/* Task We are given two strings s and c, you have to deleted all the characters in s that are equal to any character in c then check if the result string is palindrome. A string is called palindrome if it reads the same backward as forward. You should return a vector containing the result string and "True"/"False" for...
/* Task We are given two strings s and c, you have to deleted all the characters in s that are equal to any character in c then check if the result string is palindrome. A string is called palindrome if it reads the same backward as forward. You should return a vector containing the result string and "True"/"False" for...
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(reverse_delete("abcde","ae") , {"bcd","False"})); assert (issame(...
unique
/* Return sorted unique elements in a vector >>> unique({5, 3, 5, 2, 3, 3, 9, 0, 123}) {0, 2, 3, 5, 9, 123} */ #include<stdio.h> #include<vector> #include<algorithm> using namespace std; vector<int> unique(vector<int> l){ vector<int> out={}; for (int i=0;i<l.size();i++) out.push_back(l[i]); sort(out...
/* Return sorted unique elements in a vector >>> unique({5, 3, 5, 2, 3, 3, 9, 0, 123}) {0, 2, 3, 5, 9, 123} */ #include<stdio.h> #include<vector> #include<algorithm> using namespace std; vector<int> unique(vector<int> l){ vector<int> out={}; for (int i=0;i<l.size();i++) if (find(out.begin(),out.end(),l[...
/* Return sorted unique elements in a vector >>> unique({5, 3, 5, 2, 3, 3, 9, 0, 123}) {0, 2, 3, 5, 9, 123} */ #include<stdio.h> #include<vector> #include<algorithm> using namespace std; vector<int> unique(vector<int> l){
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(unique({5, 3, 5, 2, 3, 3, 9, 0, 123}) , {0, 2, 3, 5, 9, 123})); ass...
is_equal_to_sum_even
/* Evaluate whether the given number n can be written as the sum of exactly 4 positive even numbers Example is_equal_to_sum_even(4) == false is_equal_to_sum_even(6) == false is_equal_to_sum_even(8) == true */ #include<stdio.h> using namespace std; bool is_equal_to_sum_even(int n){ if (n*2==1 ^ n<8) return true; ...
/* Evaluate whether the given number n can be written as the sum of exactly 4 positive even numbers Example is_equal_to_sum_even(4) == false is_equal_to_sum_even(6) == false is_equal_to_sum_even(8) == true */ #include<stdio.h> using namespace std; bool is_equal_to_sum_even(int n){ if (n%2==0 and n>=8) return true; ...
/* Evaluate whether the given number n can be written as the sum of exactly 4 positive even numbers Example is_equal_to_sum_even(4) == false is_equal_to_sum_even(6) == false is_equal_to_sum_even(8) == true */ #include<stdio.h> using namespace std; bool is_equal_to_sum_even(int n){
#undef NDEBUG #include<assert.h> int main(){ assert (is_equal_to_sum_even(4) == false); assert (is_equal_to_sum_even(6) == false); assert (is_equal_to_sum_even(8) == true); assert (is_equal_to_sum_even(10) == true); assert (is_equal_to_sum_even(11) == false); assert (is_equal_to_sum_even(12) == ...
minpath
/* 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...
/* 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...
/* 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...
#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...
count_distinct_characters
/* 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){ ...
/* 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){ ...
/* 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){ ...
#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...
get_row
/* You are given a 2 dimensional data, as a nested vectors, which is similar to matrix, however, unlike matrices, each row may contain a different number of columns. Given lst, and integer x, find integers x in the vector, and return vector of vectors, {{x1, y1}, {x2, y2} ...} such that each vector is a coordinate - {r...
/* You are given a 2 dimensional data, as a nested vectors, which is similar to matrix, however, unlike matrices, each row may contain a different number of columns. Given lst, and integer x, find integers x in the vector, and return vector of vectors, {{x1, y1}, {x2, y2} ...} such that each vector is a coordinate - {r...
/* You are given a 2 dimensional data, as a nested vectors, which is similar to matrix, however, unlike matrices, each row may contain a different number of columns. Given lst, and integer x, find integers x in the vector, and return vector of vectors, {{x1, y1}, {x2, y2} ...} such that each vector is a coordinate - {r...
#undef NDEBUG #include<assert.h> bool issame(vector<vector<int>> a,vector<vector<int>> b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i].size()!=b[i].size()) return false; for (int j=0;j<a[i].size();j++) if (a[i][j]!=b[i][j]) return false; } ...
smallest_change
/* Given a vector arr of integers, find the minimum number of elements that need to be changed to make the vector palindromic. A palindromic vector is a vector that is read the same backwards and forwards. In one change, you can change one element to any other element. For example: smallest_change({1,2,3,5,4,7,9,6}) =...
/* Given a vector arr of integers, find the minimum number of elements that need to be changed to make the vector palindromic. A palindromic vector is a vector that is read the same backwards and forwards. In one change, you can change one element to any other element. For example: smallest_change({1,2,3,5,4,7,9,6}) =...
/* Given a vector arr of integers, find the minimum number of elements that need to be changed to make the vector palindromic. A palindromic vector is a vector that is read the same backwards and forwards. In one change, you can change one element to any other element. For example: smallest_change({1,2,3,5,4,7,9,6}) =...
#undef NDEBUG #include<assert.h> int main(){ assert (smallest_change({1,2,3,5,4,7,9,6}) == 4); assert (smallest_change({1, 2, 3, 4, 3, 2, 2}) == 1); assert (smallest_change({1, 4, 2}) == 1); assert (smallest_change({1, 4, 4, 2}) == 1); assert (smallest_change({1, 2, 3, 2, 1}) == 0); assert (smal...
search
/* You are given a non-empty vector of positive integers. Return the greatest integer that is greater than zero, and has a frequency greater than or equal to the value of the integer itself. The frequency of an integer is the number of times it appears in the vector. If no such a value exist, return -1. Examples: ...
/* You are given a non-empty vector of positive integers. Return the greatest integer that is greater than zero, and has a frequency greater than or equal to the value of the integer itself. The frequency of an integer is the number of times it appears in the vector. If no such a value exist, return -1. Examples: ...
/* You are given a non-empty vector of positive integers. Return the greatest integer that is greater than zero, and has a frequency greater than or equal to the value of the integer itself. The frequency of an integer is the number of times it appears in the vector. If no such a value exist, return -1. Examples: ...
#undef NDEBUG #include<assert.h> int main(){ assert (search({5, 5, 5, 5, 1}) == 1); assert (search({4, 1, 4, 1, 4, 4}) == 4); assert (search({3, 3}) == -1); assert (search({8, 8, 8, 8, 8, 8, 8, 8}) == 8); assert (search({2, 3, 3, 2, 2}) == 2); assert (search({2, 7, 8, 8, 4, 8, 7, 3, 9, 6, 5, 10,...
is_simple_power
/* Your task is to write a function that returns true if a number x is a simple power of n and false in other cases. x is a simple power of n if n**int=x For example: is_simple_power(1, 4) => true is_simple_power(2, 2) => true is_simple_power(8, 2) => true is_simple_power(3, 2) => false is_simple_power(3, 1) => false i...
/* Your task is to write a function that returns true if a number x is a simple power of n and false in other cases. x is a simple power of n if n**int=x For example: is_simple_power(1, 4) => true is_simple_power(2, 2) => true is_simple_power(8, 2) => true is_simple_power(3, 2) => false is_simple_power(3, 1) => false i...
/* Your task is to write a function that returns true if a number x is a simple power of n and false in other cases. x is a simple power of n if n**int=x For example: is_simple_power(1, 4) => true is_simple_power(2, 2) => true is_simple_power(8, 2) => true is_simple_power(3, 2) => false is_simple_power(3, 1) => false i...
#undef NDEBUG #include<assert.h> int main(){ assert (is_simple_power(1, 4)== true); assert (is_simple_power(2, 2)==true); assert (is_simple_power(8, 2)==true); assert (is_simple_power(3, 2)==false); assert (is_simple_power(3, 1)==false); assert (is_simple_power(5, 3)==false); assert (is_simp...
strongest_extension
/* You will be given the name of a class (a string) and a vector of extensions. The extensions are to be used to load additional classes to the class. The strength of the extension is as follows: Let CAP be the number of the uppercase letters in the extension's name, and let SM be the number of lowercase letters in th...
/* You will be given the name of a class (a string) and a vector of extensions. The extensions are to be used to load additional classes to the class. The strength of the extension is as follows: Let CAP be the number of the uppercase letters in the extension's name, and let SM be the number of lowercase letters in th...
/* You will be given the name of a class (a string) and a vector of extensions. The extensions are to be used to load additional classes to the class. The strength of the extension is as follows: Let CAP be the number of the uppercase letters in the extension's name, and let SM be the number of lowercase letters in th...
#undef NDEBUG #include<assert.h> int main(){ assert (Strongest_Extension("Watashi", {"tEN", "niNE", "eIGHt8OKe"}) == "Watashi.eIGHt8OKe"); assert (Strongest_Extension("Boku123", {"nani", "NazeDa", "YEs.WeCaNe", "32145tggg"}) == "Boku123.YEs.WeCaNe"); assert (Strongest_Extension("__YESIMHERE", {"t", "eMptY",...
pluck
/* Given a vector representing a branch of a tree that has non-negative integer nodes your task is to pluck one of the nodes and return it. The plucked node should be the node with the smallest even value. If multiple nodes with the same smallest even value are found return the node that has smallest index. The plucke...
/* Given a vector representing a branch of a tree that has non-negative integer nodes your task is to pluck one of the nodes and return it. The plucked node should be the node with the smallest even value. If multiple nodes with the same smallest even value are found return the node that has smallest index. The plucke...
/* Given a vector representing a branch of a tree that has non-negative integer nodes your task is to pluck one of the nodes and return it. The plucked node should be the node with the smallest even value. If multiple nodes with the same smallest even value are found return the node that has smallest index. The plucke...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(pluck({4,2,3}) , {2, 1})); assert (issame(pluck({1,2,3}) , {2, 1}))...
next_smallest
/* You are given a vector of integers. Write a function next_smallest() that returns the 2nd smallest element of the vector. Return None if there is no such element. next_smallest({1, 2, 3, 4, 5}) == 2 next_smallest({5, 1, 4, 3, 2}) == 2 next_smallest({}) == None next_smallest({1, 1}) == None */ #include<stdio.h> #inc...
/* You are given a vector of integers. Write a function next_smallest() that returns the 2nd smallest element of the vector. Return None if there is no such element. next_smallest({1, 2, 3, 4, 5}) == 2 next_smallest({5, 1, 4, 3, 2}) == 2 next_smallest({}) == None next_smallest({1, 1}) == None */ #include<stdio.h> #inc...
/* You are given a vector of integers. Write a function next_smallest() that returns the 2nd smallest element of the vector. Return None if there is no such element. next_smallest({1, 2, 3, 4, 5}) == 2 next_smallest({5, 1, 4, 3, 2}) == 2 next_smallest({}) == None next_smallest({1, 1}) == None */ #include<stdio.h> #inc...
#undef NDEBUG #include<assert.h> int main(){ assert (next_smallest({1, 2, 3, 4, 5}) == 2); assert (next_smallest({5, 1, 4, 3, 2}) == 2); assert (next_smallest({}) == -1); assert (next_smallest({1, 1}) == -1); assert (next_smallest({1,1,1,1,0}) == 1); assert (next_smallest({-35, 34, 12, -45}) == ...