instance_id
stringlengths
13
15
number_spans
int64
1
3
prompt
stringlengths
133
1.35k
declaration
stringlengths
111
515
splits
listlengths
2
4
removed_spans
listlengths
1
3
canonical_solution
stringlengths
18
1.4k
test
stringlengths
148
1.76k
CPP/100_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> vector<int> make_a_pile(int n){
[ " vector<int> out={n};\n", "\n" ]
[ " for (int i=1;i<n;i++)\n out.push_back(out[out.size()-1]+2);\n return out;\n}" ]
vector<int> out={n}; for (int i=1;i<n;i++) out.push_back(out[out.size()-1]+2); return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(make_a_pile(3) , {3, 5, 7})); assert (issame(make_a_pile(4) , {4,6,8,10...
CPP/101_spans_1
1
/* You will be given a string of words separated by commas or spaces. Your task is to split the string into words and return a vector of the words. For example: words_string("Hi, my name is John") == {"Hi", "my", "name", "is", "John"} words_string("One, two, three, four, five, six") == {"One", 'two", 'three", "four", ...
#include<stdio.h> #include<math.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> vector<string> words_string(string s){
[ " string current=\"\";\n vector<string> out={};\n s=s+' ';\n for (int i=0;i<s.length();i++)\n if (s[i]==' ' or s[i]==',')\n {\n if (current.length()>0)\n {\n out.push_back(current);\n ", "rent=current+s[i];\n return out;\n}\n" ]
[ " current=\"\";\n }\n }\n else cur" ]
string current=""; vector<string> out={}; s=s+' '; for (int i=0;i<s.length();i++) if (s[i]==' ' or s[i]==',') { if (current.length()>0) { out.push_back(current); current=""; } } else current=current+s[i]; return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(words_string("Hi, my name is John") , {"Hi", "my", "name", "is", "Joh...
CPP/102_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> int choose_num(int x,int y){
[ " ", "turn y-1;\n return y;\n}\n" ]
[ "if (y<x) return -1;\n if (y==x and y%2==1) return -1;\n if (y%2==1) re" ]
if (y<x) return -1; if (y==x and y%2==1) return -1; if (y%2==1) return y-1; return y; }
#undef NDEBUG #include<assert.h> int main(){ assert (choose_num(12, 15) == 14); assert (choose_num(13, 12) == -1); assert (choose_num(33, 12354) == 12354); assert (choose_num(5234, 5233) == -1); assert (choose_num(6, 29) == 28); assert (choose_num(27, 10) == -1); assert (choose_num(7, 7) == ...
CPP/103_spans_1
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...
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> string rounded_avg(int n,int m){
[ " if (n>m) return \"-1\";\n in", "um%2)+out;\n num=num/2;\n }\n return out;\n}\n" ]
[ "t num=(m+n)/2;\n string out=\"\";\n while (num>0)\n {\n out=to_string(n" ]
if (n>m) return "-1"; int num=(m+n)/2; string out=""; while (num>0) { out=to_string(num%2)+out; num=num/2; } return out; }
#undef NDEBUG #include<assert.h> int main(){ assert (rounded_avg(1, 5) == "11"); assert (rounded_avg(7, 13) == "1010"); assert (rounded_avg(964,977) == "1111001010"); assert (rounded_avg(996,997) == "1111100100"); assert (rounded_avg(560,851) == "1011000001"); assert (rounded_avg(185,546) == "...
CPP/104_spans_1
1
/* Given a vector of positive integers x. return a sorted vector of all elements that hasn't any even digit. Note: Returned vector should be sorted in increasing order. For example: >>> unique_digits({15, 33, 1422, 1}) {1, 15, 33} >>> unique_digits({152, 323, 1422, 10}) {} */ #include<stdio.h> #include<vector> #incl...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> unique_digits(vector<int> x){
[ " vector<", " {\n int num=x[i];\n bool u=true;\n if (num==0) u=false;\n while (num>0 and u)\n {\n if (num%2==0) u=false;\n num=num/10;\n }\n if (u) out.push_back(x[i]);\n }\n sort(out.begi...
[ "int> out={};\n for (int i=0;i<x.size();i++)\n " ]
vector<int> out={}; for (int i=0;i<x.size();i++) { int num=x[i]; bool u=true; if (num==0) u=false; while (num>0 and u) { if (num%2==0) u=false; num=num/10; } if (u) out.push_back(x[i]); ...
#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...
CPP/105_spans_1
1
/* 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 -> {...
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<map> #include<algorithm> using namespace std; #include<stdlib.h> vector<string> by_length(vector<int> arr){
[ " map<int,string> numto={{0,\"Zero\"},{1,\"One\"},{2,\"Two\"},{3,\"Three\"},{4,\"Four\"},{5,\"Five\"},{6,\"Six\"},{7,\"Seven\"},{8,\"Eight\"},{9,\"Nine\"}};\n sort(arr.begin(),arr.end());\n vector<string> out={};\n for (", "nd arr[i]<=9)\n out.push_back(numto[arr[i]]);\n return out;\n}\n" ]
[ "int i=arr.size()-1;i>=0;i-=1)\n if (arr[i]>=1 a" ]
map<int,string> numto={{0,"Zero"},{1,"One"},{2,"Two"},{3,"Three"},{4,"Four"},{5,"Five"},{6,"Six"},{7,"Seven"},{8,"Eight"},{9,"Nine"}}; sort(arr.begin(),arr.end()); vector<string> out={}; for (int i=arr.size()-1;i>=0;i-=1) if (arr[i]>=1 and arr[i]<=9) out.push_back(numto[arr[i]]); ret...
#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...
CPP/106_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> vector<int> f(int n){
[ " int sum=0,prod=1;\n vector<int> out={};\n for (int i=1;i<=n;i++)\n {\n ", "t.push_back(prod);\n else out.push_back(sum);\n } \n return out;\n}\n" ]
[ " sum+=i;\n prod*=i;\n if (i%2==0) ou" ]
int sum=0,prod=1; vector<int> out={}; for (int i=1;i<=n;i++) { sum+=i; prod*=i; if (i%2==0) out.push_back(prod); else out.push_back(sum); } return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(f(5) , {1, 2, 6, 24, 15})); assert (issame(f(7) , {1, 2, 6, 24, 15, 720...
CPP/107_spans_1
1
/* Given a positive integer n, return a vector that has the number of even and odd integer palindromes that fall within the range(1, n), inclusive. Example 1: Input: 3 Output: (1, 2) Explanation: Integer palindrome are 1, 2, 3. one of them is even, and two of them are odd. Example 2: Input: 12 ...
#include<stdio.h> #include<math.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> vector<int> even_odd_palindrome(int n){
[ " int num1=0,num2=0;\n ", " p(w.rbegin(),w.rend());\n if (w==p and i%2==1) num1+=1;\n if (w==p and i%2==0) num2+=1;\n \n }\n return {num2,num1};\n}\n" ]
[ " for (int i=1;i<=n;i++)\n {\n string w=to_string(i);\n string" ]
int num1=0,num2=0; for (int i=1;i<=n;i++) { string w=to_string(i); string p(w.rbegin(),w.rend()); if (w==p and i%2==1) num1+=1; if (w==p and i%2==0) num2+=1; } return {num2,num1}; }
#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...
CPP/108_spans_1
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,...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> int count_nums(vector<int> n){
[ " int num=0;\n for (int i=0;i<n.size();i++)\n if (n[i]>0) num+=1;\n else\n {\n int sum=0;\n int w;\n w=abs(n[i]);\n while (w>=10)\n {\n sum+=w%10;\n w=w/10;\n }\n ", "m+=1;\n }\n r...
[ " sum-=w;\n if (sum>0) nu" ]
int num=0; for (int i=0;i<n.size();i++) if (n[i]>0) num+=1; else { int sum=0; int w; w=abs(n[i]); while (w>=10) { sum+=w%10; w=w/10; } sum-=w; if (sum>0) num+=1...
#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,...
CPP/109_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> bool move_one_ball(vector<int> arr){
[ " int num=0;\n if (arr.size()==0) return true;\n for (int i=1;i<arr.size();i++)\n ", "arr[i-1]) num+=1;\n if (arr[arr.size()-1]>arr[0]) num+=1;\n if (num<2) return true;\n return false;\n}\n" ]
[ " if (arr[i]<" ]
int num=0; if (arr.size()==0) return true; for (int i=1;i<arr.size();i++) if (arr[i]<arr[i-1]) num+=1; if (arr[arr.size()-1]>arr[0]) num+=1; if (num<2) return true; return false; }
#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); }
CPP/110_spans_1
1
/* In this problem, you will implement a function that takes two vectors of numbers, and determines whether it is possible to perform an exchange of elements between them to make lst1 a vector of only even numbers. There is no limit on the number of exchanged elements between lst1 and lst2. If it is possible to exchang...
#include<stdio.h> #include<math.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> string exchange(vector<int> lst1,vector<int> lst2){
[ " int num=0;\n for (int i=0;i<lst1.size();i++)\n if (lst1[i]%2==", "ze();i++)\n if (lst2[i]%2==0) num+=1;\n if (num>=lst1.size()) return \"YES\";\n return \"NO\";\n}\n" ]
[ "0) num+=1;\n for (int i=0;i<lst2.si" ]
int num=0; for (int i=0;i<lst1.size();i++) if (lst1[i]%2==0) num+=1; for (int i=0;i<lst2.size();i++) if (lst2[i]%2==0) num+=1; if (num>=lst1.size()) return "YES"; return "NO"; }
#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}...
CPP/111_spans_1
1
/* Given a string representing a space separated lowercase letters, return a map of the letter with the most repetition and containing the corresponding count. If several letters have the same occurrence, return all of them. Example: histogram("a b c") == {{"a", 1}, {"b", 1}, {"c", 1}} histogram("a b b a") == {{"a", 2...
#include<stdio.h> #include<math.h> #include<string> #include<map> using namespace std; #include<algorithm> #include<stdlib.h> map<char,int> histogram(string test){
[ " map<char,int> count={},out={};\n map <char,int>::iterator it;\n int max=0;\n for (int i=0;i<test.length();i++)\n if (test[i]!=' ')\n {\n count[test[i]]+=1;\n if (count[test[i]]>max) max=count[test[i]];\n }\n for (it=count.begin();it!=count.end();it++)\n ...
[ "=it->second;\n if (w2==max) out[w1]=w2;\n " ]
map<char,int> count={},out={}; map <char,int>::iterator it; int max=0; for (int i=0;i<test.length();i++) if (test[i]!=' ') { count[test[i]]+=1; if (count[test[i]]>max) max=count[test[i]]; } for (it=count.begin();it!=count.end();it++) { char...
#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]!=...
CPP/112_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> vector<string> reverse_delete(string s,string c){
[ " string n=\"\";\n for (int i=0;i<s.length();i++)\n if (find(c.begin(),c.end(),s[i])==c.end())\n n=n+s[i]; \n if (n.length()==0) return {n,\"True\"};\n string w(n.rbegin(),n.rend());\n if (w==n) return ", ";\n return {n,\"False\"};\n}\n" ]
[ "{n,\"True\"}" ]
string n=""; for (int i=0;i<s.length();i++) if (find(c.begin(),c.end(),s[i])==c.end()) n=n+s[i]; if (n.length()==0) return {n,"True"}; string w(n.rbegin(),n.rend()); if (w==n) return {n,"True"}; return {n,"False"}; }
#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(...
CPP/113_spans_1
1
/* 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 ...
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<map> using namespace std; #include<algorithm> #include<stdlib.h> vector<string> odd_count(vector<string> lst){
[ " vector<string> out={};\n for (int i=0;i<lst.size();i++)\n {\n int sum=0;\n for (int j=0;j<lst[i].length();j++)\n if (lst[i][j]>=48 and lst[i][j]<=57 and lst[i][j]%2==1)\n sum+=1;\n string s=\"the number of odd elements in the string i of ...
[ "_string(sum);\n else s2=s2+s[j];\n out.push" ]
vector<string> out={}; for (int i=0;i<lst.size();i++) { int sum=0; for (int j=0;j<lst[i].length();j++) if (lst[i][j]>=48 and lst[i][j]<=57 and lst[i][j]%2==1) sum+=1; string s="the number of odd elements in the string i of the input."; ...
#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...
CPP/114_spans_1
1
/* Given a vector of integers nums, find the minimum sum of any non-empty sub-vector of nums. Example minSubArraySum({2, 3, 4, 1, 2, 4}) == 1 minSubArraySum({-1, -2, -3}) == -6 */ #include<stdio.h> #include<vector> using namespace std; long long minSubArraySum(vector<long long> nums){
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> long long minSubArraySum(vector<long long> nums){
[ " long long current,min;\n current=nums[0];\n ", "rrent<0) current=current+nums[i];\n else current=nums[i];\n if (current<min) min=current;\n }\n return min;\n}\n" ]
[ "min=nums[0];\n for (int i=1;i<nums.size();i++)\n {\n if (cu" ]
long long current,min; current=nums[0]; min=nums[0]; for (int i=1;i<nums.size();i++) { if (current<0) current=current+nums[i]; else current=nums[i]; if (current<min) min=current; } return min; }
#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...
CPP/115_spans_1
1
/* You are given a rectangular grid of wells. Each row represents a single well, and each 1 in a row represents a single unit of water. Each well has a corresponding bucket that can be used to extract water from it, and all buckets have the same capacity. Your task is to use the buckets to empty the wells. Output the ...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> int max_fill(vector<vector<int>> grid,int capacity){
[ " int out=0;\n for (int i=0;i<grid.size();i++)\n {\n int sum=0;\n for (int j=0;j<grid[i].size();j++)\n s", " if (sum>0) out+=(sum-1)/capacity+1;\n }\n return out;\n}\n" ]
[ "um+=grid[i][j];\n " ]
int out=0; for (int i=0;i<grid.size();i++) { int sum=0; for (int j=0;j<grid[i].size();j++) sum+=grid[i][j]; if (sum>0) out+=(sum-1)/capacity+1; } return out; }
#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...
CPP/116_spans_1
1
/* In this Kata, you have to sort a vector of non-negative integers according to number of ones in their binary representation in ascending order. For similar number of ones, sort based on decimal value. It must be implemented like this: >>> sort_vector({1, 5, 2, 3, 4}) == {1, 2, 3, 4, 5} >>> sort_vector({-2, -3, -4, ...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> sort_array(vector<int> arr){
[ " vector<int> bin={};\n int m;\n\n for (int i=0;i<arr.size();i++)\n {\n int b=0,n=abs(arr[i]);\n while (n>0)\n {\n ", "++)\n for (int j=1;j<arr.size();j++)\n if (bin[j]<bin[j-1] or (bin[j]==bin[j-1] and arr[j]<arr[j-1]))\n {\n m=arr[j];arr[j]=arr[j-1];arr[j-1]=m;\n...
[ " b+=n%2;n=n/2;\n }\n bin.push_back(b);\n }\n for (int i=0;i<arr.size();i" ]
vector<int> bin={}; int m; for (int i=0;i<arr.size();i++) { int b=0,n=abs(arr[i]); while (n>0) { b+=n%2;n=n/2; } bin.push_back(b); } for (int i=0;i<arr.size();i++) for (int j=1;j<arr.size();j++) if (bin[j]<bin[j-1] or (bin[j]==bin[j-1]...
#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(...
CPP/117_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> vector<string> select_words(string s,int n){
[ " string vowels=\"aeiouAEIOU\";\n string current=\"\";\n vector<string> out={};\n int numc=0;\n s=s+' ';\n for (int i=0;i<s.length();i++)\n if (s[i]==' ')\n {\n if (numc==n) out.push_back(current);\n current=\"\";\n numc=0;\n }\n else\n ...
[ "ent+s[i];\n if" ]
string vowels="aeiouAEIOU"; string current=""; vector<string> out={}; int numc=0; s=s+' '; for (int i=0;i<s.length();i++) if (s[i]==' ') { if (numc==n) out.push_back(current); current=""; numc=0; } else { cur...
#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...
CPP/118_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> string get_closest_vowel(string word){
[ " string out=\"\";\n string vowels=\"AEIOUaeiou\";\n for (int i=word.length()-2;i>=1;i-=1)\n if (find(vowels.begin(),vowels.end(),word[i])!=vowels.end())\n if (find(vowels.begin(),vowels.end(),word[i+1])==vowels.end()", " if (find(vowels.begin(),vowels.end(),word[i-1])==vowels.end(...
[ ")\n " ]
string out=""; string vowels="AEIOUaeiou"; for (int i=word.length()-2;i>=1;i-=1) if (find(vowels.begin(),vowels.end(),word[i])!=vowels.end()) if (find(vowels.begin(),vowels.end(),word[i+1])==vowels.end()) if (find(vowels.begin(),vowels.end(),word[i-1])==vowels.end()) ...
#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...
CPP/119_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> string match_parens(vector<string> lst){
[ " string l1=lst[0]+lst[1];\n int i,count=0;\n bool can=true;\n for (i=0;i<l1.length();i++)\n {\n if (l1[i]=='(') count+=1;\n if (l1[i]==')') count-=1;\n ", " }\n if (count!=0) return \"No\";\n if (can==true) return \"Yes\";\n l1=lst[1]+lst[0];\n can=true...
[ " if (count<0) can=false;\n " ]
string l1=lst[0]+lst[1]; int i,count=0; bool can=true; for (i=0;i<l1.length();i++) { if (l1[i]=='(') count+=1; if (l1[i]==')') count-=1; if (count<0) can=false; } if (count!=0) return "No"; if (can==true) return "Yes"; l1=lst[1]+lst[0]; ...
#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"); ...
CPP/120_spans_1
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, -...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> maximum(vector<int> arr,int k){
[ " sort(", ";\n return out;\n}\n" ]
[ "arr.begin(),arr.end());\n vector<int> out(arr.end()-k,arr.end())" ]
sort(arr.begin(),arr.end()); vector<int> out(arr.end()-k,arr.end()); return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(maximum({-3, -4, 5}, 3) , {-4, -3, 5})); assert (issame(maximum({4, -4,...
CPP/121_spans_1
1
/* Given a non-empty vector of integers, return the sum of all of the odd elements that are in even positions. Examples solution({5, 8, 7, 1}) ==> 12 solution({3, 3, 3, 3, 3}) ==> 9 solution({30, 13, 24, 321}) ==>0 */ #include<stdio.h> #include<vector> using namespace std; int solutions(vector<int> lst){
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> int solutions(vector<int> lst){
[ "", ";\n return sum;\n}\n" ]
[ " int sum=0;\n for (int i=0;i*2<lst.size();i++)\n if (lst[i*2]%2==1) sum+=lst[i*2]" ]
int sum=0; for (int i=0;i*2<lst.size();i++) if (lst[i*2]%2==1) sum+=lst[i*2]; return sum; }
#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...
CPP/122_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> int add_elements(vector<int> arr,int k){
[ " int sum=0;\n for (int i=0;i<k;i++)\n if( arr[i]>=-99 and ar", " sum+=arr[i];\n return sum;\n}\n" ]
[ "r[i]<=99)\n " ]
int sum=0; for (int i=0;i<k;i++) if( arr[i]>=-99 and arr[i]<=99) sum+=arr[i]; return sum; }
#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},...
CPP/123_spans_1
1
/* Given a positive integer n, return a sorted vector that has the odd numbers in collatz sequence. The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term i...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> get_odd_collatz(int n){
[ " vector<int> out={1};\n while (n!=1)\n", "begin(),out.end());\n return out;\n}\n" ]
[ " {\n if (n%2==1) {out.push_back(n); n=n*3+1;}\n else n=n/2;\n }\n sort(out." ]
vector<int> out={1}; while (n!=1) { if (n%2==1) {out.push_back(n); n=n*3+1;} else n=n/2; } sort(out.begin(),out.end()); return out; }
#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...
CPP/124_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> bool valid_date(string date){
[ " int mm,dd,yy,i;\n if (date.length()!=10) return false;\n for (int i=0;i", " if (i==2 or i==5)\n {\n if (date[i]!='-') return false;\n }\n else\n if (date[i]<48 or date[i]>57) return false;\n\n mm=atoi(date.substr(0,2).c_str());\n dd=atoi(date.substr(3,2...
[ "<10;i++)\n " ]
int mm,dd,yy,i; if (date.length()!=10) return false; for (int i=0;i<10;i++) if (i==2 or i==5) { if (date[i]!='-') return false; } else if (date[i]<48 or date[i]>57) return false; mm=atoi(date.substr(0,2).c_str()); dd=atoi(date.substr(3,2).c_st...
#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") ==...
CPP/125_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> vector<string> split_words(string txt){
[ " int i;\n string current=\"\";\n vector<string> out={};\n if (find(txt.begin(),txt.end(),' ')!=txt.end())\n {\n txt=txt+' ';\n for (i=0;i<txt.length();i++)\n if (txt[i]==' ') \n {\n if (current.length()>0)out.push_back(current); \n cu...
[ "ngth();i++)\n if" ]
int i; string current=""; vector<string> out={}; if (find(txt.begin(),txt.end(),' ')!=txt.end()) { txt=txt+' '; for (i=0;i<txt.length();i++) if (txt[i]==' ') { if (current.length()>0)out.push_back(current); current=""; ...
#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...
CPP/126_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> bool is_sorted(vector<int> lst){
[ " for (int i=1;i<lst.size();i++)\n {\n if (lst[", "(i>=2 and lst[i]==lst[i-1] and lst[i]==lst[i-2]) return false;\n }\n return true;\n}\n" ]
[ "i]<lst[i-1]) return false;\n if " ]
for (int i=1;i<lst.size();i++) { if (lst[i]<lst[i-1]) return false; if (i>=2 and lst[i]==lst[i-1] and lst[i]==lst[i-2]) return false; } return true; }
#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, ...
CPP/127_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> string intersection( vector<int> interval1,vector<int> interval2){
[ " int inter1,inter2,l,i;\n inter1=max(interval1[0],interval2[0]);\n inter2=min(interval1[1],interval2[1]);\n ", "l%i==0) return \"NO\";\n return \"YES\";\n}\n" ]
[ " l=inter2-inter1;\n if (l<2) return \"NO\";\n for (i=2;i*i<=l;i++)\n if (" ]
int inter1,inter2,l,i; inter1=max(interval1[0],interval2[0]); inter2=min(interval1[1],interval2[1]); l=inter2-inter1; if (l<2) return "NO"; for (i=2;i*i<=l;i++) if (l%i==0) return "NO"; return "YES"; }
#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...
CPP/128_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> int prod_signs(vector<int> arr){
[ " if (arr.size()==0) return -32768;\n int i,sum=0,prods=1;\n for (i=0;i<arr.size();i++)\n ", "if (arr[i]==0) prods=0;\n if (arr[i]<0) prods=-prods;\n }\n return sum*prods;\n}\n" ]
[ " {\n sum+=abs(arr[i]);\n " ]
if (arr.size()==0) return -32768; int i,sum=0,prods=1; for (i=0;i<arr.size();i++) { sum+=abs(arr[i]); if (arr[i]==0) prods=0; if (arr[i]<0) prods=-prods; } return sum*prods; }
#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}) ==...
CPP/129_spans_1
1
/* Given a grid with N rows and N columns (N >= 2) and a positive integer k, each cell of the grid contains a value. Every integer in the range {1, N * N} inclusive appears exactly once on the cells of the grid. You have to find the minimum path of length k in the grid. You can start from any cell, and in each step y...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> minPath(vector<vector<int>> grid, int k){
[ " int i,j,x,y,min;\n for (i=0;i<grid.size();i++)\n for (j=0;j<grid[i].size();j++)\n if (grid[i][j]==1) {\n x=i;y=j;\n }\n min=grid.size()*grid.size();\n if (x>0 and grid[x-1][y]<min) min=grid[x-1][y];\n if (x<grid.size()-1 and grid[x+1][y]<min) min=grid[x+1...
[ ";\n for (i=0;i<k;i++)\n if (i%2==0) out.push_back(1);\n else out.push_back(min);" ]
int i,j,x,y,min; for (i=0;i<grid.size();i++) for (j=0;j<grid[i].size();j++) if (grid[i][j]==1) { x=i;y=j; } min=grid.size()*grid.size(); if (x>0 and grid[x-1][y]<min) min=grid[x-1][y]; if (x<grid.size()-1 and grid[x+1][y]<min) min=grid[x+1][y]; if ...
#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...
CPP/130_spans_1
1
/* 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....
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> tri(int n){
[ " vector<int> out={1,3};\n if (n==0) return {1};\n for (int i=2;i<=n;i++)\n {\n if (i%2==0) out.push_back(1+i/2);\n else out.push_back(out[i-1]+out[i-2]+1+(i+1)/2);\n }\n ", " out;\n}\n" ]
[ " return" ]
vector<int> out={1,3}; if (n==0) return {1}; for (int i=2;i<=n;i++) { if (i%2==0) out.push_back(1+i/2); else out.push_back(out[i-1]+out[i-2]+1+(i+1)/2); } return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(tri(3) , {1, 3, 2, 8})); assert (issame(tri(4) , {1, 3, 2, 8, 3})); ...
CPP/131_spans_1
1
/* Given a positive integer n, return the product of the odd digits. Return 0 if all digits are even. For example: digits(1) == 1 digits(4) == 0 digits(235) == 15 */ #include<stdio.h> #include<string> using namespace std; int digits(int n){
#include<stdio.h> #include<math.h> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> int digits(int n){
[ " int prod=1,has=0;\n string s=to_string(n);\n for (int i=0;i<s.length();i++)\n if (s[i]%2==1) \n {\n has=1", "\n }\n if (has==0) return 0;\n return prod;\n}\n" ]
[ ";\n prod=prod*(s[i]-48);" ]
int prod=1,has=0; string s=to_string(n); for (int i=0;i<s.length();i++) if (s[i]%2==1) { has=1; prod=prod*(s[i]-48); } if (has==0) return 0; return prod; }
#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); }
CPP/132_spans_1
1
/* 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 ...
#include<stdio.h> #include<math.h> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> bool is_nested(string str){
[ " int count=0,maxcount=0;\n for (int i=0;i<str.length();i++)\n {\n if (str[i]=='[') count+=1;\n if (str[i]==']') count-=1;\n if (count<0) count=0;\n if (count>maxcount) maxcount=count;\n if (", "count-2) return true;\n }\n return false;\n}\n" ]
[ "count<=max" ]
int count=0,maxcount=0; for (int i=0;i<str.length();i++) { if (str[i]=='[') count+=1; if (str[i]==']') count-=1; if (count<0) count=0; if (count>maxcount) maxcount=count; if (count<=maxcount-2) return true; } return 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...
CPP/133_spans_1
1
/* 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 = {...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> int sum_squares(vector<float> lst){
[ " int sum=0;\n ", ";\n}\n" ]
[ " for (int i=0;i<lst.size();i++)\n sum+=ceil(lst[i])*ceil(lst[i]);\n return sum" ]
int sum=0; for (int i=0;i<lst.size();i++) sum+=ceil(lst[i])*ceil(lst[i]); return sum; }
#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...
CPP/134_spans_1
1
/* 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 ...
#include<stdio.h> #include<math.h> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> bool check_if_last_char_is_a_letter(string txt){
[ " if (txt.length()==0) return false;\n char chr=txt[txt.length()-1];\n if (chr<65 or (chr>90 and chr<97) or chr>122) ", "e;\n if (txt.length()==1) return true;\n chr=txt[txt.length()-2];\n if ((chr>=65 and chr<=90) or (chr>=97 and chr<=122)) return false;\n return true;\n}\n" ]
[ "return fals" ]
if (txt.length()==0) return false; char chr=txt[txt.length()-1]; if (chr<65 or (chr>90 and chr<97) or chr>122) return false; if (txt.length()==1) return true; chr=txt[txt.length()-2]; if ((chr>=65 and chr<=90) or (chr>=97 and chr<=122)) return false; return true; }
#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...
CPP/135_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> int can_arrange(vector<int> arr){
[ " i", " for (int i=0;i<arr.size();i++)\n if (arr[i]<=i) max=i;\n return max;\n}\n" ]
[ "nt max=-1;\n " ]
int max=-1; for (int i=0;i<arr.size();i++) if (arr[i]<=i) max=i; return max; }
#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); }
CPP/136_spans_1
1
/* 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({}) == ...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> largest_smallest_integers(vector<int> lst){
[ " int maxneg=0,minpos=0;\n for (int i=0;i<lst.size();i++)\n {\n ", "<0 and (maxneg==0 or lst[i]>maxneg)) maxneg=lst[i];\n if (lst[i]>0 and (minpos==0 or lst[i]<minpos)) minpos=lst[i];\n }\n return {maxneg,minpos};\n}\n" ]
[ " if (lst[i]" ]
int maxneg=0,minpos=0; for (int i=0;i<lst.size();i++) { if (lst[i]<0 and (maxneg==0 or lst[i]>maxneg)) maxneg=lst[i]; if (lst[i]>0 and (minpos==0 or lst[i]<minpos)) minpos=lst[i]; } return {maxneg,minpos}; }
#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...
CPP/137_spans_1
1
/* 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"...
#include<stdio.h> #include<math.h> #include<string> #include<algorithm> #include<boost/any.hpp> using namespace std; #include<stdlib.h> boost::any compare_one(boost::any a,boost::any b){
[ " double numa,numb;\n boost::any out;\n \n if (a.type()==typeid(string))\n {\n string s;\n s=boost::any_cast<string>(a);\n if (find(s.begin(),s.end(),',')!=s.end())\n for (int i=0;i<s.length();i++)\n if (s[i]==',') s=s.substr(0,i)+'.'+s.substr(i+1);\n ...
[ "return string(\"None\");\n if (numa<numb) return b;\n" ]
double numa,numb; boost::any out; if (a.type()==typeid(string)) { string s; s=boost::any_cast<string>(a); if (find(s.begin(),s.end(),',')!=s.end()) for (int i=0;i<s.length();i++) if (s[i]==',') s=s.substr(0,i)+'.'+s.substr(i+1); numa=atof(...
#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...
CPP/138_spans_1
1
/* 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){
#include<stdio.h> #include<math.h> #include<algorithm> using namespace std; #include<stdlib.h> bool is_equal_to_sum_even(int n){
[ " if", "e;\n}\n" ]
[ " (n%2==0 and n>=8) return true;\n return fals" ]
if (n%2==0 and n>=8) return true; return false; }
#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) == ...
CPP/139_spans_1
1
/* The Brazilian factorial is defined as: brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1! where n > 0 For example: >>> special_factorial(4) 288 The function will receive an integer as input and should return the special factorial of this integer. */ #include<stdio.h> using namespace std; long long special_fa...
#include<stdio.h> #include<math.h> #include<algorithm> using namespace std; #include<stdlib.h> long long special_factorial(int n){
[ " long long fact=1,bfact=1;\n for (int i=1;i<=", "\n}\n" ]
[ "n;i++)\n {\n fact=fact*i;\n bfact=bfact*fact;\n }\n return bfact;" ]
long long fact=1,bfact=1; for (int i=1;i<=n;i++) { fact=fact*i; bfact=bfact*fact; } return bfact; }
#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); }
CPP/140_spans_1
1
/* 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...
#include<stdio.h> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> string fix_spaces(string text){
[ " string out=\"\";\n int spacelen=0;\n for (int i=0;i<text.length();i++)\n if (text[i]==' ') spacelen+=1;\n else\n {\n if (spacelen==1) out=out+'_';\n if (spacelen==2) out=out+\"__\";\n if (spacelen>2) out=out+'-';\n spacelen=0;\n out=out+text[i];\n }\n if ...
[ "pacelen==1) out=out+'_';\n if (spacele" ]
string out=""; int spacelen=0; for (int i=0;i<text.length();i++) if (text[i]==' ') spacelen+=1; else { if (spacelen==1) out=out+'_'; if (spacelen==2) out=out+"__"; if (spacelen>2) out=out+'-'; spacelen=0; out=out+text[i]; } if (spacelen==1) out=out...
#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...
CPP/141_spans_1
1
/* 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. - ...
#include<stdio.h> #include<string> #include<algorithm> using namespace std; #include<math.h> #include<stdlib.h> string file_name_check(string file_name){
[ " int numdigit=0,numdot=0;\n if (file_name.length()<5) return \"No\";\n char w=file_name[0];\n if (w<65 or (w>90 and w<97) or w>122) return \"No\";\n string last=file_name.substr(file_name.length()-4,4);\n if (last!=\".txt\" and last!=\".exe\" and last!=\".dll\") return \"No\";\n for (int i=0;i...
[ "length();i++)\n {\n if (file_name[i]>=48 and file_name[i]<=57) n" ]
int numdigit=0,numdot=0; if (file_name.length()<5) return "No"; char w=file_name[0]; if (w<65 or (w>90 and w<97) or w>122) return "No"; string last=file_name.substr(file_name.length()-4,4); if (last!=".txt" and last!=".exe" and last!=".dll") return "No"; for (int i=0;i<file_name.length();i++...
#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 ...
CPP/142_spans_1
1
/* " 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...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> int sum_squares(vector<int> lst){
[ " int sum=0;\n for (int", "e if (i%4==0) sum+=lst[i]*lst[i]*lst[i];\n else sum+=lst[i];\n return sum;\n}\n" ]
[ " i=0;i<lst.size();i++)\n if (i%3==0) sum+=lst[i]*lst[i];\n els" ]
int sum=0; for (int i=0;i<lst.size();i++) if (i%3==0) sum+=lst[i]*lst[i]; else if (i%4==0) sum+=lst[i]*lst[i]*lst[i]; else sum+=lst[i]; return sum; }
#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...
CPP/143_spans_1
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: ...
#include<stdio.h> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> string words_in_sentence(string sentence){
[ " string out=\"\";\n string current=\"\";\n sentence=sentence+' ';\n\n for (int i=0;i<sentence.size();i++)\n if (sentence[i]!=' ') current=current+sentence[i];\n else\n {\n bool isp=true;\n int l=current.length();\n if (l<2) isp=false;\n for (int j=2;", "p) out=out...
[ "j*j<=l;j++)\n if (l%j==0) isp=false;\n if (is" ]
string out=""; string current=""; sentence=sentence+' '; for (int i=0;i<sentence.size();i++) if (sentence[i]!=' ') current=current+sentence[i]; else { bool isp=true; int l=current.length(); if (l<2) isp=false; for (int j=2;j*j<=l;j++) if (l%j==0) ...
#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...
CPP/144_spans_1
1
/* Your task is to implement a function that will simplify the expression x * n. The function returns true if x * n evaluates to a whole number and false otherwise. Both x and n, are string representation of a fraction, and have the following format, <numerator>/<denominator> where both numerator and denominator are po...
#include<stdio.h> #include<string> #include<algorithm> using namespace std; #include<math.h> #include<stdlib.h> bool simplify(string x,string n){
[ " int a,b,c,d,i;\n for (i=0;i<x.size();i++)\n if (x[i]=='/') \n {\n a=atoi(x.substr(0,i).c_str());\n b=atoi", " for (i=0;i<n.size();i++)\n if (n[i]=='/') \n {\n c=atoi(n.substr(0,i).c_str());\n d=atoi(n.substr(i+1).c_str());\n }\...
[ "(x.substr(i+1).c_str());\n }\n " ]
int a,b,c,d,i; for (i=0;i<x.size();i++) if (x[i]=='/') { a=atoi(x.substr(0,i).c_str()); b=atoi(x.substr(i+1).c_str()); } for (i=0;i<n.size();i++) if (n[i]=='/') { c=atoi(n.substr(0,i).c_str()); d=atoi(n.substr(i+1).c_s...
#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)...
CPP/145_spans_1
1
/* Write a function which sorts the given vector of integers in ascending order according to the sum of their digits. Note: if there are several items with similar sum of their digits, order them based on their index in original vector. For example: >>> order_by_points({1, 11, -1, -11, -12}) == {-1, -11, 1, -12, 11} >...
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> order_by_points(vector<int> nums){
[ " vector<int> sumdigit={};\n for (int i=0;i<nums.size();i++)\n {\n string w=to_string(abs(nums[i]));\n int sum=0;\n for (int j=1;j<w.length();j++)\n sum+=w[j]-48;\n if (nums[i]>0) sum+=w[0]-48;\n else sum-=w[0]-48;\n sumdigit.push_back(sum);\n }\n ...
[ "e();j" ]
vector<int> sumdigit={}; for (int i=0;i<nums.size();i++) { string w=to_string(abs(nums[i])); int sum=0; for (int j=1;j<w.length();j++) sum+=w[j]-48; if (nums[i]>0) sum+=w[0]-48; else sum-=w[0]-48; sumdigit.push_back(sum); } int m; for (...
#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...
CPP/146_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> int specialFilter(vector<int> nums){
[ " int num=0;\n for (int i=0;i<nums.size();i++)\n if (nums", "length()-1]%2==1) num+=1;\n }\n return num;\n}\n" ]
[ "[i]>10)\n {\n string w=to_string(nums[i]);\n if (w[0]%2==1 and w[w." ]
int num=0; for (int i=0;i<nums.size();i++) if (nums[i]>10) { string w=to_string(nums[i]); if (w[0]%2==1 and w[w.length()-1]%2==1) num+=1; } return num; }
#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, ...
CPP/147_spans_1
1
/* 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...
#include<stdio.h> #include<vector> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> int get_matrix_triples(int n){
[ " vector<int> a;\n vector<vector<int>> sum={{0,0,0}};\n vector<vector<int>> sum2={{0,0,0}};\n for (int i=1;i<=n;i++)\n {\n a.push_back((i*i-i+1)%3);\n sum.push_back(sum[sum.size()-1]);\n sum[i][a[i-1]]+=1;\n }\n for (int times=1;times<3;times++)\n {\n for (int i=1;i<=...
[ "2;j++)\n" ]
vector<int> a; vector<vector<int>> sum={{0,0,0}}; vector<vector<int>> sum2={{0,0,0}}; for (int i=1;i<=n;i++) { a.push_back((i*i-i+1)%3); sum.push_back(sum[sum.size()-1]); sum[i][a[i-1]]+=1; } for (int times=1;times<3;times++) { for (int i=1;i<=n;i++) { ...
#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); }
CPP/148_spans_1
1
/* There are eight planets in our solar system: the closerst to the Sun is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn, Uranus, Neptune. Write a function that takes two planet names as strings planet1 and planet2. The function should return a vector containing all planets whose orbits are loca...
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> vector<string> bf(string planet1,string planet2){
[ " vector<string> planets={\"Mercury\",\"Venus\",\"Earth\",\"Mars\",\"Jupiter\",\"Saturn\",\"Uranus\",\"Neptune\"};\n int pos1=-1,pos2=-1,m;\n ", "(planets[m]==planet2) pos2=m;\n }\n if (pos1==-1 or pos2==-1) return {};\n if (pos1>pos2) {m=pos1;pos1=pos2;pos2=m;}\n vector<string> out={};\n ...
[ "for (m=0;m<planets.size();m++)\n {\n if (planets[m]==planet1) pos1=m;\n if " ]
vector<string> planets={"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"}; int pos1=-1,pos2=-1,m; for (m=0;m<planets.size();m++) { if (planets[m]==planet1) pos1=m; if (planets[m]==planet2) pos2=m; } if (pos1==-1 or pos2==-1) return {}; if (pos1>pos2) {m=pos1;pos...
#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...
CPP/149_spans_1
1
/* 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 ...
#include<stdio.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<math.h> #include<stdlib.h> vector<string> sorted_list_sum(vector<string> lst){
[ " vector<string> out={};\n for (int i=0;i<lst.size();i++)\n if (lst[i].length()%2==0) out.push_back(lst[i]);\n ", "out.begin(),out.end());\n for (int i=0;i<out.size();i++)\n for (int j=1;j<out.size();j++)\n if (out[j].length()<out[j-1].length())\n {\n mid=out[j];out[j]=out[j-1];out[j-...
[ " string mid;\n sort(" ]
vector<string> out={}; for (int i=0;i<lst.size();i++) if (lst[i].length()%2==0) out.push_back(lst[i]); string mid; sort(out.begin(),out.end()); for (int i=0;i<out.size();i++) for (int j=1;j<out.size();j++) if (out[j].length()<out[j-1].length()) { mid=out[j];out[j]=out[j-1];ou...
#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...
CPP/150_spans_1
1
/* A simple program which should return the value of x if n is a prime number and should return the value of y otherwise. Examples: for x_or_y(7, 34, 12) == 34 for x_or_y(15, 8, 5) == 5 */ #include<stdio.h> using namespace std; int x_or_y(int n,int x,int y){
#include<stdio.h> #include<math.h> #include<algorithm> using namespace std; #include<stdlib.h> int x_or_y(int n,int x,int y){
[ " bool isp=true;\n if (n<2) isp=false;\n for (int i=2;i", "=false;\n if (isp) return x;\n return y;\n}\n" ]
[ "*i<=n;i++)\n if (n%i==0) isp" ]
bool isp=true; if (n<2) isp=false; for (int i=2;i*i<=n;i++) if (n%i==0) isp=false; if (isp) return x; return y; }
#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...
CPP/151_spans_1
1
/* 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 ...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> long long double_the_difference(vector<float> lst){
[ " long lo", "i]-round(lst[i])<1e-4)\n if (lst[i]>0 and (int)(round(lst[i]))%2==1) sum+=(int)(round(lst[i]))*(int)(round(lst[i]));\n return sum;\n}\n" ]
[ "ng sum=0;\n for (int i=0;i<lst.size();i++)\n if (lst[" ]
long long sum=0; for (int i=0;i<lst.size();i++) if (lst[i]-round(lst[i])<1e-4) if (lst[i]>0 and (int)(round(lst[i]))%2==1) sum+=(int)(round(lst[i]))*(int)(round(lst[i])); return sum; }
#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...
CPP/152_spans_1
1
/* 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 ...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> vector<int> compare(vector<int> game,vector<int> guess){
[ " ", "e();i++)\n out.push_back(abs(game[i]-guess[i]));\n return out;\n}\n" ]
[ " vector<int> out;\n for (int i=0;i<game.siz" ]
vector<int> out; for (int i=0;i<game.size();i++) out.push_back(abs(game[i]-guess[i])); return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(compare({1,2,3,4,5,1},{1,2,3,4,2,-2}),{0,0,0,0,3,3})); assert (issame(c...
CPP/153_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> string Strongest_Extension(string class_name,vector<string> extensions){
[ " string strongest=\"\";\n int max=-1000;\n for (int i=0;i<extensions.size();i++)\n {\n int strength=0;\n for (int j=0;j<extensions[i].length();j++)\n {\n char chr=extensions[i][j];\n if (chr>=65 and chr<=90) strength+=1;\n if (chr>=97 and chr<=122) ...
[ "st=extensions[i];\n " ]
string strongest=""; int max=-1000; for (int i=0;i<extensions.size();i++) { int strength=0; for (int j=0;j<extensions[i].length();j++) { char chr=extensions[i][j]; if (chr>=65 and chr<=90) strength+=1; if (chr>=97 and chr<=122) strength-=1; ...
#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",...
CPP/154_spans_1
1
/* 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")...
#include<stdio.h> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> bool cycpattern_check(string a,string b){
[ " for (int i=0;", ";i++)\n {\n string rotate=b.substr(i)+b.substr(0,i);\n if (a.find(rotate)!=string::npos) return true;\n }\n return false;\n\n}\n" ]
[ "i<b.size()" ]
for (int i=0;i<b.size();i++) { string rotate=b.substr(i)+b.substr(0,i); if (a.find(rotate)!=string::npos) return true; } return false; }
#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") == ...
CPP/155_spans_1
1
/* 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){
#include<stdio.h> #include<math.h> #include<string> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> vector<int> even_odd_count(int num){
[ " string w=to_string(abs(num));\n int n1=0,n2=0;\n for (int i=0;i<w.length();i", " return {n2,n1};\n}\n" ]
[ "++)\n if (w[i]%2==1) n1+=1;\n else n2+=1;\n" ]
string w=to_string(abs(num)); int n1=0,n2=0; for (int i=0;i<w.length();i++) if (w[i]%2==1) n1+=1; else n2+=1; return {n2,n1}; }
#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, ...
CPP/156_spans_1
1
/* Given a positive integer, obtain its roman numeral equivalent as a string, and return it in lowercase. Restrictions: 1 <= num <= 1000 Examples: >>> int_to_mini_roman(19) == "xix" >>> int_to_mini_roman(152) == "clii" >>> int_to_mini_roman(426) == "cdxxvi" */ #include<stdio.h> #include<vector> #include<string> using ...
#include<stdio.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> string int_to_mini_romank(int number){
[ " string current=\"\";\n vector<string> rep={\"m\",\"cm\",\"d\",\"cd\",\"c\",\"xc\",\"l\",\"xl\",\"x\",\"ix\",\"v\",\"iv\",\"i\"};\n vector<int> num={1000,900,500,400,100,90,50,40,10,9,5,4,1};\n int pos=0;\n while(number>0)\n {\n while (number>=num[pos])\n {\n current=curr...
[ "nt+rep[pos];\n number-=num[pos];\n }\n if (" ]
string current=""; vector<string> rep={"m","cm","d","cd","c","xc","l","xl","x","ix","v","iv","i"}; vector<int> num={1000,900,500,400,100,90,50,40,10,9,5,4,1}; int pos=0; while(number>0) { while (number>=num[pos]) { current=current+rep[pos]; number-=num[pos...
#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) == "...
CPP/157_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> bool right_angle_triangle(float a,float b,float c){
[ " ", "b)<1e-4 or abs(b*b+c*c-a*a)<1e-4) return true;\n return false;\n}\n" ]
[ " if (abs(a*a+b*b-c*c)<1e-4 or abs(a*a+c*c-b*" ]
if (abs(a*a+b*b-c*c)<1e-4 or abs(a*a+c*c-b*b)<1e-4 or abs(b*b+c*c-a*a)<1e-4) return true; return false; }
#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...
CPP/158_spans_1
1
/* 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...
#include<stdio.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<math.h> #include<stdlib.h> string find_max(vector<string> words){
[ " stri", " string unique=\"\";\n for (int j=0;j<words[i].length();j++)\n if (find(unique.begin(),unique.end(),words[i][j])==unique.end())\n unique=unique+words[i][j];\n if (unique.length()>maxu or (unique.length()==maxu and words[i]<max))\n {\n ma...
[ "ng max=\"\";\n int maxu=0;\n for (int i=0;i<words.size();i++)\n {\n " ]
string max=""; int maxu=0; for (int i=0;i<words.size();i++) { string unique=""; for (int j=0;j<words[i].length();j++) if (find(unique.begin(),unique.end(),words[i][j])==unique.end()) unique=unique+words[i][j]; if (unique.length()>maxu or (unique.length...
#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",...
CPP/159_spans_1
1
/* 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 ...
#include<stdio.h> #include<vector> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<int> eat(int number,int need,int remaining){
[ " if (need>remainin", "need};\n}\n" ]
[ "g) return {number+remaining, 0};\n return {number+need,remaining-" ]
if (need>remaining) return {number+remaining, 0}; return {number+need,remaining-need}; }
#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})); ...
CPP/160_spans_1
1
/* 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...
#include<stdio.h> #include<math.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> int do_algebra(vector<string> operato, vector<int> operand){
[ " vector<int> num={};\n vector<int> posto={};\n for (int i=0;i<operand.size();i++)\n posto.push_back(i);\n for (int i=0;i<operato.size();i++)\n if (operato[i]==\"**\") \n {\n while (posto[posto[i]]!=posto[i]) posto[i]=posto[posto[i]];\n while (posto[posto[i+1]]!=posto[...
[ "o[i]==" ]
vector<int> num={}; vector<int> posto={}; for (int i=0;i<operand.size();i++) posto.push_back(i); for (int i=0;i<operato.size();i++) if (operato[i]=="**") { while (posto[posto[i]]!=posto[i]) posto[i]=posto[posto[i]]; while (posto[posto[i+1]]!=posto[i+1]) posto[i+1...
#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); }
CPP/161_spans_1
1
/* 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<...
#include<stdio.h> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> string solve(string s){
[ " int nletter=0;\n string out=\"\";\n for (int i=0;i<s.length();i++)\n {\n char w=s", "-32;\n else nletter+=1;\n out=out+w;\n }\n if (nletter==s.length())\n {\n string p(s.rbegin(),s.rend());\n return p;\n }\n else return out;\n}\n" ]
[ "[i];\n if (w>=65 and w<=90) w=w+32;\n else if (w>=97 and w<=122) w=w" ]
int nletter=0; string out=""; for (int i=0;i<s.length();i++) { char w=s[i]; if (w>=65 and w<=90) w=w+32; else if (w>=97 and w<=122) w=w-32; else nletter+=1; out=out+w; } if (nletter==s.length()) { string p(s.rbegin(),s.rend()); return p...
#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"); ...
CPP/162_spans_1
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){
#include<stdio.h> #include<string> #include<openssl/md5.h> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> string string_to_md5(string text){
[ " unsigned char md[16];\n if (text.length()==0) return \"None\";\n MD5_CTX c;\n int i;\n MD5_Init(&c);\n MD5_Update(&c, (unsigned char*)text.c_str(), text.length());\n MD5_Final(md, &c);\n string out_str=\"\";\n for (int i=0;i<16;i++)\n {\n char w;\n if (md[i]<1...
[ "0) w=48+md[i]%16;\n else w=87+md[i]%16;\n " ]
unsigned char md[16]; if (text.length()==0) return "None"; MD5_CTX c; int i; MD5_Init(&c); MD5_Update(&c, (unsigned char*)text.c_str(), text.length()); MD5_Final(md, &c); string out_str=""; for (int i=0;i<16;i++) { char w; if (md[i]<160) w=48+md[i]/16; ...
#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");...
CPP/163_spans_1
1
/* 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...
#include<stdio.h> #include<vector> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<int> generate_integers(int a,int b){
[ " int m;\n if (", " if (i<10 and i%2==0) out.push_back(i);\n return out;\n}\n" ]
[ "b<a)\n {\n m=a;a=b;b=m;\n }\n\n vector<int> out={};\n for (int i=a;i<=b;i++)\n " ]
int m; if (b<a) { m=a;a=b;b=m; } vector<int> out={}; for (int i=a;i<=b;i++) if (i<10 and i%2==0) out.push_back(i); return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(generate_integers(2, 10) , {2, 4, 6, 8})); assert (issame(generate_inte...
CPP/0_spans_2
2
/* Check if in given vector of numbers, are any two numbers closer to each other than given threshold. >>> has_close_elements({1.0, 2.0, 3.0}, 0.5) false >>> has_close_elements({1.0, 2.8, 3.0, 4.0, 5.0, 2.0}, 0.3) true */ #include<stdio.h> #include<vector> #include<math.h> using namespace std; bool has_close_elements(v...
#include<stdio.h> #include<vector> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> bool has_close_elements(vector<float> numbers, float threshold){
[ " i", "=i+1;j<numbers.size();j++)\n if (abs(numbers[i]-numbers[j])<thres", "se;\n}\n\n" ]
[ "nt i,j;\n \n for (i=0;i<numbers.size();i++)\n for (j", "hold)\n return true;\n\n return fal" ]
int i,j; for (i=0;i<numbers.size();i++) for (j=i+1;j<numbers.size();j++) if (abs(numbers[i]-numbers[j])<threshold) return true; return false; }
#undef NDEBUG #include<assert.h> int main(){ vector<float> a={1.0, 2.0, 3.9, 4.0, 5.0, 2.2}; assert (has_close_elements(a, 0.3)==true); assert (has_close_elements(a, 0.05) == false); assert (has_close_elements({1.0, 2.0, 5.9, 4.0, 5.0}, 0.95) == true); assert (has_close_elements({1.0, 2.0, 5.9, 4.0...
CPP/1_spans_2
2
/* Input to this function is a string containing multiple groups of nested parentheses. Your goal is to separate those group into separate strings and return the vector of those. Separate groups are balanced (each open brace is properly closed) and not nested within each other Ignore any spaces in the input string. >>>...
#include<stdio.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<string> separate_paren_groups(string paren_string){
[ " vector<string> all_parens;\n string current_paren;\n int level=0;\n char chr;\n int i;\n for (i=0;i<paren_string.length();i++)\n {\n chr=paren_string[i];\n if (chr=='(')\n {\n level+=1;\n current_pa", " {\n ", ";\n if (level==0){\n ...
[ "ren+=chr;\n }\n if (chr==')')\n ", " level-=1;\n current_paren+=chr" ]
vector<string> all_parens; string current_paren; int level=0; char chr; int i; for (i=0;i<paren_string.length();i++) { chr=paren_string[i]; if (chr=='(') { level+=1; current_paren+=chr; } if (chr==')') { level-=1; ...
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(separate_paren_groups("(()()) ((())) () ((())()())"),{"(()())", "((...
CPP/2_spans_2
2
/* Given a positive floating point number, it can be decomposed into and integer part (largest integer smaller than given number) and decimals (leftover part always smaller than 1). Return the decimal part of the number. >>> truncate_number(3.5) 0.5 */ #include<stdio.h> #include<math.h> using namespace std; float trun...
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> float truncate_number(float number){
[ " ", "um", "}\n" ]
[ " return number-int(n", "ber);\n" ]
return number-int(number); }
#undef NDEBUG #include<assert.h> int main(){ assert (truncate_number(3.5) == 0.5); assert (abs(truncate_number(1.33) - 0.33) < 1e-4); assert (abs(truncate_number(123.456) - 0.456) < 1e-4); }
CPP/3_spans_2
2
/* You"re given a vector of deposit and withdrawal operations on a bank account that starts with zero balance. Your task is to detect if at any point the balance of account falls below zero, and at that point function should return true. Otherwise it should return false. >>> below_zero({1, 2, 3}) false >>> below_zero({...
#include<stdio.h> #include<vector> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> bool below_zero(vector<int> operations){
[ " int num=0;\n for (int i=0;i<operations.size();i++)\n {\n num+=operations[i];\n if ", "rue;\n ", " return false;\n}\n" ]
[ "(num<0) return t", " }\n " ]
int num=0; for (int i=0;i<operations.size();i++) { num+=operations[i]; if (num<0) return true; } return false; }
#undef NDEBUG #include<assert.h> int main(){ assert (below_zero({}) == false); assert (below_zero({1, 2, -3, 1, 2, -3}) == false); assert (below_zero({1, 2, -4, 5, 6}) == true); assert (below_zero({1, -1, 2, -2, 5, -5, 4, -4}) == false); assert (below_zero({1, -1, 2, -2, 5, -5, 4, -5}) == true); ...
CPP/4_spans_2
2
/* For a given vector of input numbers, calculate Mean Absolute Deviation around the mean of this dataset. Mean Absolute Deviation is the average absolute difference between each element and a centerpoint (mean in this case): MAD = average | x - x_mean | >>> mean_absolute_deviation({1.0, 2.0, 3.0, 4.0}) 1.0 */ #include...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> float mean_absolute_deviation(vector<float> numbers){
[ " float sum=0;\n float avg,msum,mavg;\n int i=0;\n for (i=0;i<numbers.size(", "e();i++)\n msum+=a", " return msum/numbers.size();\n}\n" ]
[ ");i++)\n sum+=numbers[i];\n avg=sum/numbers.size();\n msum=0;\n for (i=0;i<numbers.siz", "bs(numbers[i]-avg);\n " ]
float sum=0; float avg,msum,mavg; int i=0; for (i=0;i<numbers.size();i++) sum+=numbers[i]; avg=sum/numbers.size(); msum=0; for (i=0;i<numbers.size();i++) msum+=abs(numbers[i]-avg); return msum/numbers.size(); }
#undef NDEBUG #include<assert.h> int main(){ assert (abs(mean_absolute_deviation({1.0, 2.0, 3.0}) - 2.0/3.0) < 1e-4); assert (abs(mean_absolute_deviation({1.0, 2.0, 3.0, 4.0}) - 1.0) < 1e-4); assert (abs(mean_absolute_deviation({1.0, 2.0, 3.0, 4.0, 5.0}) - 6.0/5.0) < 1e-4); }
CPP/5_spans_2
2
/* Insert a number "delimeter" between every two consecutive elements of input vector `numbers" >>> intersperse({}, 4) {} >>> intersperse({1, 2, 3}, 4) {1, 4, 2, 4, 3} */ #include<stdio.h> #include<vector> using namespace std; vector<int> intersperse(vector<int> numbers, int delimeter){
#include<stdio.h> #include<vector> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<int> intersperse(vector<int> numbers, int delimeter){
[ " vector<int> out=", "ers.size()>0) out.push_back(numbers[0]);\n for (int i=1;i<numbers.size();i++)\n {\n ", "ck(numbers[i]);\n\n }\n return out;\n}\n" ]
[ "{};\n if (numb", "out.push_back(delimeter);\n out.push_ba" ]
vector<int> out={}; if (numbers.size()>0) out.push_back(numbers[0]); for (int i=1;i<numbers.size();i++) { out.push_back(delimeter); out.push_back(numbers[i]); } return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(intersperse({}, 7), {})); assert (issame(intersperse({5, 6, 3,...
CPP/6_spans_2
2
/* Input to this function is a string represented multiple groups for nested parentheses separated by spaces. For each of the group, output the deepest level of nesting of parentheses. E.g. (()()) has maximum two levels of nesting while ((())) has three. >>> parse_nested_parens("(()()) ((())) () ((())()())") {2, 3, 1,...
#include<stdio.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<int> parse_nested_parens(string paren_string){
[ " vector<int> all_levels;\n string current_paren;\n int l", " chr=paren_string[i];\n if (chr=='(')\n {\n level+=1;\n if (level>max_level) max_level=level;\n current_p", " {\n level-=1;\n current_paren+=chr;\n if (level==0){...
[ "evel=0,max_level=0;\n char chr;\n int i;\n for (i=0;i<paren_string.length();i++)\n {\n ", "aren+=chr;\n }\n if (chr==')')\n" ]
vector<int> all_levels; string current_paren; int level=0,max_level=0; char chr; int i; for (i=0;i<paren_string.length();i++) { chr=paren_string[i]; if (chr=='(') { level+=1; if (level>max_level) max_level=level; current_paren+=chr; } ...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(parse_nested_parens("(()()) ((())) () ((())()())"),{2, 3, 1, 3})); ...
CPP/7_spans_2
2
/* Filter an input vector of strings only for ones that contain given substring >>> filter_by_substring({}, "a") {} >>> filter_by_substring({"abc", "bacd", "cde", "vector"}, "a") {"abc", "bacd", "vector"} */ #include<stdio.h> #include<vector> #include<string> using namespace std; vector<string> filter_by_substring(vect...
#include<stdio.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<string> filter_by_substring(vector<string> strings, string substring){
[ " vector<string> out;\n ", "st", "}\n" ]
[ " for (int i=0;i<strings.size();i++)\n {\n if (strings[i].find(sub", "ring)!=strings[i].npos)\n out.push_back(strings[i]);\n }\n return out;\n" ]
vector<string> out; for (int i=0;i<strings.size();i++) { if (strings[i].find(substring)!=strings[i].npos) out.push_back(strings[i]); } return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(filter_by_substring({}, "john"),{})); assert (issame(filter_by_s...
CPP/8_spans_2
2
/* For a given vector of integers, return a vector consisting of a sum and a product of all the integers in a vector. Empty sum should be equal to 0 and empty product should be equal to 1. >>> sum_product({}) (0, 1) >>> sum_product({1, 2, 3, 4}) (10, 24) */ #include<stdio.h> #include<vector> using namespace std; vector...
#include<stdio.h> #include<vector> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<int> sum_product(vector<int> numbers){
[ " int sum=0,product=1;\n f", "ct*=numbers[i];\n }\n retu", "};\n}\n" ]
[ "or (int i=0;i<numbers.size();i++)\n {\n sum+=numbers[i];\n produ", "rn {sum,product" ]
int sum=0,product=1; for (int i=0;i<numbers.size();i++) { sum+=numbers[i]; product*=numbers[i]; } return {sum,product}; }
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(sum_product({}) ,{0, 1})); assert (issame(sum_product({1, 1, 1}), {...
CPP/9_spans_2
2
/* From a given vector of integers, generate a vector of rolling maximum element found until given moment in the sequence. >>> rolling_max({1, 2, 3, 2, 3, 4, 2}) {1, 2, 3, 3, 3, 4, 4} */ #include<stdio.h> #include<vector> using namespace std; vector<int> rolling_max(vector<int> numbers){
#include<stdio.h> #include<vector> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<int> rolling_max(vector<int> numbers){
[ " vector<int> out;\n ", " for (int i=0;i<numbers.size", " return out;\n}\n" ]
[ "int max=0;\n ", "();i++)\n {\n if (numbers[i]>max) max=numbers[i];\n out.push_back(max);\n }\n " ]
vector<int> out; int max=0; for (int i=0;i<numbers.size();i++) { if (numbers[i]>max) max=numbers[i]; out.push_back(max); } return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(rolling_max({}),{})); assert (issame(rolling_max({1, 2, 3, 4}) , {...
CPP/10_spans_2
2
#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; #include<algorithm> #include<math.h> #include<stdlib.h> bool is_palindrome(string str){ string s(str.rbegin(),str.rend()); return s==str; } string make_palindrome(string str){
[ " int i;\n for (i=0;i<str.length();", "{\n string nstr;\n nstr=str.substr(0,i);\n string n2str(nstr.rbegin(),nstr.rend());\n return str+n2str;\n }\n }\n string n", "begin(),str.rend());\n return str+n2str;\n}\n" ]
[ "i++)\n {\n string rstr=str.substr(i);\n if (is_palindrome(rstr))\n ", "2str(str.r" ]
int i; for (i=0;i<str.length();i++) { string rstr=str.substr(i); if (is_palindrome(rstr)) { string nstr; nstr=str.substr(0,i); string n2str(nstr.rbegin(),nstr.rend()); return str+n2str; } } string n2str(str.rbegin(),str.rend(...
#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"); }
CPP/11_spans_2
2
/* Input are two strings a and b consisting only of 1s and 0s. Perform binary XOR on these inputs and return result also as a string. >>> string_xor("010", "110") "100" */ #include<stdio.h> #include<string> using namespace std; string string_xor(string a,string b){
#include<stdio.h> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> string string_xor(string a,string b){
[ " string output=\"\";\n for (int i=0;(i<a.length() and i<b.length());i++)\n {\n if (i<", " output+='0';\n } \n else output+='1';\n }\n else\n {\n ", " else output+=a[i];\n }\n }\n return output;\n}\n" ]
[ "a.length() and i<b.length())\n {\n if (a[i]== b[i]) \n {\n ", " if (i>=a.length()) \n {\n output+=b[i];\n }\n " ]
string output=""; for (int i=0;(i<a.length() and i<b.length());i++) { if (i<a.length() and i<b.length()) { if (a[i]== b[i]) { output+='0'; } else output+='1'; } else { if (i>=a.length()) ...
#undef NDEBUG #include<assert.h> int main(){ assert (string_xor("111000", "101010") == "010010"); assert (string_xor("1", "1") == "0"); assert (string_xor("0101", "0000") == "0101"); }
CPP/12_spans_2
2
/* Out of vector of strings, return the longest one. Return the first one in case of multiple strings of the same length. Return None in case the input vector is empty. >>> longest({}) >>> longest({"a", "b", "c"}) "a" >>> longest({"a", "bb", "ccc"}) "ccc" */ #include<stdio.h> #include<vector> #include<string> using na...
#include<stdio.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> string longest(vector<string> strings){
[ " strin", " i=0;i<strings.size();i++)\n {\n if (strings[i].length()>o", "=strings[i];\n }\n return out;\n}\n" ]
[ "g out;\n for (int", "ut.length()) out" ]
string out; for (int i=0;i<strings.size();i++) { if (strings[i].length()>out.length()) out=strings[i]; } return out; }
#undef NDEBUG #include<assert.h> int main(){ assert (longest({}) == ""); assert (longest({"x", "y", "z"}) == "x"); assert (longest({"x", "yyy", "zzzz", "www", "kkkk", "abc"}) == "zzzz"); }
CPP/13_spans_2
2
/* 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){
#include<stdio.h> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> int greatest_common_divisor(int a, int b){
[ " int out,m;\n whil", " ", " return b;\n }\n}\n" ]
[ "e (true){\n if (a<b) \n ", " {\n m=a;a=b;b=m;\n }\n a=a%b;\n if (a==0)" ]
int out,m; while (true){ if (a<b) { m=a;a=b;b=m; } a=a%b; if (a==0) return b; } }
#undef NDEBUG #include<assert.h> int main(){ assert (greatest_common_divisor(3, 7) == 1); assert (greatest_common_divisor(10, 15) == 5); assert (greatest_common_divisor(49, 14) == 7); assert (greatest_common_divisor(144, 60) == 12); }
CPP/14_spans_2
2
/* Return vector of all prefixes from shortest to longest of the input string >>> all_prefixes("abc") {"a", "ab", "abc"} */ #include<stdio.h> #include<vector> #include<string> using namespace std; vector<string> all_prefixes(string str){
#include<stdio.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<math.h> #include<stdlib.h> vector<string> all_prefixes(string str){
[ " vector<string", "(int i=0;i<str.length();i++)\n {\n current=curre", " return out;\n}\n" ]
[ "> out;\n string current=\"\";\n for ", "nt+str[i];\n out.push_back(current);\n }\n" ]
vector<string> out; string current=""; for (int i=0;i<str.length();i++) { current=current+str[i]; out.push_back(current); } return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(all_prefixes(""),{})); assert (issame(all_prefixes("asdfgh") ,{"a...
CPP/15_spans_2
2
/* 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){
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> string string_sequence(int n){
[ " ", "g out", "turn out;\n}\n" ]
[ "strin", "=\"0\";\n for (int i=1;i<=n;i++)\n out=out+\" \"+to_string(i);\n re" ]
string out="0"; for (int i=1;i<=n;i++) out=out+" "+to_string(i); return out; }
#undef NDEBUG #include<assert.h> int main(){ assert (string_sequence(0) == "0"); assert (string_sequence(3) == "0 1 2 3"); assert (string_sequence(10) == "0 1 2 3 4 5 6 7 8 9 10"); }
CPP/16_spans_2
2
/* Given a string, find out how many distinct characters (regardless of case) does it consist of >>> count_distinct_characters("xyzXYZ") 3 >>> count_distinct_characters("Jerry") 4 */ #include<stdio.h> #include<vector> #include<string> #include<algorithm> using namespace std; int count_distinct_characters(string str){
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; #include<stdlib.h> int count_distinct_characters(string str){
[ " vector<char> distinct={};\n transform(str.begin(),str.end(),str.begin(),::tolower);\n for (int i=0;i<str.size();i++)\n {\n bool isin=false;\n for (int j=0;j<distinct.size();j++)\n ", "t[j]==str[i])\n ", "alse) distinct.push_back(str[i]);\n\n }\n return distinct.size...
[ "if (distinc", " isin=true;\n if (isin==f" ]
vector<char> distinct={}; transform(str.begin(),str.end(),str.begin(),::tolower); for (int i=0;i<str.size();i++) { bool isin=false; for (int j=0;j<distinct.size();j++) if (distinct[j]==str[i]) isin=true; if (isin==false) distinct.push_back(str[i]); ...
#undef NDEBUG #include<assert.h> int main(){ assert (count_distinct_characters("") == 0); assert (count_distinct_characters("abcde") == 5); assert (count_distinct_characters("abcdecadeCADE") == 5); assert (count_distinct_characters("aaaaAAAAaaaa") == 1); assert (count_distinct_characters("Jerry jERR...
CPP/17_spans_2
2
/* 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...
#include<stdio.h> #include<math.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> vector<int> parse_music(string music_string){
[ " string current=\"\";\n vector<int> out", "int i=0;i<music_string.length();i++)\n {\n if (music_string[i]==' ')\n", " {\n if (current==\"o\") out.push_back(4);\n if (current==\"o|\") out.push_back(2);\n if (current==\".|\") out.push_back(1);\n curren...
[ "={};\n if (music_string.length()>0)\n music_string=music_string+' ';\n for (", " " ]
string current=""; vector<int> out={}; if (music_string.length()>0) music_string=music_string+' '; for (int i=0;i<music_string.length();i++) { if (music_string[i]==' ') { if (current=="o") out.push_back(4); if (current=="o|") out.push_back(2); ...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(parse_music("") , {})); assert (issame(parse_music("o o o o") ,{4,...
CPP/18_spans_2
2
/* Find how many times a given substring can be found in the original string. Count overlaping cases. >>> how_many_times("", "a") 0 >>> how_many_times("aaa", "a") 3 >>> how_many_times("aaaa", "aa") 3 */ #include<stdio.h> #include<string> using namespace std; int how_many_times(string str,string substring){
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> int how_many_times(string str,string substring){
[ "", "n 0;\n for (int i=0;i<=str.length()-substring.len", "}\n" ]
[ " int out=0;\n if (str.length()==0) retur", "gth();i++)\n if (str.substr(i,substring.length())==substring)\n out+=1;\n return out;\n" ]
int out=0; if (str.length()==0) return 0; for (int i=0;i<=str.length()-substring.length();i++) if (str.substr(i,substring.length())==substring) out+=1; return out; }
#undef NDEBUG #include<assert.h> int main(){ assert (how_many_times("", "x") == 0); assert (how_many_times("xyxyxyx", "x") == 4); assert (how_many_times("cacacacac", "cac") == 4); assert (how_many_times("john doe", "john") == 1); }
CPP/19_spans_2
2
/* 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...
#include<stdio.h> #include<math.h> #include<string> #include<map> using namespace std; #include<algorithm> #include<stdlib.h> string sort_numbers(string numbers){
[ " map<string,int> tonum={{\"zero\",0},{\"one\",1},{\"two\",2},{\"three\",3},{\"four\",4},{\"five\",5},{\"six\",6},{\"seven\",7},{\"eight\",8},{\"nine\",9}};\n map<int,string> numto={{0,\"zero\"},{1,\"one\"},{2,\"two\"", "\"four\"},{5,\"five\"},{6,\"six\"},{7,\"seven\"},{8,\"eight\"},{9,\"nine\"}};\n int ...
[ "},{3,\"three\"},{4,", "i<10;i++)\n for (int j=0;j<count[i];j++)\n out=out+numto" ]
map<string,int> tonum={{"zero",0},{"one",1},{"two",2},{"three",3},{"four",4},{"five",5},{"six",6},{"seven",7},{"eight",8},{"nine",9}}; map<int,string> numto={{0,"zero"},{1,"one"},{2,"two"},{3,"three"},{4,"four"},{5,"five"},{6,"six"},{7,"seven"},{8,"eight"},{9,"nine"}}; int count[10]; for (int i=0;i<10;i...
#undef NDEBUG #include<assert.h> int main(){ assert (sort_numbers("") == ""); assert (sort_numbers("three") == "three"); assert (sort_numbers("three five nine") == "three five nine"); assert (sort_numbers("five zero four seven nine eight") == "zero four five seven eight nine"); assert (sort_numbe...
CPP/20_spans_2
2
/* 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...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> vector<float> find_closest_elements(vector<float> numbers){
[ " vector<float> out={};\n for (int i=0;i<numbers.size();i++)\n fo", "if (out.size()==0 or abs(numbers[i]-numbers[j])<abs(out[0]-out[1]))\n out={numbers[i],numbers[j]};\n if (out[0]>out[1", "ut[1],out[0]};\n return out;\n}\n" ]
[ "r (int j=i+1;j<numbers.size();j++)\n ", "])\n out={o" ]
vector<float> out={}; for (int i=0;i<numbers.size();i++) for (int j=i+1;j<numbers.size();j++) if (out.size()==0 or abs(numbers[i]-numbers[j])<abs(out[0]-out[1])) out={numbers[i],numbers[j]}; if (out[0]>out[1]) out={out[1],out[0]}; return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<float> a,vector<float>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (abs(a[i]-b[i])>1e-4) return false; } return true; } int main(){ assert (issame(find_closest_elements({1.0, 2.0, 3.9, 4.0, 5.0, 2.2}) , {3...
CPP/21_spans_2
2
/* Given vector of numbers (of at least two elements), apply a linear transform to that vector, such that the smallest number will become 0 and the largest will become 1 >>> rescale_to_unit({1.0, 2.0, 3.0, 4.0, 5.0}) {0.0, 0.25, 0.5, 0.75, 1.0} */ #include<stdio.h> #include<math.h> #include<vector> using namespace std;...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> vector<float> rescale_to_unit(vector<float> numbers){
[ " float m", "100000;\n for (int i=0;i<numbers.size();i++)\n {\n if (numbers[i]<min) min=numbers[i];\n if (numbers[i]>max) max=numbers[i];\n }\n for (int i=0;i<numbers.size();i++)\n ", "n);\n return numbers;\n}\n" ]
[ "in=100000,max=-", " numbers[i]=(numbers[i]-min)/(max-mi" ]
float min=100000,max=-100000; for (int i=0;i<numbers.size();i++) { if (numbers[i]<min) min=numbers[i]; if (numbers[i]>max) max=numbers[i]; } for (int i=0;i<numbers.size();i++) numbers[i]=(numbers[i]-min)/(max-min); return numbers; }
#undef NDEBUG #include<assert.h> bool issame(vector<float> a,vector<float>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (abs(a[i]-b[i])>1e-4) return false; } return true; } int main(){ assert (issame(rescale_to_unit({2.0, 49.9}) , {0.0, 1.0})); assert (...
CPP/22_spans_2
2
/* Filter given vector of any python values only for integers >>> filter_integers({"a", 3.14, 5}) {5} >>> filter_integers({1, 2, 3, "abc", {}, {}}) {1, 2, 3} */ #include<stdio.h> #include<vector> #include<string> #include<boost/any.hpp> #include<list> typedef std::list<boost::any> list_any; using namespace std; vector<...
#include<stdio.h> #include<math.h> #include<vector> #include<string> #include<boost/any.hpp> #include<list> typedef std::list<boost::any> list_any; using namespace std; #include<algorithm> #include<stdlib.h> vector<int> filter_integers(list_any values){
[ " list_any::iterator it;\n boo", " for (it=values.begin();it!=values.end();it++)\n {\n ", "::any_cast<int>(*it));\n }\n return out;\n}\n" ]
[ "st::any anyone;\n vector<int> out;\n", " anyone=*it;\n if( anyone.type() == typeid(int) )\n out.push_back(boost" ]
list_any::iterator it; boost::any anyone; vector<int> out; for (it=values.begin();it!=values.end();it++) { anyone=*it; if( anyone.type() == typeid(int) ) out.push_back(boost::any_cast<int>(*it)); } return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(filter_integers({}),{})); assert (issame(filter_integers({4, {},2...
CPP/23_spans_2
2
/* Return length of given string >>> strlen("") 0 >>> strlen("abc") 3 */ #include<stdio.h> #include<string> using namespace std; int strlen(string str){
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> int strlen(string str){
[ " ", "ng", "}\n" ]
[ " return str.le", "th();\n" ]
return str.length(); }
#undef NDEBUG #include<assert.h> int main(){ assert (strlen("") == 0); assert (strlen("x") == 1); assert (strlen("asdasnakj") == 9); }
CPP/24_spans_2
2
/* For a given number n, find the largest number that divides n evenly, smaller than n >>> largest_divisor(15) 5 */ #include<stdio.h> using namespace std; int largest_divisor(int n){
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> int largest_divisor(int n){
[ " f", "", "\n}\n" ]
[ "or (int i", "i=2;i*i<=n;i++)\n if (n%i==0) return n/i;\n return 1;\n" ]
for (int i=2;i*i<=n;i++) if (n%i==0) return n/i; return 1; }
#undef NDEBUG #include<assert.h> int main(){ assert (largest_divisor(3) == 1); assert (largest_divisor(7) == 1); assert (largest_divisor(10) == 5); assert (largest_divisor(100) == 50); assert (largest_divisor(49) == 7); }
CPP/25_spans_2
2
/* 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} >>...
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> vector<int> factorize(int n){
[ " vector<int> out={};\n for (int i=2;i*i<=n;i++)\n if (n%i==0)\n {\n ", " i-=1;", "back(n);\n return out;\n}\n" ]
[ " n=n/i;\n out.push_back(i);\n ", "\n }\n out.push_" ]
vector<int> out={}; for (int i=2;i*i<=n;i++) if (n%i==0) { n=n/i; out.push_back(i); i-=1; } out.push_back(n); return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(factorize(2) , {2})); assert (issame(factorize(4) , {2, 2})); a...
CPP/26_spans_2
2
/* 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){
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> remove_duplicates(vector<int> numbers){
[ " vector<int> out={};\n vector<int> has1={};\n vector<int> has2={};\n for (int i=0;i<numbers.size();i++)\n {\n if (find(has2.begin(),h", "!=has2.end()) continue;\n if (find(has1.begin(),has1.end(),numbers[", " {\n\n has2.push_back(numbers[i]);\n }\n els...
[ "as2.end(),numbers[i])", "i])!=has1.end())\n " ]
vector<int> out={}; vector<int> has1={}; vector<int> has2={}; for (int i=0;i<numbers.size();i++) { if (find(has2.begin(),has2.end(),numbers[i])!=has2.end()) continue; if (find(has1.begin(),has1.end(),numbers[i])!=has1.end()) { has2.push_back(numbers[i]); ...
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(remove_duplicates({}) , {})); assert (issame(remove_duplicates({1,...
CPP/27_spans_2
2
/* For a given string, flip lowercase characters to uppercase and uppercase to lowercase. >>> flip_case("Hello") "hELLO" */ #include<stdio.h> #include<string> using namespace std; string filp_case(string str){
#include<stdio.h> #include<math.h> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> string filp_case(string str){
[ " string out=\"\";\n for (int i=0;i<str.length();i++)\n {\n char w=s", " if (w>=97 and w<=122) {w-=32;}\n e", "nd w<=90){ w+=32;}\n out=out+w;\n }\n return out;\n}\n" ]
[ "tr[i];\n ", "lse\n if (w>=65 a" ]
string out=""; for (int i=0;i<str.length();i++) { char w=str[i]; if (w>=97 and w<=122) {w-=32;} else if (w>=65 and w<=90){ w+=32;} out=out+w; } return out; }
#undef NDEBUG #include<assert.h> int main(){ assert (filp_case("") == ""); assert (filp_case("Hello!") == "hELLO!"); assert (filp_case("These violent delights have violent ends") == "tHESE VIOLENT DELIGHTS HAVE VIOLENT ENDS"); }
CPP/28_spans_2
2
/* Concatenate vector of strings into a single string >>> concatenate({}) "" >>> concatenate({"a", "b", "c"}) "abc" */ #include<stdio.h> #include<vector> #include<string> using namespace std; string concatenate(vector<string> strings){
#include<stdio.h> #include<math.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> string concatenate(vector<string> strings){
[ " string out=\"\";\n fo", "t=o", " return out;\n}\n" ]
[ "r (int i=0;i<strings.size();i++)\n ou", "ut+strings[i];\n " ]
string out=""; for (int i=0;i<strings.size();i++) out=out+strings[i]; return out; }
#undef NDEBUG #include<assert.h> int main(){ assert (concatenate({}) == ""); assert (concatenate({"x", "y", "z"}) == "xyz"); assert (concatenate({"x", "y", "z", "w", "k"}) == "xyzwk"); }
CPP/29_spans_2
2
/* Filter an input vector of strings only for ones that start with a given prefix. >>> filter_by_prefix({}, "a") {} >>> filter_by_prefix({"abc", "bcd", "cde", "vector"}, "a") {"abc", "vector"} */ #include<stdio.h> #include<vector> #include<string> using namespace std; vector<string> filter_by_prefix(vector<string> stri...
#include<stdio.h> #include<math.h> #include<vector> #include<string> using namespace std; #include<algorithm> #include<stdlib.h> vector<string> filter_by_prefix(vector<string> strings, string prefix){
[ " vector<stri", "+)\n if (strings[i].substr(0,prefix.le", "return out;\n}\n" ]
[ "ng> out={};\n for (int i=0;i<strings.size();i+", "ngth())==prefix) out.push_back(strings[i]);\n " ]
vector<string> out={}; for (int i=0;i<strings.size();i++) if (strings[i].substr(0,prefix.length())==prefix) out.push_back(strings[i]); return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<string> a,vector<string>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(filter_by_prefix({}, "john") , {})); assert (issame(filter_by_pre...
CPP/30_spans_2
2
/* Return only positive numbers in the vector. >>> get_positive({-1, 2, -4, 5, 6}) {2, 5, 6} >>> get_positive({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10}) {5, 3, 2, 3, 9, 123, 1} */ #include<stdio.h> #include<math.h> #include<vector> using namespace std; vector<float> get_positive(vector<float> l){
#include<stdio.h> #include<math.h> #include<vector> using namespace std; #include<algorithm> #include<stdlib.h> vector<float> get_positive(vector<float> l){
[ " ", "", "n out;\n}\n" ]
[ "vector<float", "t> out={};\n for (int i=0;i<l.size();i++)\n if (l[i]>0) out.push_back(l[i]);\n retur" ]
vector<float> out={}; for (int i=0;i<l.size();i++) if (l[i]>0) out.push_back(l[i]); return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<float> a,vector<float>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (abs(a[i]-b[i])>1e-4) return false; } return true; } int main(){ assert (issame(get_positive({-1, -2, 4, 5, 6}) , {4, 5, 6} )); assert...
CPP/31_spans_2
2
/* Return true if a given number is prime, and false otherwise. >>> is_prime(6) false >>> is_prime(101) true >>> is_prime(11) true >>> is_prime(13441) true >>> is_prime(61) true >>> is_prime(4) false >>> is_prime(1) false */ #include<stdio.h> using namespace std; bool is_prime(long long n){
#include<stdio.h> #include<math.h> using namespace std; #include<algorithm> #include<stdlib.h> bool is_prime(long long n){
[ " ", "+)\n", "urn false;\n return true;\n}\n" ]
[ " if (n<2) return false;\n for (long long i=2;i*i<=n;i+", " if (n%i==0) ret" ]
if (n<2) return false; for (long long i=2;i*i<=n;i++) if (n%i==0) return false; return true; }
#undef NDEBUG #include<assert.h> int main(){ assert (is_prime(6) == false); assert (is_prime(101) == true); assert (is_prime(11) == true); assert (is_prime(13441) == true); assert (is_prime(61) == true); assert (is_prime(4) == false); assert (is_prime(1) == false); assert (is_prime(5) ==...
CPP/32_spans_2
2
#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; #include<algorithm> #include<stdlib.h> double poly(vector<double> xs, double x){ double sum=0; int i; for (i=0;i<xs.size();i++) { sum+=xs[i]*pow(x,i); } return sum; } double find_zero(vector<double> xs){
[ " double ans=0;\n double value;\n value=poly(", " while (abs(value)>1e-6)\n {\n double driv=0;\n", " driv+=xs[i]*pow(ans,i-1)*i;\n }\n ans=ans-value/driv;\n value=poly(xs,ans);\n }\n return ans;\n\n}\n" ]
[ "xs,ans);\n ", " for (int i=1;i<xs.size();i++)\n {\n " ]
double ans=0; double value; value=poly(xs,ans); while (abs(value)>1e-6) { double driv=0; for (int i=1;i<xs.size();i++) { driv+=xs[i]*pow(ans,i-1)*i; } ans=ans-value/driv; value=poly(xs,ans); } return ans; }
#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; ...
CPP/33_spans_2
2
/* This function takes a vector l and returns a vector l' such that l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal to the values of the corresponding indicies of l, but sorted. >>> sort_third({1, 2, 3}) {1, 2, 3} >>> sort_thir...
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> sort_third(vector<int> l){
[ " vector", "i;\n for (i=0;i*3<l.size();i++)", "third.push_back(l[i*3]);\n \n sort(third.begin(),third.end());\n\n vector<int> out={};\n for (i=0;i<l.size();i++)\n {\n if (i%3==0) {out.push_back(third[i/3]);}\n else out.push_back(l[i]);\n }\n return out;\n\n}\n" ]
[ "<int> third={};\n int ", "\n " ]
vector<int> third={}; int i; for (i=0;i*3<l.size();i++) third.push_back(l[i*3]); sort(third.begin(),third.end()); vector<int> out={}; for (i=0;i<l.size();i++) { if (i%3==0) {out.push_back(third[i/3]);} else out.push_back(l[i]); } return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(sort_third({1, 2, 3}) , sort_third({1, 2, 3}))); assert (issame(sor...
CPP/34_spans_2
2
/* Return sorted unique elements in a vector >>> unique({5, 3, 5, 2, 3, 3, 9, 0, 123}) {0, 2, 3, 5, 9, 123} */ #include<stdio.h> #include<vector> #include<algorithm> using namespace std; vector<int> unique(vector<int> l){
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> vector<int> unique(vector<int> l){
[ " vector<int> out={};\n for (int i=0;i<l.size();i++)\n if (find(out.begin", "t.begin(),out.end());", "\n" ]
[ "(),out.end(),l[i])==out.end())\n out.push_back(l[i]);\n sort(ou", "\n return out;\n}" ]
vector<int> out={}; for (int i=0;i<l.size();i++) if (find(out.begin(),out.end(),l[i])==out.end()) out.push_back(l[i]); sort(out.begin(),out.end()); return out; }
#undef NDEBUG #include<assert.h> bool issame(vector<int> a,vector<int>b){ if (a.size()!=b.size()) return false; for (int i=0;i<a.size();i++) { if (a[i]!=b[i]) return false; } return true; } int main(){ assert (issame(unique({5, 3, 5, 2, 3, 3, 9, 0, 123}) , {0, 2, 3, 5, 9, 123})); }
CPP/35_spans_2
2
/* Return maximum element in the vector. >>> max_element({1, 2, 3}) 3 >>> max_element({5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10}) 123 */ #include<stdio.h> #include<math.h> #include<vector> using namespace std; float max_element(vector<float> l){
#include<stdio.h> #include<math.h> #include<vector> #include<algorithm> using namespace std; #include<stdlib.h> float max_element(vector<float> l){
[ " float", "(int ", " max;\n\n}\n" ]
[ " max=-10000;\n for ", "i=0;i<l.size();i++)\n if (max<l[i]) max=l[i];\n return" ]
float max=-10000; for (int i=0;i<l.size();i++) if (max<l[i]) max=l[i]; return max; }
#undef NDEBUG #include<assert.h> int main(){ assert (abs(max_element({1, 2, 3})- 3)<1e-4); assert (abs(max_element({5, 3, -5, 2, -3, 3, 9, 0, 124, 1, -10})- 124)<1e-4); }