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 |
|---|---|---|---|---|
max_element | /*
Return maximum element in the vector.
>>> max_element({1, 2, 3})
3
>>> max_element({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10})
123
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
float max_element(vector<float> l){
float max=0;
for (int i=0;i<l.size();i++)
if (max<l[i]) max=l[i];
ret... | /*
Return maximum element in the vector.
>>> max_element({1, 2, 3})
3
>>> max_element({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10})
123
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
float max_element(vector<float> l){
float max=-10000;
for (int i=0;i<l.size();i++)
if (max<l[i]) max=l[i];
... | /*
Return maximum element in the vector.
>>> max_element({1, 2, 3})
3
>>> max_element({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10})
123
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
float max_element(vector<float> l){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (abs(max_element({1, 2, 3})- 3)<1e-4);
assert (abs(max_element({5, 3, -5, 2, -3, 3, 9, 0, 124, 1, -10})- 124)<1e-4);
assert(abs(max_element({1,2,3}) - 3) < 1e-4);
assert(abs(max_element({5,3,-5,2,-3,3,9,0,124,1,-10}) - 124) < 1e-4);
assert(abs(max_... |
has_close_elements | /*
Check if in given vector of numbers, are any two numbers closer to each other than
given threshold.
>>> has_close_elements({1.0, 2.0, 3.0}, 0.5)
false
>>> has_close_elements({1.0, 2.8, 3.0, 4.0, 5.0, 2.0}, 0.3)
true
*/
#include<stdio.h>
#include<vector>
#include<math.h>
using namespace std;
bool has_close_elements(v... | /*
Check if in given vector of numbers, are any two numbers closer to each other than
given threshold.
>>> has_close_elements({1.0, 2.0, 3.0}, 0.5)
false
>>> has_close_elements({1.0, 2.8, 3.0, 4.0, 5.0, 2.0}, 0.3)
true
*/
#include<stdio.h>
#include<vector>
#include<math.h>
using namespace std;
bool has_close_elements(v... | /*
Check if in given vector of numbers, are any two numbers closer to each other than
given threshold.
>>> has_close_elements({1.0, 2.0, 3.0}, 0.5)
false
>>> has_close_elements({1.0, 2.8, 3.0, 4.0, 5.0, 2.0}, 0.3)
true
*/
#include<stdio.h>
#include<vector>
#include<math.h>
using namespace std;
bool has_close_elements(v... | #undef NDEBUG
#include<assert.h>
int main(){
vector<float> a={1.0, 2.0, 3.9, 4.0, 5.0, 2.2};
assert (has_close_elements(a, 0.3)==true);
assert (has_close_elements(a, 0.05) == false);
assert (has_close_elements({1.0, 2.0, 5.9, 4.0, 5.0}, 0.95) == true);
assert (has_close_elements({1.0, 2.0, 5.9, 4.0... |
count_nums | /*
Write a function count_nums which takes a vector of integers and returns
the number of elements which has a sum of digits > 0.
If a number is negative, then its first signed digit will be negative:
e.g. -123 has signed digits -1, 2, and 3.
>>> count_nums({}) == 0
>>> count_nums({-1, 11, -11}) == 1
>>> count_nums({1,... | /*
Write a function count_nums which takes a vector of integers and returns
the number of elements which has a sum of digits > 0.
If a number is negative, then its first signed digit will be negative:
e.g. -123 has signed digits -1, 2, and 3.
>>> count_nums({}) == 0
>>> count_nums({-1, 11, -11}) == 1
>>> count_nums({1,... | /*
Write a function count_nums which takes a vector of integers and returns
the number of elements which has a sum of digits > 0.
If a number is negative, then its first signed digit will be negative:
e.g. -123 has signed digits -1, 2, and 3.
>>> count_nums({}) == 0
>>> count_nums({-1, 11, -11}) == 1
>>> count_nums({1,... | #undef NDEBUG
#include<assert.h>
int main(){
assert (count_nums({}) == 0);
assert (count_nums({-1, -2, 0}) == 0);
assert (count_nums({1, 1, 2, -2, 3, 4, 5}) == 6);
assert (count_nums({1, 6, 9, -6, 0, 1, 5}) == 5);
assert (count_nums({1, 100, 98, -7, 1, -1}) == 4);
assert (count_nums({12, 23, 34,... |
int_to_mini_roman | /*
Given a positive integer, obtain its roman numeral equivalent as a string,
and return it in lowercase.
Restrictions: 1 <= num <= 1000
Examples:
>>> int_to_mini_roman(19) == "xix"
>>> int_to_mini_roman(152) == "clii"
>>> int_to_mini_roman(426) == "cdxxvi"
*/
#include<stdio.h>
#include<vector>
#include<string>
using ... | /*
Given a positive integer, obtain its roman numeral equivalent as a string,
and return it in lowercase.
Restrictions: 1 <= num <= 1000
Examples:
>>> int_to_mini_roman(19) == "xix"
>>> int_to_mini_roman(152) == "clii"
>>> int_to_mini_roman(426) == "cdxxvi"
*/
#include<stdio.h>
#include<vector>
#include<string>
using ... | /*
Given a positive integer, obtain its roman numeral equivalent as a string,
and return it in lowercase.
Restrictions: 1 <= num <= 1000
Examples:
>>> int_to_mini_roman(19) == "xix"
>>> int_to_mini_roman(152) == "clii"
>>> int_to_mini_roman(426) == "cdxxvi"
*/
#include<stdio.h>
#include<vector>
#include<string>
using ... | #undef NDEBUG
#include<assert.h>
int main(){
assert (int_to_mini_romank(19) == "xix");
assert (int_to_mini_romank(152) == "clii");
assert (int_to_mini_romank(251) == "ccli");
assert (int_to_mini_romank(426) == "cdxxvi");
assert (int_to_mini_romank(500) == "d");
assert (int_to_mini_romank(1) == "... |
solve2 | /*
Given a positive integer N, return the total sum of its digits in binary.
Example
For N = 1000, the sum of digits will be 1 the output should be "1".
For N = 150, the sum of digits will be 6 the output should be "110".
For N = 147, the sum of digits will be 12 the output should be "1100".
Variables:
... | /*
Given a positive integer N, return the total sum of its digits in binary.
Example
For N = 1000, the sum of digits will be 1 the output should be "1".
For N = 150, the sum of digits will be 6 the output should be "110".
For N = 147, the sum of digits will be 12 the output should be "1100".
Variables:
... | /*
Given a positive integer N, return the total sum of its digits in binary.
Example
For N = 1000, the sum of digits will be 1 the output should be "1".
For N = 150, the sum of digits will be 6 the output should be "110".
For N = 147, the sum of digits will be 12 the output should be "1100".
Variables:
... | #undef NDEBUG
#include<assert.h>
int main(){
assert (solve(1000) == "1");
assert (solve(150) == "110");
assert (solve(147) == "1100");
assert (solve(333) == "1001");
assert (solve(963) == "10010");
assert (solve(1000) == "1");
assert (solve(150) == "110");
assert (solve(147) == "1100");
... |
monotonic | /*
Return true is vector elements are monotonically increasing or decreasing.
>>> monotonic({1, 2, 4, 20})
true
>>> monotonic({1, 20, 4, 10})
false
>>> monotonic({4, 1, 0, -10})
true
*/
#include<stdio.h>
#include<vector>
using namespace std;
bool monotonic(vector<float> l){
int incr,decr;
incr=0;decr=0;
for... | /*
Return true is vector elements are monotonically increasing or decreasing.
>>> monotonic({1, 2, 4, 20})
true
>>> monotonic({1, 20, 4, 10})
false
>>> monotonic({4, 1, 0, -10})
true
*/
#include<stdio.h>
#include<vector>
using namespace std;
bool monotonic(vector<float> l){
int incr,decr;
incr=0;decr=0;
for... | /*
Return true is vector elements are monotonically increasing or decreasing.
>>> monotonic({1, 2, 4, 20})
true
>>> monotonic({1, 20, 4, 10})
false
>>> monotonic({4, 1, 0, -10})
true
*/
#include<stdio.h>
#include<vector>
using namespace std;
bool monotonic(vector<float> l){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (monotonic({1, 2, 4, 10}) == true);
assert (monotonic({1, 2, 4, 20}) == true);
assert (monotonic({1, 20, 4, 10}) == false);
assert (monotonic({4, 1, 0, -10}) == true);
assert (monotonic({4, 1, 1, 0}) == true);
assert (monotonic({1, 2, 3, 2, 5, ... |
pairs_sum_to_zero | /*
pairs_sum_to_zero takes a vector of integers as an input.
it returns true if there are two distinct elements in the vector that
sum to zero, and false otherwise.
>>> pairs_sum_to_zero({1, 3, 5, 0})
false
>>> pairs_sum_to_zero({1, 3, -2, 1})
false
>>> pairs_sum_to_zero({1, 2, 3, 7})
false
>>> pairs_sum_to_zero({2, 4,... | /*
pairs_sum_to_zero takes a vector of integers as an input.
it returns true if there are two distinct elements in the vector that
sum to zero, and false otherwise.
>>> pairs_sum_to_zero({1, 3, 5, 0})
false
>>> pairs_sum_to_zero({1, 3, -2, 1})
false
>>> pairs_sum_to_zero({1, 2, 3, 7})
false
>>> pairs_sum_to_zero({2, 4,... | /*
pairs_sum_to_zero takes a vector of integers as an input.
it returns true if there are two distinct elements in the vector that
sum to zero, and false otherwise.
>>> pairs_sum_to_zero({1, 3, 5, 0})
false
>>> pairs_sum_to_zero({1, 3, -2, 1})
false
>>> pairs_sum_to_zero({1, 2, 3, 7})
false
>>> pairs_sum_to_zero({2, 4,... | #undef NDEBUG
#include<assert.h>
int main(){
assert (pairs_sum_to_zero({1, 3, 5, 0}) == false);
assert (pairs_sum_to_zero({1, 3, -2, 1}) == false);
assert (pairs_sum_to_zero({1, 2, 3, 7}) == false);
assert (pairs_sum_to_zero({2, 4, -5, 3, 5, 7}) == true);
assert (pairs_sum_to_zero({1}) == false);
... |
prime_length | /*
Write a function that takes a string and returns true if the string
length is a prime number or false otherwise
Examples
prime_length("Hello") == true
prime_length("abcdcba") == true
prime_length("kittens") == true
prime_length("orange") == false
*/
#include<stdio.h>
#include<string>
using namespace std;
bool prime_... | /*
Write a function that takes a string and returns true if the string
length is a prime number or false otherwise
Examples
prime_length("Hello") == true
prime_length("abcdcba") == true
prime_length("kittens") == true
prime_length("orange") == false
*/
#include<stdio.h>
#include<string>
using namespace std;
bool prime_... | /*
Write a function that takes a string and returns true if the string
length is a prime number or false otherwise
Examples
prime_length("Hello") == true
prime_length("abcdcba") == true
prime_length("kittens") == true
prime_length("orange") == false
*/
#include<stdio.h>
#include<string>
using namespace std;
bool prime_... | #undef NDEBUG
#include<assert.h>
int main(){
assert (prime_length("Hello") == true);
assert (prime_length("abcdcba") == true);
assert (prime_length("kittens") == true);
assert (prime_length("orange") == false);
assert (prime_length("wow") == true);
assert (prime_length("world") == true);
ass... |
sum_to_n | /*
sum_to_n is a function that sums numbers from 1 to n.
>>> sum_to_n(30)
465
>>> sum_to_n(100)
5050
>>> sum_to_n(5)
15
>>> sum_to_n(10)
55
>>> sum_to_n(1)
1
*/
#include<stdio.h>
using namespace std;
int sum_to_n(int n){
return n*((n+1)/2);
}
| /*
sum_to_n is a function that sums numbers from 1 to n.
>>> sum_to_n(30)
465
>>> sum_to_n(100)
5050
>>> sum_to_n(5)
15
>>> sum_to_n(10)
55
>>> sum_to_n(1)
1
*/
#include<stdio.h>
using namespace std;
int sum_to_n(int n){
return n*(n+1)/2;
}
| /*
sum_to_n is a function that sums numbers from 1 to n.
>>> sum_to_n(30)
465
>>> sum_to_n(100)
5050
>>> sum_to_n(5)
15
>>> sum_to_n(10)
55
>>> sum_to_n(1)
1
*/
#include<stdio.h>
using namespace std;
int sum_to_n(int n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (sum_to_n(1) == 1);
assert (sum_to_n(6) == 21);
assert (sum_to_n(11) == 66);
assert (sum_to_n(30) == 465);
assert (sum_to_n(100) == 5050);
assert(sum_to_n(1) == 1);
assert(sum_to_n(6) == 21);
assert(sum_to_n(11) == 66);
assert(sum_t... |
all_prefixes | /*
Return vector of all prefixes from shortest to longest of the input string
>>> all_prefixes("abc")
{"a", "ab", "abc"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> all_prefixes(string str){
vector<string> out;
string current="";
for (int i=0;i<str.length();i++... | /*
Return vector of all prefixes from shortest to longest of the input string
>>> all_prefixes("abc")
{"a", "ab", "abc"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> all_prefixes(string str){
vector<string> out;
string current="";
for (int i=0;i<str.length();i++... | /*
Return vector of all prefixes from shortest to longest of the input string
>>> all_prefixes("abc")
{"a", "ab", "abc"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> all_prefixes(string str){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(all_prefixes(""),{}));
assert (issame(all_prefixes("asdfgh") ,{"a... |
closest_integer | /*
Create a function that takes a value (string) representing a number
and returns the closest integer to it. If the number is equidistant
from two integers, round it away from zero.
Examples
>>> closest_integer("10")
10
>>> closest_integer("15.3")
15
Note:
Rounding away from zero means that if the given number is eq... | /*
Create a function that takes a value (string) representing a number
and returns the closest integer to it. If the number is equidistant
from two integers, round it away from zero.
Examples
>>> closest_integer("10")
10
>>> closest_integer("15.3")
15
Note:
Rounding away from zero means that if the given number is eq... | /*
Create a function that takes a value (string) representing a number
and returns the closest integer to it. If the number is equidistant
from two integers, round it away from zero.
Examples
>>> closest_integer("10")
10
>>> closest_integer("15.3")
15
Note:
Rounding away from zero means that if the given number is eq... | #undef NDEBUG
#include<assert.h>
int main(){
assert (closest_integer("10") == 10);
assert (closest_integer("14.5") == 15);
assert (closest_integer("-15.5") == -16);
assert (closest_integer("15.3") == 15);
assert (closest_integer("0") == 0);
assert (closest_integer("10") == 10);
assert (close... |
circular_shift | /*
Circular shift the digits of the integer x, shift the digits right by shift
and return the result as a string.
If shift > number of digits, return digits reversed.
>>> circular_shift(12, 1)
"21"
>>> circular_shift(12, 2)
"12"
*/
#include<stdio.h>
#include<string>
using namespace std;
string circular_shift(int x,int ... | /*
Circular shift the digits of the integer x, shift the digits right by shift
and return the result as a string.
If shift > number of digits, return digits reversed.
>>> circular_shift(12, 1)
"21"
>>> circular_shift(12, 2)
"12"
*/
#include<stdio.h>
#include<string>
using namespace std;
string circular_shift(int x,int ... | /*
Circular shift the digits of the integer x, shift the digits right by shift
and return the result as a string.
If shift > number of digits, return digits reversed.
>>> circular_shift(12, 1)
"21"
>>> circular_shift(12, 2)
"12"
*/
#include<stdio.h>
#include<string>
using namespace std;
string circular_shift(int x,int ... | #undef NDEBUG
#include<assert.h>
int main(){
assert (circular_shift(100, 2) == "001");
assert (circular_shift(12, 2) == "12");
assert (circular_shift(97, 8) == "79");
assert (circular_shift(12, 1) == "21");
assert (circular_shift(11, 101) == "11");
assert (circular_shift(100, 2) == "001");
a... |
find_max | /*
Write a function that accepts a vector of strings.
The vector contains different words. Return the word with maximum number
of unique characters. If multiple strings have maximum number of unique
characters, return the one which comes first in lexicographical order.
find_max({"name", "of", 'string"}) == 'string"
fi... | /*
Write a function that accepts a vector of strings.
The vector contains different words. Return the word with maximum number
of unique characters. If multiple strings have maximum number of unique
characters, return the one which comes first in lexicographical order.
find_max({"name", "of", 'string"}) == 'string"
fi... | /*
Write a function that accepts a vector of strings.
The vector contains different words. Return the word with maximum number
of unique characters. If multiple strings have maximum number of unique
characters, return the one which comes first in lexicographical order.
find_max({"name", "of", 'string"}) == 'string"
fi... | #undef NDEBUG
#include<assert.h>
int main(){
assert ((find_max({"name", "of", "string"}) == "string"));
assert ((find_max({"name", "enam", "game"}) == "enam"));
assert ((find_max({"aaaaaaa", "bb", "cc"}) == "aaaaaaa"));
assert ((find_max({"abc", "cba"}) == "abc"));
assert ((find_max({"play", "this",... |
string_sequence | /*
Return a string containing space-delimited numbers starting from 0 upto n inclusive.
>>> string_sequence(0)
"0"
>>> string_sequence(5)
"0 1 2 3 4 5"
*/
#include<stdio.h>
#include<string>
using namespace std;
string string_sequence(int n){
string out="";
for (int i=0;i<=n;i++)
out=out+" "+to_string(i);
... | /*
Return a string containing space-delimited numbers starting from 0 upto n inclusive.
>>> string_sequence(0)
"0"
>>> string_sequence(5)
"0 1 2 3 4 5"
*/
#include<stdio.h>
#include<string>
using namespace std;
string string_sequence(int n){
string out="0";
for (int i=1;i<=n;i++)
out=out+" "+to_string(i);
... | /*
Return a string containing space-delimited numbers starting from 0 upto n inclusive.
>>> string_sequence(0)
"0"
>>> string_sequence(5)
"0 1 2 3 4 5"
*/
#include<stdio.h>
#include<string>
using namespace std;
string string_sequence(int n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (string_sequence(0) == "0");
assert (string_sequence(3) == "0 1 2 3");
assert (string_sequence(10) == "0 1 2 3 4 5 6 7 8 9 10");
assert(string_sequence(0) == "0");
assert(string_sequence(3) == "0 1 2 3");
assert(string_sequence(10) == "0 1 2 3... |
special_factorial | /*
The Brazilian factorial is defined as:
brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1!
where n > 0
For example:
>>> special_factorial(4)
288
The function will receive an integer as input and should return the special
factorial of this integer.
*/
#include<stdio.h>
using namespace std;
long long special_fa... | /*
The Brazilian factorial is defined as:
brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1!
where n > 0
For example:
>>> special_factorial(4)
288
The function will receive an integer as input and should return the special
factorial of this integer.
*/
#include<stdio.h>
using namespace std;
long long special_fa... | /*
The Brazilian factorial is defined as:
brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1!
where n > 0
For example:
>>> special_factorial(4)
288
The function will receive an integer as input and should return the special
factorial of this integer.
*/
#include<stdio.h>
using namespace std;
long long special_fa... | #undef NDEBUG
#include<assert.h>
int main(){
assert (special_factorial(4) == 288);
assert (special_factorial(5) == 34560);
assert (special_factorial(7) == 125411328000);
assert (special_factorial(1) == 1);
assert (special_factorial(4) == 288);
assert (special_factorial(5) == 34560);
assert (... |
count_upper | /*
Given a string s, count the number of uppercase vowels in even indices.
For example:
count_upper("aBCdEf") returns 1
count_upper("abcdefg") returns 0
count_upper("dBBE") returns 0
*/
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
int count_upper(string s){
string uvowel="AeIou";
... | /*
Given a string s, count the number of uppercase vowels in even indices.
For example:
count_upper("aBCdEf") returns 1
count_upper("abcdefg") returns 0
count_upper("dBBE") returns 0
*/
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
int count_upper(string s){
string uvowel="AEIOU";
... | /*
Given a string s, count the number of uppercase vowels in even indices.
For example:
count_upper("aBCdEf") returns 1
count_upper("abcdefg") returns 0
count_upper("dBBE") returns 0
*/
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
int count_upper(string s){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (count_upper("aBCdEf") == 1);
assert (count_upper("abcdefg") == 0);
assert (count_upper("dBBE") == 0);
assert (count_upper("B") == 0);
assert (count_upper("U") == 1);
assert (count_upper("") == 0);
assert (count_upper("EEEE") == 2);
... |
truncate_number | /*
Given a positive floating point number, it can be decomposed into
and integer part (largest integer smaller than given number) and decimals
(leftover part always smaller than 1).
Return the decimal part of the number.
>>> truncate_number(3.5)
0.5
*/
#include<stdio.h>
#include<math.h>
using namespace std;
float trun... | /*
Given a positive floating point number, it can be decomposed into
and integer part (largest integer smaller than given number) and decimals
(leftover part always smaller than 1).
Return the decimal part of the number.
>>> truncate_number(3.5)
0.5
*/
#include<stdio.h>
#include<math.h>
using namespace std;
double tru... | /*
Given a positive floating point number, it can be decomposed into
and integer part (largest integer smaller than given number) and decimals
(leftover part always smaller than 1).
Return the decimal part of the number.
>>> truncate_number(3.5)
0.5
*/
#include<stdio.h>
#include<math.h>
using namespace std;
double tru... | #undef NDEBUG
#include<assert.h>
int main(){
assert (truncate_number(3.5) == 0.5);
assert (abs(truncate_number(1.33) - 0.33) < 1e-4);
assert (abs(truncate_number(123.456) - 0.456) < 1e-4);
assert(abs(truncate_number(3.5) - 0.5) < 1e-4);
assert(abs(truncate_number(1.33) - 0.33000000000000007) < 1e-4);
a... |
histogram | /*
Given a string representing a space separated lowercase letters, return a map
of the letter with the most repetition and containing the corresponding count.
If several letters have the same occurrence, return all of them.
Example:
histogram("a b c") == {{"a", 1}, {"b", 1}, {"c", 1}}
histogram("a b b a") == {{"a", 2... | /*
Given a string representing a space separated lowercase letters, return a map
of the letter with the most repetition and containing the corresponding count.
If several letters have the same occurrence, return all of them.
Example:
histogram("a b c") == {{"a", 1}, {"b", 1}, {"c", 1}}
histogram("a b b a") == {{"a", 2... | /*
Given a string representing a space separated lowercase letters, return a map
of the letter with the most repetition and containing the corresponding count.
If several letters have the same occurrence, return all of them.
Example:
histogram("a b c") == {{"a", 1}, {"b", 1}, {"c", 1}}
histogram("a b b a") == {{"a", 2... | #undef NDEBUG
#include<assert.h>
bool issame(map<char,int> a,map<char,int> b){
if (a.size()!=b.size()) return false;
map <char,int>::iterator it;
for (it=a.begin();it!=a.end();it++)
{
char w1=it->first;
int w2=it->second;
if (b.find(w1)==b.end()) return false;
if (b[w1]!=... |
sum_product | /*
For a given vector of integers, return a vector consisting of a sum and a product of all the integers in a vector.
Empty sum should be equal to 0 and empty product should be equal to 1.
>>> sum_product({})
(0, 1)
>>> sum_product({1, 2, 3, 4})
(10, 24)
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector... | /*
For a given vector of integers, return a vector consisting of a sum and a product of all the integers in a vector.
Empty sum should be equal to 0 and empty product should be equal to 1.
>>> sum_product({})
(0, 1)
>>> sum_product({1, 2, 3, 4})
(10, 24)
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector... | /*
For a given vector of integers, return a vector consisting of a sum and a product of all the integers in a vector.
Empty sum should be equal to 0 and empty product should be equal to 1.
>>> sum_product({})
(0, 1)
>>> sum_product({1, 2, 3, 4})
(10, 24)
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector... | #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(sum_product({}) ,{0, 1}));
assert (issame(sum_product({1, 1, 1}), {... |
decimal_to_binary | /*
You will be given a number in decimal form and your task is to convert it to
binary format. The function should return a string, with each character representing a binary
number. Each character in the string will be '0' or '1'.
There will be an extra couple of characters "db" at the beginning and at the end of the ... | /*
You will be given a number in decimal form and your task is to convert it to
binary format. The function should return a string, with each character representing a binary
number. Each character in the string will be '0' or '1'.
There will be an extra couple of characters "db" at the beginning and at the end of the ... | /*
You will be given a number in decimal form and your task is to convert it to
binary format. The function should return a string, with each character representing a binary
number. Each character in the string will be '0' or '1'.
There will be an extra couple of characters "db" at the beginning and at the end of the ... | #undef NDEBUG
#include<assert.h>
int main(){
assert (decimal_to_binary(0) == "db0db");
assert (decimal_to_binary(32) == "db100000db");
assert (decimal_to_binary(103) == "db1100111db");
assert (decimal_to_binary(15) == "db1111db");
assert (decimal_to_binary(0) == "db0db");
assert (decimal_to_bin... |
sort_numbers | /*
Input is a space-delimited string of numberals from "zero" to "nine".
Valid choices are "zero", "one", 'two", 'three", "four", "five", 'six", 'seven", "eight" and "nine".
Return the string with numbers sorted from smallest to largest
>>> sort_numbers('three one five")
"one three five"
*/
#include<stdio.h>
#include<s... | /*
Input is a space-delimited string of numberals from "zero" to "nine".
Valid choices are "zero", "one", 'two", 'three", "four", "five", 'six", 'seven", "eight" and "nine".
Return the string with numbers sorted from smallest to largest
>>> sort_numbers('three one five")
"one three five"
*/
#include<stdio.h>
#include<s... | /*
Input is a space-delimited string of numberals from "zero" to "nine".
Valid choices are "zero", "one", 'two", 'three", "four", "five", 'six", 'seven", "eight" and "nine".
Return the string with numbers sorted from smallest to largest
>>> sort_numbers('three one five")
"one three five"
*/
#include<stdio.h>
#include<s... | #undef NDEBUG
#include<assert.h>
int main(){
assert (sort_numbers("") == "");
assert (sort_numbers("three") == "three");
assert (sort_numbers("three five nine") == "three five nine");
assert (sort_numbers("five zero four seven nine eight") == "zero four five seven eight nine");
assert (sort_numbe... |
solve | /*
You are given a string s.
if s[i] is a letter, reverse its case from lower to upper or vise versa,
otherwise keep it as it is.
If the string contains no letters, reverse the string.
The function should return the resulted string.
Examples
solve("1234") = "4321"
solve("ab") = "AB"
solve("#a@C") = "#A@c"
*/
#include<... | /*
You are given a string s.
if s[i] is a letter, reverse its case from lower to upper or vise versa,
otherwise keep it as it is.
If the string contains no letters, reverse the string.
The function should return the resulted string.
Examples
solve("1234") = "4321"
solve("ab") = "AB"
solve("#a@C") = "#A@c"
*/
#include<... | /*
You are given a string s.
if s[i] is a letter, reverse its case from lower to upper or vise versa,
otherwise keep it as it is.
If the string contains no letters, reverse the string.
The function should return the resulted string.
Examples
solve("1234") = "4321"
solve("ab") = "AB"
solve("#a@C") = "#A@c"
*/
#include<... | #undef NDEBUG
#include<assert.h>
int main(){
assert (solve("AsDf") == "aSdF");
assert (solve("1234") == "4321");
assert (solve("ab") == "AB");
assert (solve("#a@C") == "#A@c");
assert (solve("#AsdfW^45") == "#aSDFw^45");
assert (solve("#6@2") == "2@6#");
assert (solve("#$a^D") == "#$A^d");
... |
f | /*
Implement the function f that takes n as a parameter,
and returns a vector of size n, such that the value of the element at index i is the factorial of i if i is even
or the sum of numbers from 1 to i otherwise.
i starts from 1.
the factorial of i is the multiplication of the numbers from 1 to i (1 * 2 * ... * i).
E... | /*
Implement the function f that takes n as a parameter,
and returns a vector of size n, such that the value of the element at index i is the factorial of i if i is even
or the sum of numbers from 1 to i otherwise.
i starts from 1.
the factorial of i is the multiplication of the numbers from 1 to i (1 * 2 * ... * i).
E... | /*
Implement the function f that takes n as a parameter,
and returns a vector of size n, such that the value of the element at index i is the factorial of i if i is even
or the sum of numbers from 1 to i otherwise.
i starts from 1.
the factorial of i is the multiplication of the numbers from 1 to i (1 * 2 * ... * i).
E... | #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(f(5) , {1, 2, 6, 24, 15}));
assert (issame(f(7) , {1, 2, 6, 24, 15, 720... |
fix_spaces | /*
Given a string text, replace all spaces in it with underscores,
and if a string has more than 2 consecutive spaces,
then replace all consecutive spaces with -
fix_spaces("Example") == "Example"
fix_spaces("Example 1") == "Example_1"
fix_spaces(" Example 2") == "_Example_2"
fix_spaces(" Example 3") == "_Example... | /*
Given a string text, replace all spaces in it with underscores,
and if a string has more than 2 consecutive spaces,
then replace all consecutive spaces with -
fix_spaces("Example") == "Example"
fix_spaces("Example 1") == "Example_1"
fix_spaces(" Example 2") == "_Example_2"
fix_spaces(" Example 3") == "_Example... | /*
Given a string text, replace all spaces in it with underscores,
and if a string has more than 2 consecutive spaces,
then replace all consecutive spaces with -
fix_spaces("Example") == "Example"
fix_spaces("Example 1") == "Example_1"
fix_spaces(" Example 2") == "_Example_2"
fix_spaces(" Example 3") == "_Example... | #undef NDEBUG
#include<assert.h>
int main(){
assert (fix_spaces("Example") == "Example");
assert (fix_spaces("Mudasir Hanif ") == "Mudasir_Hanif_");
assert (fix_spaces("Yellow Yellow Dirty Fellow") == "Yellow_Yellow__Dirty__Fellow");
assert (fix_spaces("Exa mple") == "Exa-mple");
assert (fix_spa... |
decode_shift | #include<stdio.h>
#include<string>
using namespace std;
string encode_shift(string s){
// returns encoded string by shifting every character by 5 in the alphabet.
string out;
int i;
for (i=1;i<s.length();i++)
{
int w=((int)s[i]+5-(int)'a')%26+(int)'a';
out=out+(char)w;
}
r... | #include<stdio.h>
#include<string>
using namespace std;
string encode_shift(string s){
// returns encoded string by shifting every character by 5 in the alphabet.
string out;
int i;
for (i=0;i<s.length();i++)
{
int w=((int)s[i]+5-(int)'a')%26+(int)'a';
out=out+(char)w;
}
r... | #include<stdio.h>
#include<string>
using namespace std;
string encode_shift(string s){
// returns encoded string by shifting every character by 5 in the alphabet.
string out;
int i;
for (i=0;i<s.length();i++)
{
int w=((int)s[i]+5-(int)'a')%26+(int)'a';
out=out+(char)w;
}
r... | #undef NDEBUG
#include<assert.h>
int main(){
for (int i=0;i<100;i++)
{
int l=10+rand()%11;
string str="";
for (int j=0;j<l;j++)
{
char chr=97+rand()%26;
str+=chr;
}
string encoded_str = encode_shift(str);
assert (decode_shift(... |
mean_absolute_deviation | /*
For a given vector of input numbers, calculate Mean Absolute Deviation
around the mean of this dataset.
Mean Absolute Deviation is the average absolute difference between each
element and a centerpoint (mean in this case):
MAD = average | x - x_mean |
>>> mean_absolute_deviation({1.0, 2.0, 3.0, 4.0})
1.0
*/
#include... | /*
For a given vector of input numbers, calculate Mean Absolute Deviation
around the mean of this dataset.
Mean Absolute Deviation is the average absolute difference between each
element and a centerpoint (mean in this case):
MAD = average | x - x_mean |
>>> mean_absolute_deviation({1.0, 2.0, 3.0, 4.0})
1.0
*/
#include... | /*
For a given vector of input numbers, calculate Mean Absolute Deviation
around the mean of this dataset.
Mean Absolute Deviation is the average absolute difference between each
element and a centerpoint (mean in this case):
MAD = average | x - x_mean |
>>> mean_absolute_deviation({1.0, 2.0, 3.0, 4.0})
1.0
*/
#include... | #undef NDEBUG
#include<assert.h>
int main(){
assert (abs(mean_absolute_deviation({1.0, 2.0, 3.0}) - 2.0/3.0) < 1e-4);
assert (abs(mean_absolute_deviation({1.0, 2.0, 3.0, 4.0}) - 1.0) < 1e-4);
assert (abs(mean_absolute_deviation({1.0, 2.0, 3.0, 4.0, 5.0}) - 6.0/5.0) < 1e-4);
assert (abs(mean_absolute_dev... |
choose_num | /*
This function takes two positive numbers x and y and returns the largest even integer number that is in the range [x, y] inclusive. If there's no such number, then the function should return -1.
For example:
choose_num(12, 15) = 14
choose_num(1, 21) = 20
choose_num(13, 12) = -1
*/
#include<stdio.h>
using namespace ... | /*
This function takes two positive numbers x and y and returns the largest even integer number that is in the range [x, y] inclusive. If there's no such number, then the function should return -1.
For example:
choose_num(12, 15) = 14
choose_num(1, 21) = 20
choose_num(13, 12) = -1
*/
#include<stdio.h>
using namespace ... | /*
This function takes two positive numbers x and y and returns the
biggest even integer number that is in the range [x, y] inclusive. If
there's no such number, then the function should return -1.
For example:
choose_num(12, 15) = 14
choose_num(13, 12) = -1
*/
#include<stdio.h>
using namespace std;
int choose_num(in... | #undef NDEBUG
#include<assert.h>
int main(){
assert (choose_num(12, 15) == 14);
assert (choose_num(13, 12) == -1);
assert (choose_num(33, 12354) == 12354);
assert (choose_num(5234, 5233) == -1);
assert (choose_num(6, 29) == 28);
assert (choose_num(27, 10) == -1);
assert (choose_num(7, 7) == ... |
minsubarraysum | /*
Given a vector of integers nums, find the minimum sum of any non-empty sub-vector
of nums.
Example
minSubArraySum({2, 3, 4, 1, 2, 4}) == 1
minSubArraySum({-1, -2, -3}) == -6
*/
#include<stdio.h>
#include<vector>
using namespace std;
long long minSubArraySum(vector<long long> nums){
long long current,min;
cur... | /*
Given a vector of integers nums, find the minimum sum of any non-empty sub-vector
of nums.
Example
minSubArraySum({2, 3, 4, 1, 2, 4}) == 1
minSubArraySum({-1, -2, -3}) == -6
*/
#include<stdio.h>
#include<vector>
using namespace std;
long long minSubArraySum(vector<long long> nums){
long long current,min;
cur... | /*
Given a vector of integers nums, find the minimum sum of any non-empty sub-vector
of nums.
Example
minSubArraySum({2, 3, 4, 1, 2, 4}) == 1
minSubArraySum({-1, -2, -3}) == -6
*/
#include<stdio.h>
#include<vector>
using namespace std;
long long minSubArraySum(vector<long long> nums){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (minSubArraySum({2, 3, 4, 1, 2, 4}) == 1);
assert (minSubArraySum({-1, -2, -3}) == -6);
assert (minSubArraySum({-1, -2, -3, 2, -10}) == -14);
assert (minSubArraySum({-9999999999999999}) == -9999999999999999);
assert (minSubArraySum({0, 10, 20, 1000... |
unique_digits | /*
Given a vector of positive integers x. return a sorted vector of all
elements that hasn't any even digit.
Note: Returned vector should be sorted in increasing order.
For example:
>>> unique_digits({15, 33, 1422, 1})
{1, 15, 33}
>>> unique_digits({152, 323, 1422, 10})
{}
*/
#include<stdio.h>
#include<vector>
#incl... | /*
Given a vector of positive integers x. return a sorted vector of all
elements that hasn't any even digit.
Note: Returned vector should be sorted in increasing order.
For example:
>>> unique_digits({15, 33, 1422, 1})
{1, 15, 33}
>>> unique_digits({152, 323, 1422, 10})
{}
*/
#include<stdio.h>
#include<vector>
#incl... | /*
Given a vector of positive integers x. return a sorted vector of all
elements that hasn't any even digit.
Note: Returned vector should be sorted in increasing order.
For example:
>>> unique_digits({15, 33, 1422, 1})
{1, 15, 33}
>>> unique_digits({152, 323, 1422, 10})
{}
*/
#include<stdio.h>
#include<vector>
#incl... | #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(unique_digits({15, 33, 1422, 1}) , {1, 15, 33}));
assert (issame(unique... |
numerical_letter_grade | /*
It is the last week of the semester and the teacher has to give the grades
to students. The teacher has been making her own algorithm for grading.
The only problem is, she has lost the code she used for grading.
She has given you a vector of GPAs for some students and you have to write
a function that can output a ... | /*
It is the last week of the semester and the teacher has to give the grades
to students. The teacher has been making her own algorithm for grading.
The only problem is, she has lost the code she used for grading.
She has given you a vector of GPAs for some students and you have to write
a function that can output a ... | /*
It is the last week of the semester and the teacher has to give the grades
to students. The teacher has been making her own algorithm for grading.
The only problem is, she has lost the code she used for grading.
She has given you a vector of GPAs for some students and you have to write
a function that can output a ... | #undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(numerical_letter_grade({4.0, 3, 1.7, 2, 3.5}) , {"A+", "B", "C-", "C"... |
right_angle_triangle | /*
Given the lengths of the three sides of a triangle. Return true if the three
sides form a right-angled triangle, false otherwise.
A right-angled triangle is a triangle in which one angle is right angle or
90 degree.
Example:
right_angle_triangle(3, 4, 5) == true
right_angle_triangle(1, 2, 3) == false
*/
#include<st... | /*
Given the lengths of the three sides of a triangle. Return true if the three
sides form a right-angled triangle, false otherwise.
A right-angled triangle is a triangle in which one angle is right angle or
90 degree.
Example:
right_angle_triangle(3, 4, 5) == true
right_angle_triangle(1, 2, 3) == false
*/
#include<st... | /*
Given the lengths of the three sides of a triangle. Return true if the three
sides form a right-angled triangle, false otherwise.
A right-angled triangle is a triangle in which one angle is right angle or
90 degree.
Example:
right_angle_triangle(3, 4, 5) == true
right_angle_triangle(1, 2, 3) == false
*/
#include<st... | #undef NDEBUG
#include<assert.h>
int main(){
assert (right_angle_triangle(3, 4, 5) == true);
assert (right_angle_triangle(1, 2, 3) == false);
assert (right_angle_triangle(10, 6, 8) == true);
assert (right_angle_triangle(2, 2, 2) == false);
assert (right_angle_triangle(7, 24, 25) == true);
assert... |
order_by_points | /*
Write a function which sorts the given vector of integers
in ascending order according to the sum of their digits.
Note: if there are several items with similar sum of their digits,
order them based on their index in original vector.
For example:
>>> order_by_points({1, 11, -1, -11, -12}) == {-1, -11, 1, -12, 11}
>... | /*
Write a function which sorts the given vector of integers
in ascending order according to the sum of their digits.
Note: if there are several items with similar sum of their digits,
order them based on their index in original vector.
For example:
>>> order_by_points({1, 11, -1, -11, -12}) == {-1, -11, 1, -12, 11}
>... | /*
Write a function which sorts the given vector of integers
in ascending order according to the sum of their digits.
Note: if there are several items with similar sum of their digits,
order them based on their index in original vector.
For example:
>>> order_by_points({1, 11, -1, -11, -12}) == {-1, -11, 1, -12, 11}
>... | #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(order_by_points({1, 11, -1, -11, -12}) , {-1, -11, 1, -12, 11}));
asser... |
sorted_list_sum | /*
Write a function that accepts a vector of strings as a parameter,
deletes the strings that have odd lengths from it,
and returns the resulted vector with a sorted order,
The vector is always a vector of strings and never a vector of numbers,
and it may contain duplicates.
The order of the vector should be ascending ... | /*
Write a function that accepts a vector of strings as a parameter,
deletes the strings that have odd lengths from it,
and returns the resulted vector with a sorted order,
The vector is always a vector of strings and never a vector of numbers,
and it may contain duplicates.
The order of the vector should be ascending ... | /*
Write a function that accepts a vector of strings as a parameter,
deletes the strings that have odd lengths from it,
and returns the resulted vector with a sorted order,
The vector is always a vector of strings and never a vector of numbers,
and it may contain duplicates.
The order of the vector should be ascending ... | #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(sorted_list_sum({"aa", "a", "aaa"}) , {"aa"}));
assert (issame(so... |
strlen | /*
Return length of given string
>>> strlen("")
0
>>> strlen("abc")
3
*/
#include<stdio.h>
#include<string>
using namespace std;
int strlen(string str){
return str.length()-1;
}
| /*
Return length of given string
>>> strlen("")
0
>>> strlen("abc")
3
*/
#include<stdio.h>
#include<string>
using namespace std;
int strlen(string str){
return str.length();
}
| /*
Return length of given string
>>> strlen("")
0
>>> strlen("abc")
3
*/
#include<stdio.h>
#include<string>
using namespace std;
int strlen(string str){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (strlen("") == 0);
assert (strlen("x") == 1);
assert (strlen("asdasnakj") == 9);
assert(strlen("") == 0);
assert(strlen("x") == 1);
assert(strlen("asdasnakj") == 9);
assert(strlen("12345") == 5);
assert(strlen("abcdefghijklmnopqrstuvwx... |
decode_cyclic | #include<stdio.h>
#include<string>
using namespace std;
string encode_cyclic(string s){
// returns encoded string by cycling groups of three characters.
// split string to groups. Each of length 3.
int l=s.length();
int num=(l+2)/3;
string x,output;
int i;
for (i=0;i*3<l-3;i++)
{
... | #include<stdio.h>
#include<string>
using namespace std;
string encode_cyclic(string s){
// returns encoded string by cycling groups of three characters.
// split string to groups. Each of length 3.
int l=s.length();
int num=(l+2)/3;
string x,output;
int i;
for (i=0;i*3<l;i++)
{
... | #include<stdio.h>
#include<string>
using namespace std;
string encode_cyclic(string s){
// returns encoded string by cycling groups of three characters.
// split string to groups. Each of length 3.
int l=s.length();
int num=(l+2)/3;
string x,output;
int i;
for (i=0;i*3<l;i++)
{
... | #undef NDEBUG
#include<assert.h>
int main(){
for (int i=0;i<100;i++)
{
int l=10+rand()%11;
string str="";
for (int j=0;j<l;j++)
{
char chr=97+rand()%26;
str+=chr;
}
string encoded_str = encode_cyclic(str);
assert (decode_cycli... |
longest | /*
Out of vector of strings, return the longest one. Return the first one in case of multiple
strings of the same length. Return None in case the input vector is empty.
>>> longest({})
>>> longest({"a", "b", "c"})
"a"
>>> longest({"a", "bb", "ccc"})
"ccc"
*/
#include<stdio.h>
#include<vector>
#include<string>
using na... | /*
Out of vector of strings, return the longest one. Return the first one in case of multiple
strings of the same length. Return None in case the input vector is empty.
>>> longest({})
>>> longest({"a", "b", "c"})
"a"
>>> longest({"a", "bb", "ccc"})
"ccc"
*/
#include<stdio.h>
#include<vector>
#include<string>
using na... | /*
Out of vector of strings, return the longest one. Return the first one in case of multiple
strings of the same length. Return None in case the input vector is empty.
>>> longest({})
>>> longest({"a", "b", "c"})
"a"
>>> longest({"a", "bb", "ccc"})
"ccc"
*/
#include<stdio.h>
#include<vector>
#include<string>
using na... | #undef NDEBUG
#include<assert.h>
int main(){
assert (longest({}) == "");
assert (longest({"x", "y", "z"}) == "x");
assert (longest({"x", "yyy", "zzzz", "www", "kkkk", "abc"}) == "zzzz");
assert (longest({"x","y","z"}) == "x");
assert (longest({"x","yyy","zzzz","www","kkkk","abc"}) == "zzzz");
as... |
fizz_buzz | /*
Return the number of times the digit 7 appears in integers less than n which are divisible by 11 or 13.
>>> fizz_buzz(50)
0
>>> fizz_buzz(78)
2
>>> fizz_buzz(79)
3
*/
#include<stdio.h>
using namespace std;
int fizz_buzz(int n){
int count=0;
for (int i=0;i<n;i++)
if (i%11==0 or i%13==0)
{
int ... | /*
Return the number of times the digit 7 appears in integers less than n which are divisible by 11 or 13.
>>> fizz_buzz(50)
0
>>> fizz_buzz(78)
2
>>> fizz_buzz(79)
3
*/
#include<stdio.h>
using namespace std;
int fizz_buzz(int n){
int count=0;
for (int i=0;i<n;i++)
if (i%11==0 or i%13==0)
{
int ... | /*
Return the number of times the digit 7 appears in integers less than n which are divisible by 11 or 13.
>>> fizz_buzz(50)
0
>>> fizz_buzz(78)
2
>>> fizz_buzz(79)
3
*/
#include<stdio.h>
using namespace std;
int fizz_buzz(int n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (fizz_buzz(50) == 0);
assert (fizz_buzz(78) == 2);
assert (fizz_buzz(79) == 3);
assert (fizz_buzz(100) == 3);
assert (fizz_buzz(200) == 6);
assert (fizz_buzz(4000) == 192);
assert (fizz_buzz(10000) == 639);
assert (fizz_buzz(100000) == ... |
digitsum | /*
Task
Write a function that takes a string as input and returns the sum of the upper characters only's
ASCII codes.
Examples:
digitSum("") => 0
digitSum("abAB") => 131
digitSum("abcCd") => 67
digitSum("helloE") => 69
digitSum("woArBld") => 131
digitSum("aAaaaXa") => 153
*/
#include<stdio.h>
#... | /*
Task
Write a function that takes a string as input and returns the sum of the upper characters only's
ASCII codes.
Examples:
digitSum("") => 0
digitSum("abAB") => 131
digitSum("abcCd") => 67
digitSum("helloE") => 69
digitSum("woArBld") => 131
digitSum("aAaaaXa") => 153
*/
#include<stdio.h>
#... | /*
Task
Write a function that takes a string as input and returns the sum of the upper characters only's
ASCII codes.
Examples:
digitSum("") => 0
digitSum("abAB") => 131
digitSum("abcCd") => 67
digitSum("helloE") => 69
digitSum("woArBld") => 131
digitSum("aAaaaXa") => 153
*/
#include<stdio.h>
#... | #undef NDEBUG
#include<assert.h>
int main(){
assert (digitSum("") == 0);
assert (digitSum("abAB") == 131);
assert (digitSum("abcCd") == 67);
assert (digitSum("helloE") == 69);
assert (digitSum("woArBld") == 131);
assert (digitSum("aAaaaXa") == 153);
assert (digitSum(" How are yOu?") == 151);... |
intersperse | /*
Insert a number "delimeter" between every two consecutive elements of input vector `numbers"
>>> intersperse({}, 4)
{}
>>> intersperse({1, 2, 3}, 4)
{1, 4, 2, 4, 3}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> intersperse(vector<int> numbers, int delimeter){
vector<int> out={};
if ... | /*
Insert a number "delimeter" between every two consecutive elements of input vector `numbers"
>>> intersperse({}, 4)
{}
>>> intersperse({1, 2, 3}, 4)
{1, 4, 2, 4, 3}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> intersperse(vector<int> numbers, int delimeter){
vector<int> out={};
if ... | /*
Insert a number "delimeter" between every two consecutive elements of input vector `numbers"
>>> intersperse({}, 4)
{}
>>> intersperse({1, 2, 3}, 4)
{1, 4, 2, 4, 3}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> intersperse(vector<int> numbers, int delimeter){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(intersperse({}, 7), {}));
assert (issame(intersperse({5, 6, 3,... |
triangle_area | /*
Given the lengths of the three sides of a triangle. Return the area of
the triangle rounded to 2 decimal points if the three sides form a valid triangle.
Otherwise return -1
Three sides make a valid triangle when the sum of any two sides is greater
than the third side.
Example:
triangle_area(3, 4, 5) == 6.00
trian... | /*
Given the lengths of the three sides of a triangle. Return the area of
the triangle rounded to 2 decimal points if the three sides form a valid triangle.
Otherwise return -1
Three sides make a valid triangle when the sum of any two sides is greater
than the third side.
Example:
triangle_area(3, 4, 5) == 6.00
trian... | /*
Given the lengths of the three sides of a triangle. Return the area of
the triangle rounded to 2 decimal points if the three sides form a valid triangle.
Otherwise return -1
Three sides make a valid triangle when the sum of any two sides is greater
than the third side.
Example:
triangle_area(3, 4, 5) == 6.00
trian... | #undef NDEBUG
#include<assert.h>
int main(){
assert (abs(triangle_area(3, 4, 5)-6.00)<0.01);
assert (abs(triangle_area(1, 2, 10) +1)<0.01);
assert (abs(triangle_area(4, 8, 5) -8.18)<0.01);
assert (abs(triangle_area(2, 2, 2) -1.73)<0.01);
assert (abs(triangle_area(1, 2, 3) +1)<0.01);
assert (abs(... |
check_dict_case | /*
Given a map, return true if all keys are strings in lower
case or all keys are strings in upper case, else return false.
The function should return false is the given map is empty.
Examples:
check_map_case({{"a","apple"}, {"b","banana"}}) should return true.
check_map_case({{"a","apple"}, {"A","banana"}, {"B","bana... | /*
Given a map, return true if all keys are strings in lower
case or all keys are strings in upper case, else return false.
The function should return false is the given map is empty.
Examples:
check_map_case({{"a","apple"}, {"b","banana"}}) should return true.
check_map_case({{"a","apple"}, {"A","banana"}, {"B","bana... | /*
Given a map, return true if all keys are strings in lower
case or all keys are strings in upper case, else return false.
The function should return false is the given map is empty.
Examples:
check_map_case({{"a","apple"}, {"b","banana"}}) should return true.
check_map_case({{"a","apple"}, {"A","banana"}, {"B","bana... | #undef NDEBUG
#include<assert.h>
int main(){
assert (check_dict_case({{"p","pineapple"}, {"b","banana"}}) == true);
assert (check_dict_case({{"p","pineapple"}, {"A","banana"}, {"B","banana"}}) == false);
assert (check_dict_case({{"p","pineapple"}, {"5","banana"}, {"a","apple"}}) == false);
assert (check... |
same_chars | /*
Check if two words have the same characters.
>>> same_chars("eabcdzzzz", "dddzzzzzzzddeddabc")
true
>>> same_chars("abcd", "dddddddabc")
true
>>> same_chars("dddddddabc", "abcd")
true
>>> same_chars("eabcd", "dddddddabc")
false
>>> same_chars("abcd", "dddddddabce")
false
>>> same_chars("eabcdzzzz", "dddzzzzzzzddddab... | /*
Check if two words have the same characters.
>>> same_chars("eabcdzzzz", "dddzzzzzzzddeddabc")
true
>>> same_chars("abcd", "dddddddabc")
true
>>> same_chars("dddddddabc", "abcd")
true
>>> same_chars("eabcd", "dddddddabc")
false
>>> same_chars("abcd", "dddddddabce")
false
>>> same_chars("eabcdzzzz", "dddzzzzzzzddddab... | /*
Check if two words have the same characters.
>>> same_chars("eabcdzzzz", "dddzzzzzzzddeddabc")
true
>>> same_chars("abcd", "dddddddabc")
true
>>> same_chars("dddddddabc", "abcd")
true
>>> same_chars("eabcd", "dddddddabc")
false
>>> same_chars("abcd", "dddddddabce")
false
>>> same_chars("eabcdzzzz", "dddzzzzzzzddddab... | #undef NDEBUG
#include<assert.h>
int main(){
assert (same_chars("eabcdzzzz", "dddzzzzzzzddeddabc") == true);
assert (same_chars("abcd", "dddddddabc") == true);
assert (same_chars("dddddddabc", "abcd") == true);
assert (same_chars("eabcd", "dddddddabc") == false);
assert (same_chars("abcd", "ddddddda... |
largest_divisor | /*
For a given number n, find the largest number that divides n evenly, smaller than n
>>> largest_divisor(15)
5
*/
#include<stdio.h>
using namespace std;
int largest_divisor(int n){
for (int i=1;i*i<=n;i++)
if (n%i==0) return n/i;
return 1;
}
| /*
For a given number n, find the largest number that divides n evenly, smaller than n
>>> largest_divisor(15)
5
*/
#include<stdio.h>
using namespace std;
int largest_divisor(int n){
for (int i=2;i*i<=n;i++)
if (n%i==0) return n/i;
return 1;
}
| /*
For a given number n, find the largest number that divides n evenly, smaller than n
>>> largest_divisor(15)
5
*/
#include<stdio.h>
using namespace std;
int largest_divisor(int n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (largest_divisor(3) == 1);
assert (largest_divisor(7) == 1);
assert (largest_divisor(10) == 5);
assert (largest_divisor(100) == 50);
assert (largest_divisor(49) == 7);
assert (largest_divisor(3) == 1);
assert (largest_divisor(7) == 1);
... |
is_multiply_prime | /*
Write a function that returns true if the given number is the multiplication of 3 prime numbers
and false otherwise.
Knowing that (a) is less then 100.
Example:
is_multiply_prime(30) == true
30 = 2 * 3 * 5
*/
#include<stdio.h>
using namespace std;
bool is_multiply_prime(int a){
int num=0;
for (int i=2;i*i<=... | /*
Write a function that returns true if the given number is the multiplication of 3 prime numbers
and false otherwise.
Knowing that (a) is less then 100.
Example:
is_multiply_prime(30) == true
30 = 2 * 3 * 5
*/
#include<stdio.h>
using namespace std;
bool is_multiply_prime(int a){
int num=0;
for (int i=2;i*i<=... | /*
Write a function that returns true if the given number is the multiplication of 3 prime numbers
and false otherwise.
Knowing that (a) is less then 100.
Example:
is_multiply_prime(30) == true
30 = 2 * 3 * 5
*/
#include<stdio.h>
using namespace std;
bool is_multiply_prime(int a){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (is_multiply_prime(5) == false);
assert (is_multiply_prime(30) == true);
assert (is_multiply_prime(8) == true);
assert (is_multiply_prime(10) == false);
assert (is_multiply_prime(125) == true);
assert (is_multiply_prime(3 * 5 * 7) == true);
... |
max_fill | /*
You are given a rectangular grid of wells. Each row represents a single well,
and each 1 in a row represents a single unit of water.
Each well has a corresponding bucket that can be used to extract water from it,
and all buckets have the same capacity.
Your task is to use the buckets to empty the wells.
Output the ... | /*
You are given a rectangular grid of wells. Each row represents a single well,
and each 1 in a row represents a single unit of water.
Each well has a corresponding bucket that can be used to extract water from it,
and all buckets have the same capacity.
Your task is to use the buckets to empty the wells.
Output the ... | /*
You are given a rectangular grid of wells. Each row represents a single well,
and each 1 in a row represents a single unit of water.
Each well has a corresponding bucket that can be used to extract water from it,
and all buckets have the same capacity.
Your task is to use the buckets to empty the wells.
Output the ... | #undef NDEBUG
#include<assert.h>
int main(){
assert (max_fill({{0,0,1,0}, {0,1,0,0}, {1,1,1,1}}, 1) == 6);
assert (max_fill({{0,0,1,1}, {0,0,0,0}, {1,1,1,1}, {0,1,1,1}}, 2) == 5);
assert (max_fill({{0,0,0}, {0,0,0}}, 5) == 0);
assert (max_fill({{1,1,1,1}, {1,1,1,1}}, 2) == 4);
assert (max_fill({{1,1... |
any_int | /*
Create a function that takes 3 numbers.
Returns true if one of the numbers is equal to the sum of the other two, and all numbers are integers.
Returns false in any other cases.
Examples
any_int(5, 2, 7) ➞ true
any_int(3, 2, 2) ➞ false
any_int(3, -2, 1) ➞ true
any_int(3.6, -2.2, 2) ➞ false
*/
#include<stdio.h>... | /*
Create a function that takes 3 numbers.
Returns true if one of the numbers is equal to the sum of the other two, and all numbers are integers.
Returns false in any other cases.
Examples
any_int(5, 2, 7) ➞ true
any_int(3, 2, 2) ➞ false
any_int(3, -2, 1) ➞ true
any_int(3.6, -2.2, 2) ➞ false
*/
#include<stdio.h>... | /*
Create a function that takes 3 numbers.
Returns true if one of the numbers is equal to the sum of the other two, and all numbers are integers.
Returns false in any other cases.
Examples
any_int(5, 2, 7) ➞ true
any_int(3, 2, 2) ➞ false
any_int(3, -2, 1) ➞ true
any_int(3.6, -2.2, 2) ➞ false
*/
#include<stdio.h>... | #undef NDEBUG
#include<assert.h>
int main(){
assert (any_int(2, 3, 1)==true);
assert (any_int(2.5, 2, 3)==false);
assert (any_int(1.5, 5, 3.5)==false);
assert (any_int(2, 6, 2)==false);
assert (any_int(4, 2, 2)==true);
assert (any_int(2.2, 2.2, 2.2)==false);
assert (any_int(-4, 6, 2)==true);... |
vowels_count | /*
Write a function vowels_count which takes a string representing a word as input and returns the number of vowels in the string. Vowels in this case are 'a', 'e', 'i', 'o', 'u'.
Here, 'y' is also a vowel, but only when it is at the end of the given word.
Example:
>>> vowels_count("abcde")
2
>>> vowels_count("ACED... | /*
Write a function vowels_count which takes a string representing a word as input and returns the number of vowels in the string. Vowels in this case are 'a', 'e', 'i', 'o', 'u'.
Here, 'y' is also a vowel, but only when it is at the end of the given word.
Example:
>>> vowels_count("abcde")
2
>>> vowels_count("ACED... | /*
Write a function vowels_count which takes a string representing a word as input and returns the number of vowels in the string. Vowels in this case are 'a', 'e', 'i', 'o', 'u'.
Here, 'y' is also a vowel, but only when it is at the end of the given word.
Example:
>>> vowels_count("abcde")
2
>>> vowels_count("ACED... | #undef NDEBUG
#include<assert.h>
int main(){
assert (vowels_count("abcde") == 2);
assert (vowels_count("Alone") == 3);
assert (vowels_count("key") == 2);
assert (vowels_count("bye") == 1);
assert (vowels_count("keY") == 2);
assert (vowels_count("bYe") == 1);
assert (vowels_count("ACEDY") == ... |
remove_duplicates | /*
From a vector of integers, remove all elements that occur more than once.
Keep order of elements left the same as in the input.
>>> remove_duplicates({1, 2, 3, 2, 4})
{1, 3, 4}
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> remove_duplicates(vector<int> numbers){
vecto... | /*
From a vector of integers, remove all elements that occur more than once.
Keep order of elements left the same as in the input.
>>> remove_duplicates({1, 2, 3, 2, 4})
{1, 3, 4}
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> remove_duplicates(vector<int> numbers){
vecto... | /*
From a vector of integers, remove all elements that occur more than once.
Keep order of elements left the same as in the input.
>>> remove_duplicates({1, 2, 3, 2, 4})
{1, 3, 4}
*/
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> remove_duplicates(vector<int> numbers){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(remove_duplicates({}) , {}));
assert (issame(remove_duplicates({1,... |
parse_nested_parens | /*
Input to this function is a string represented multiple groups for nested parentheses separated by spaces.
For each of the group, output the deepest level of nesting of parentheses.
E.g. (()()) has maximum two levels of nesting while ((())) has three.
>>> parse_nested_parens("(()()) ((())) () ((())()())")
{2, 3, 1,... | /*
Input to this function is a string represented multiple groups for nested parentheses separated by spaces.
For each of the group, output the deepest level of nesting of parentheses.
E.g. (()()) has maximum two levels of nesting while ((())) has three.
>>> parse_nested_parens("(()()) ((())) () ((())()())")
{2, 3, 1,... | /*
Input to this function is a string represented multiple groups for nested parentheses separated by spaces.
For each of the group, output the deepest level of nesting of parentheses.
E.g. (()()) has maximum two levels of nesting while ((())) has three.
>>> parse_nested_parens("(()()) ((())) () ((())()())")
{2, 3, 1,... | #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(parse_nested_parens("(()()) ((())) () ((())()())"),{2, 3, 1, 3}));
... |
intersection | /*
You are given two intervals,
where each interval is a pair of integers. For example, interval = (start, end) = (1, 2).
The given intervals are closed which means that the interval (start, end)
includes both start and end.
For each given interval, it is assumed that its start is less or equal its end.
Your task is to... | /*
You are given two intervals,
where each interval is a pair of integers. For example, interval = (start, end) = (1, 2).
The given intervals are closed which means that the interval (start, end)
includes both start and end.
For each given interval, it is assumed that its start is less or equal its end.
Your task is to... | /*
You are given two intervals,
where each interval is a pair of integers. For example, interval = (start, end) = (1, 2).
The given intervals are closed which means that the interval (start, end)
includes both start and end.
For each given interval, it is assumed that its start is less or equal its end.
Your task is to... | #undef NDEBUG
#include<assert.h>
int main(){
assert (intersection({1, 2}, {2, 3}) == "NO");
assert (intersection({-1, 1}, {0, 4}) == "NO");
assert (intersection({-3, -1}, {-5, 5}) == "YES");
assert (intersection({-2, 2}, {-4, 0}) == "YES");
assert (intersection({-11, 2}, {-1, -1}) == "NO");
asse... |
change_base | /*
Change numerical base of input number x to base.
return string representation after the conversion.
base numbers are less than 10.
>>> change_base(8, 3)
"22"
>>> change_base(8, 2)
"1000"
>>> change_base(7, 2)
"111"
*/
#include<stdio.h>
#include<string>
using namespace std;
string change_base(int x,int base){
str... | /*
Change numerical base of input number x to base.
return string representation after the conversion.
base numbers are less than 10.
>>> change_base(8, 3)
"22"
>>> change_base(8, 2)
"1000"
>>> change_base(7, 2)
"111"
*/
#include<stdio.h>
#include<string>
using namespace std;
string change_base(int x,int base){
str... | /*
Change numerical base of input number x to base.
return string representation after the conversion.
base numbers are less than 10.
>>> change_base(8, 3)
"22"
>>> change_base(8, 2)
"1000"
>>> change_base(7, 2)
"111"
*/
#include<stdio.h>
#include<string>
using namespace std;
string change_base(int x,int base){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (change_base(8, 3) == "22");
assert (change_base(9, 3) == "100");
assert (change_base(234, 2) == "11101010");
assert (change_base(16, 2) == "10000");
assert (change_base(8, 2) == "1000");
assert (change_base(7, 2) == "111");
for (int x=2;x<... |
compare | /*
I think we all remember that feeling when the result of some long-awaited
event is finally known. The feelings and thoughts you have at that moment are
definitely worth noting down and comparing.
Your task is to determine if a person correctly guessed the results of a number of matches.
You are given two vectors of ... | /*
I think we all remember that feeling when the result of some long-awaited
event is finally known. The feelings and thoughts you have at that moment are
definitely worth noting down and comparing.
Your task is to determine if a person correctly guessed the results of a number of matches.
You are given two vectors of ... | /*
I think we all remember that feeling when the result of some long-awaited
event is finally known. The feelings and thoughts you have at that moment are
definitely worth noting down and comparing.
Your task is to determine if a person correctly guessed the results of a number of matches.
You are given two vectors of ... | #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(compare({1,2,3,4,5,1},{1,2,3,4,2,-2}),{0,0,0,0,3,3}));
assert (issame(c... |
valid_date | /*
You have to write a function which validates a given date string and
returns true if the date is valid otherwise false.
The date is valid if all of the following rules are satisfied:
1. The date string is not empty.
2. The number of days is not less than 1 or higher than 31 days for months 1,3,5,7,8,10,12. And the n... | /*
You have to write a function which validates a given date string and
returns true if the date is valid otherwise false.
The date is valid if all of the following rules are satisfied:
1. The date string is not empty.
2. The number of days is not less than 1 or higher than 31 days for months 1,3,5,7,8,10,12. And the n... | /*
You have to write a function which validates a given date string and
returns true if the date is valid otherwise false.
The date is valid if all of the following rules are satisfied:
1. The date string is not empty.
2. The number of days is not less than 1 or higher than 31 days for months 1,3,5,7,8,10,12. And the n... | #undef NDEBUG
#include<assert.h>
int main(){
assert (valid_date("03-11-2000") == true);
assert (valid_date("15-01-2012") == false);
assert (valid_date("04-0-2040") == false);
assert (valid_date("06-04-2020") == true);
assert (valid_date("01-01-2007") == true);
assert (valid_date("03-32-2011") ==... |
rolling_max | /*
From a given vector of integers, generate a vector of rolling maximum element found until given moment
in the sequence.
>>> rolling_max({1, 2, 3, 2, 3, 4, 2})
{1, 2, 3, 3, 3, 4, 4}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> rolling_max(vector<int> numbers){
vector<int> out;
int ma... | /*
From a given vector of integers, generate a vector of rolling maximum element found until given moment
in the sequence.
>>> rolling_max({1, 2, 3, 2, 3, 4, 2})
{1, 2, 3, 3, 3, 4, 4}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> rolling_max(vector<int> numbers){
vector<int> out;
int ma... | /*
From a given vector of integers, generate a vector of rolling maximum element found until given moment
in the sequence.
>>> rolling_max({1, 2, 3, 2, 3, 4, 2})
{1, 2, 3, 3, 3, 4, 4}
*/
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> rolling_max(vector<int> numbers){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(rolling_max({}),{}));
assert (issame(rolling_max({1, 2, 3, 4}) , {... |
can_arrange | /*
Create a function which returns the largest index of an element which
is not greater than or equal to the element immediately preceding it. If
no such element exists then return -1. The given vector will not contain
duplicate values.
Examples:
can_arrange({1,2,4,3,5}) = 3
can_arrange({1,2,3}) = -1
*/
#include<stdio... | /*
Create a function which returns the largest index of an element which
is not greater than or equal to the element immediately preceding it. If
no such element exists then return -1. The given vector will not contain
duplicate values.
Examples:
can_arrange({1,2,4,3,5}) = 3
can_arrange({1,2,3}) = -1
*/
#include<stdio... | /*
Create a function which returns the largest index of an element which
is not greater than or equal to the element immediately preceding it. If
no such element exists then return -1. The given vector will not contain
duplicate values.
Examples:
can_arrange({1,2,4,3,5}) = 3
can_arrange({1,2,3}) = -1
*/
#include<stdio... | #undef NDEBUG
#include<assert.h>
int main(){
assert (can_arrange({1,2,4,3,5})==3);
assert (can_arrange({1,2,4,5})==-1);
assert (can_arrange({1,4,2,5,6,7,8,9,10})==2);
assert (can_arrange({4,8,5,7,3})==4);
assert (can_arrange({})==-1);
assert (can_arrange({1,2,4,3,5})==3);
assert (can_arrange... |
largest_prime_factor | /*
Return the largest prime factor of n. Assume n > 1 and is not a prime.
>>> largest_prime_factor(13195)
29
>>> largest_prime_factor(2048)
2
*/
#include<stdio.h>
using namespace std;
int largest_prime_factor(int n){
for (int i=2;i*i<=n;i++)
while (n%i==0 and n>=i) n=n/i;
return n;
}
| /*
Return the largest prime factor of n. Assume n > 1 and is not a prime.
>>> largest_prime_factor(13195)
29
>>> largest_prime_factor(2048)
2
*/
#include<stdio.h>
using namespace std;
int largest_prime_factor(int n){
for (int i=2;i*i<=n;i++)
while (n%i==0 and n>i) n=n/i;
return n;
}
| /*
Return the largest prime factor of n. Assume n > 1 and is not a prime.
>>> largest_prime_factor(13195)
29
>>> largest_prime_factor(2048)
2
*/
#include<stdio.h>
using namespace std;
int largest_prime_factor(int n){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (largest_prime_factor(15) == 5);
assert (largest_prime_factor(27) == 3);
assert (largest_prime_factor(63) == 7);
assert (largest_prime_factor(330) == 11);
assert (largest_prime_factor(13195) == 29);
assert(largest_prime_factor(15) == 5);
as... |
concatenate | /*
Concatenate vector of strings into a single string
>>> concatenate({})
""
>>> concatenate({"a", "b", "c"})
"abc"
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
string concatenate(vector<string> strings){
string out=" ";
for (int i=0;i<strings.size();i++)
out=out+strings[i... | /*
Concatenate vector of strings into a single string
>>> concatenate({})
""
>>> concatenate({"a", "b", "c"})
"abc"
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
string concatenate(vector<string> strings){
string out="";
for (int i=0;i<strings.size();i++)
out=out+strings[i]... | /*
Concatenate vector of strings into a single string
>>> concatenate({})
""
>>> concatenate({"a", "b", "c"})
"abc"
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
string concatenate(vector<string> strings){
| #undef NDEBUG
#include<assert.h>
int main(){
assert (concatenate({}) == "");
assert (concatenate({"x", "y", "z"}) == "xyz");
assert (concatenate({"x", "y", "z", "w", "k"}) == "xyzwk");
assert(concatenate({}) == "");
assert(concatenate({"x","y","z"}) == "xyz");
assert(concatenate({"x","y","z","w... |
exchange | /*
In this problem, you will implement a function that takes two vectors of numbers,
and determines whether it is possible to perform an exchange of elements
between them to make lst1 a vector of only even numbers.
There is no limit on the number of exchanged elements between lst1 and lst2.
If it is possible to exchang... | /*
In this problem, you will implement a function that takes two vectors of numbers,
and determines whether it is possible to perform an exchange of elements
between them to make lst1 a vector of only even numbers.
There is no limit on the number of exchanged elements between lst1 and lst2.
If it is possible to exchang... | /*
In this problem, you will implement a function that takes two vectors of numbers,
and determines whether it is possible to perform an exchange of elements
between them to make lst1 a vector of only even numbers.
There is no limit on the number of exchanged elements between lst1 and lst2.
If it is possible to exchang... | #undef NDEBUG
#include<assert.h>
int main(){
assert (exchange({1, 2, 3, 4}, {1, 2, 3, 4}) == "YES");
assert (exchange({1, 2, 3, 4}, {1, 5, 3, 4}) == "NO");
assert (exchange({1, 2, 3, 4}, {2, 1, 4, 3}) == "YES" );
assert (exchange({5, 7, 3}, {2, 6, 4}) == "YES");
assert (exchange({5, 7, 3}, {2, 6, 3}... |
encode | /*
Write a function that takes a message, and encodes in such a
way that it swaps case of all letters, replaces all vowels in
the message with the letter that appears 2 places ahead of that
vowel in the english alphabet.
Assume only letters.
Examples:
>>> encode('test")
"TGST"
>>> encode("This is a message")
'tHK... | /*
Write a function that takes a message, and encodes in such a
way that it swaps case of all letters, replaces all vowels in
the message with the letter that appears 2 places ahead of that
vowel in the english alphabet.
Assume only letters.
Examples:
>>> encode('test")
"TGST"
>>> encode("This is a message")
'tHK... | /*
Write a function that takes a message, and encodes in such a
way that it swaps case of all letters, replaces all vowels in
the message with the letter that appears 2 places ahead of that
vowel in the english alphabet.
Assume only letters.
Examples:
>>> encode('test")
"TGST"
>>> encode("This is a message")
'tHK... | #undef NDEBUG
#include<assert.h>
int main(){
assert (encode("TEST") == "tgst");
assert (encode("Mudasir") == "mWDCSKR");
assert (encode("YES") == "ygs");
assert (encode("This is a message") == "tHKS KS C MGSSCGG");
assert (encode("I DoNt KnOw WhAt tO WrItE") == "k dQnT kNqW wHcT Tq wRkTg");
asse... |
tri | /*
Everyone knows Fibonacci sequence, it was studied deeply by mathematicians in
the last couple centuries. However, what people don't know is Tribonacci sequence.
Tribonacci sequence is defined by the recurrence:
tri(1) = 3
tri(n) = 1 + n / 2, if n is even.
tri(n) = tri(n - 1) + tri(n - 2) + tri(n + 1), if n is odd.... | /*
Everyone knows Fibonacci sequence, it was studied deeply by mathematicians in
the last couple centuries. However, what people don't know is Tribonacci sequence.
Tribonacci sequence is defined by the recurrence:
tri(1) = 3
tri(n) = 1 + n / 2, if n is even.
tri(n) = tri(n - 1) + tri(n - 2) + tri(n + 1), if n is odd.... | /*
Everyone knows Fibonacci sequence, it was studied deeply by mathematicians in
the last couple centuries. However, what people don't know is Tribonacci sequence.
Tribonacci sequence is defined by the recurrence:
tri(1) = 3
tri(n) = 1 + n / 2, if n is even.
tri(n) = tri(n - 1) + tri(n - 2) + tri(n + 1), if n is odd.... | #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(tri(3) , {1, 3, 2, 8}));
assert (issame(tri(4) , {1, 3, 2, 8, 3}));
... |
get_positive | /*
Return only positive numbers in the vector.
>>> get_positive({-1, 2, -4, 5, 6})
{2, 5, 6}
>>> get_positive({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10})
{5, 3, 2, 3, 9, 123, 1}
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
vector<float> get_positive(vector<float> l){
vector<float> out={}... | /*
Return only positive numbers in the vector.
>>> get_positive({-1, 2, -4, 5, 6})
{2, 5, 6}
>>> get_positive({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10})
{5, 3, 2, 3, 9, 123, 1}
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
vector<float> get_positive(vector<float> l){
vector<float> out={}... | /*
Return only positive numbers in the vector.
>>> get_positive({-1, 2, -4, 5, 6})
{2, 5, 6}
>>> get_positive({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10})
{5, 3, 2, 3, 9, 123, 1}
*/
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
vector<float> get_positive(vector<float> l){
| #undef NDEBUG
#include<assert.h>
bool issame(vector<float> a,vector<float>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (abs(a[i]-b[i])>1e-4) return false;
}
return true;
}
int main(){
assert (issame(get_positive({-1, -2, 4, 5, 6}) , {4, 5, 6} ));
assert... |
add_elements | /*
Given a non-empty vector of integers arr and an integer k, return
the sum of the elements with at most two digits from the first k elements of arr.
Example:
Input: arr = {111,21,3,4000,5,6,7,8,9}, k = 4
Output: 24 # sum of 21 + 3
Constraints:
1. 1 <= len(arr) <= 100
2. 1 <= k <= len(arr)
*/
#inclu... | /*
Given a non-empty vector of integers arr and an integer k, return
the sum of the elements with at most two digits from the first k elements of arr.
Example:
Input: arr = {111,21,3,4000,5,6,7,8,9}, k = 4
Output: 24 # sum of 21 + 3
Constraints:
1. 1 <= len(arr) <= 100
2. 1 <= k <= len(arr)
*/
#inclu... | /*
Given a non-empty vector of integers arr and an integer k, return
the sum of the elements with at most two digits from the first k elements of arr.
Example:
Input: arr = {111,21,3,4000,5,6,7,8,9}, k = 4
Output: 24 # sum of 21 + 3
Constraints:
1. 1 <= len(arr) <= 100
2. 1 <= k <= len(arr)
*/
#inclu... | #undef NDEBUG
#include<assert.h>
int main(){
assert (add_elements({1,-2,-3,41,57,76,87,88,99}, 3) == -4);
assert (add_elements({111,121,3,4000,5,6}, 2) == 0);
assert (add_elements({11,21,3,90,5,6,7,8,9}, 4) == 125);
assert (add_elements({111,21,3,4000,5,6,7,8,9}, 4) == 24);
assert (add_elements({1},... |
filter_by_substring | /*
Filter an input vector of strings only for ones that contain given substring
>>> filter_by_substring({}, "a")
{}
>>> filter_by_substring({"abc", "bacd", "cde", "vector"}, "a")
{"abc", "bacd", "vector"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> filter_by_substring(vect... | /*
Filter an input vector of strings only for ones that contain given substring
>>> filter_by_substring({}, "a")
{}
>>> filter_by_substring({"abc", "bacd", "cde", "vector"}, "a")
{"abc", "bacd", "vector"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> filter_by_substring(vect... | /*
Filter an input vector of strings only for ones that contain given substring
>>> filter_by_substring({}, "a")
{}
>>> filter_by_substring({"abc", "bacd", "cde", "vector"}, "a")
{"abc", "bacd", "vector"}
*/
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
vector<string> filter_by_substring(vect... | #undef NDEBUG
#include<assert.h>
bool issame(vector<string> a,vector<string>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(filter_by_substring({}, "john"),{}));
assert (issame(filter_by_s... |
strange_sort_list | /*
Given vector of integers, return vector in strange order.
Strange sorting, is when you start with the minimum value,
then maximum of the remaining integers, then minimum and so on.
Examples:
strange_sort_vector({1, 2, 3, 4}) == {1, 4, 2, 3}
strange_sort_vector({5, 5, 5, 5}) == {5, 5, 5, 5}
strange_sort_vector({}) =... | /*
Given vector of integers, return vector in strange order.
Strange sorting, is when you start with the minimum value,
then maximum of the remaining integers, then minimum and so on.
Examples:
strange_sort_vector({1, 2, 3, 4}) == {1, 4, 2, 3}
strange_sort_vector({5, 5, 5, 5}) == {5, 5, 5, 5}
strange_sort_vector({}) =... | /*
Given vector of integers, return vector in strange order.
Strange sorting, is when you start with the minimum value,
then maximum of the remaining integers, then minimum and so on.
Examples:
strange_sort_vector({1, 2, 3, 4}) == {1, 4, 2, 3}
strange_sort_vector({5, 5, 5, 5}) == {5, 5, 5, 5}
strange_sort_vector({}) =... | #undef NDEBUG
#include<assert.h>
bool issame(vector<int> a,vector<int>b){
if (a.size()!=b.size()) return false;
for (int i=0;i<a.size();i++)
{
if (a[i]!=b[i]) return false;
}
return true;
}
int main(){
assert (issame(strange_sort_list({1, 2, 3, 4}) , {1, 4, 2, 3}));
assert (issame(st... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.