0% found this document useful (0 votes)
152 views

Coding Test

The document contains code snippets from different C++ programs. The snippets cover topics like: 1. Finding a pattern in an image by comparing strings representing the image and pattern pixel-by-pixel. 2. Traversing a graph by recursively searching for the end node starting from a given start node. 3. Calculating the join point of two numbers by iteratively summing their digits and adding to the original number. So in summary, the document contains disjoint code snippets from different C++ programs that implement image pattern matching, graph traversal, and number joining algorithms.

Uploaded by

Prashant Beri
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

Coding Test

The document contains code snippets from different C++ programs. The snippets cover topics like: 1. Finding a pattern in an image by comparing strings representing the image and pattern pixel-by-pixel. 2. Traversing a graph by recursively searching for the end node starting from a given start node. 3. Calculating the join point of two numbers by iteratively summing their digits and adding to the original number. So in summary, the document contains disjoint code snippets from different C++ programs that implement image pattern matching, graph traversal, and number joining algorithms.

Uploaded by

Prashant Beri
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

//Finding Image

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <unistd.h>
#include<cmath>

using namespace std;

vector<int> solve(int imageWidth, int imageHeight, vector<string> image, int


patternWidth, int patternHeight, vector<string> pattern)
{
// Write your code here
// To debug: cerr << "Debug messages..." << endl;
// cout<<"Coorsdinates "<<imageWidth<<" "<<imageHeight<<" "<<patternWidth<<"
"<<patternHeight<<endl;
string str;
int it=0;
// cout<<"Image detail"<<endl;
for(it=0;it<imageHeight;++it)
{
str=str+image[it];
// cout<<image[it]<<" ";
}
cout<<str<<endl;
// cout<<"Pattern detail"<<endl;
/* for(it=0;it<patternHeight;++it)
{
cout<<pattern[it]<<" ";
}
*/
string fndstr=pattern[0];
int pos = 0;

while(pos<str.size())
{
pos = str.find(fndstr,pos==0?0:pos+1);
cout<<endl<<"Outer Loction "<<pos<<endl;
int y=pos/imageWidth;
int x=pos%imageWidth;
cout<<"X="<<x<<"Y="<<y;
int sx,sy,spos=0;
for(it=1;it<patternHeight;++it)
{
spos=str.find(pattern[it],spos==0?pos+1:spos+1);
cout<<endl<<it<<".Loction "<<spos<<endl;
sy=spos/imageWidth;
sx=spos%imageWidth;
cout<<it<<".X="<<sx<<"Y="<<sy;
if(sx!=x || sy!=y+it) break;
}
if(it==patternHeight)
{
cout<<endl<<it<<"-"<<patternHeight<<" X="<<x<<"Y="<<y;
return vector<int>(x,y);
}
}
return vector<int>(0,0);
}

//Loop in graph
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unistd.h>

using namespace std;

int find_network_endpoint(int startNodeId, vector<int> fromIds, vector<int> toIds)


{
// Write your code here
// To debug: cerr << "Debug messages..." << endl;
for(auto it=fromIds.begin();it<fromIds.end();++it)
cout<<*it<<" ";
cout<<endl;
for(auto it=toIds.begin();it<toIds.end();++it)
cout<<*it<<" ";

cout<<endl<<startNodeId<<endl;
int i=1;
vector<int> v1;
int start=0;
int s=0;
int elem=startNodeId;
int elem1=startNodeId;
while(1)
{
auto temp=find(fromIds.begin(),fromIds.end(),elem1);
start=fromIds.end()-temp;

cout<<endl<<"st - "<<elem1<<"="<<*temp;

if (temp==fromIds.end()) return elem1;


v1.push_back(elem1);

s=fromIds.size()-start;
cout<<" - "<<s;

elem=fromIds[s];
// cout<<" "<<elem;
elem1=toIds[s];
if (find(v1.begin(),v1.end(),elem1)!=v1.end()) return elem;
cout<<" "<<elem1;
}
return -1;
}

// sum of number + its digit sum


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unistd.h>
#include<cmath>
using namespace std;

long join(long s1,long s2 )


{
int sum=0;
int sum1=0;
long ss1,ss2;
vector<long> v1;
vector<long> v2;
int i=0;
cout<<s1<<" "<<s2<<endl;
while(1)
{
v1.push_back(s1);
v2.push_back(s2);

if(find(v1.begin(),v1.end(),s2)!=v1.end() )
return s2;

else if(find(v2.begin(),v2.end(),s1)!=v2.end())
return s1;

ss1=s1;
ss2=s2;
cout<<ss1<<" "<<ss2<<" "<<++i<<endl;
for (sum = 0; s1 > 0; sum += s1 % 10, s1 /= 10 );
for (sum1 = 0; s2 > 0; sum1 += s2 % 10, s2 /= 10);
s1=ss1+sum ;
s2=ss2+sum1;
// cout<<sum<<" "<<sum1<<endl;
// cout<<"s1="<<s1<<"s2="<<s2<<endl;
if(s1<0 || s1>20000000) return -1;
if(s2<0 || s2>20000000) return -1;
}
return -1;
}

int compute_join_point(int s1, int s2)


{
// Write your code here
// To debug: cerr << "Debug messages..." << endl;
long val;
val=join(s1,s2);
return val;

//Package rejected standard


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unistd.h>

using namespace std;

string solve(int width, int height, int length, int mass)


{
// Write your code here
// To debug: cerr << "Debug messages..." << endl;
if(width*height*length<1000000 &&
width<150 &&
length<150 &&
height<150 &&
mass<20
)
return "STANDARD";
else {
if ((width*height*length>=1000000 ||
width>=150 ||
length>=150 ||
height>=150) &&
mass>=20
)
return "REJECTED";
else
return "SPECIAL";
}
}

/* Ignore and do not change the code below */


int main()
{

// game loop
while (1) {
int width;
int height;
int length;
int mass;
cin >> width >> height >> length >> mass; cin.ignore();
int std_out_fd = dup(1);
dup2(2, 1);
string action = solve(width, height, length, mass);
dup2(std_out_fd, 1);
cout << action << endl;
}
}
/* Ignore and do not change the code above */
////Discount Sale
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unistd.h>
#include<numeric>
using namespace std;

int calculate_total_price(vector<int> prices, int discount)


{
// Write your code here
// To debug: cerr << "Debug messages..." << endl;
int mostexp;
int total;
if(discount>=0 && discount<=100 &&
*max_element(prices.begin(),prices.end())<100000 &&
*min_element(prices.begin(),prices.end())>0 &&
prices.size()>0&&
prices.size()<100)
{
mostexp=*max_element(prices.begin(),prices.end());
cout<<discount<<" "<<mostexp<<endl;
total=accumulate(prices.begin(),prices.end(),0)-mostexp+mostexp*(1-
(float)discount/100);

return total;
}

return -1;
}

//change question
#include <cmath>
#include<iostream>
using namespace std;

// Do not modify the Change‌


‌structure
typedef struct {
long coin2;
long bill5;
long bill10;
} Change;

class Answer
{
public:
static bool optimalChange(long s, Change &c)
{
c.coin2 = 0;
c.bill5 = 0;
c.bill10 = 0;
if(s<2) return false;
if(s%2!=0)
{
c.bill5=1;
s=s-5;
}
int ud=s%10;

long s1=(long)s/10;
if(ud>0)
c.coin2=ud/2;
c.bill10=s1;
return true;
}
};

///close to zero
#include <cmath>
#include<algorithm>
#include<vector>
using namespace std;

class Answer
{
public:
/// This method finds the number closest to‌
‌zero
static int closestToZero(int ints[], int size)
{
sort(ints,ints+size);
vector<int> v1(ints,ints+size);
vector<int>::iterator it;
if(ints[0]>0) return ints[0];
else if (ints[size-1]<0) return ints[size-1];
else{
it=partition_point(v1.begin(),v1.end(),[](int x){
return x<0;
}
);
return *it;
}
return 0;
}
};

//find greatest

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

class Algorithm
{
public:
/** @return the largest number in the given array‌‌*/
static int findLargest(int numbers[], int length)
{
vector<int> vect(numbers,numbers+length);
return *max_element(vect.begin(),vect.end());
}
};

//two string mismatch


#include <iostream>
#include <string>
#include <vector>
#include<algorithm>
using namespace std;
int main()
{
std::string s = "HelloWorld!Hello";
std::string s1 = "Hello";
// declaring pointer pair
pair< vector<char>::iterator,
vector<char>::iterator > mispair;
vector<char> v(s.begin(), s.end());
vector<char> v1(s1.begin(), s1.end());
vector<char>::iterator ip;
// cout<<*v1<<" "<<*v;
// Using std::find_end
mispair = mismatch(v1.begin(), v1.end(), v.begin());
// ip = std::find_end(v.begin(), v.end(), v1.begin(),v1.end());

// Displaying the index where the last common occurrence


// begins
cout << *mispair.second;

return 0;
}

//Last Occurence

#include <iostream>
#include <string>
#include <vector>
#include<algorithm>
using namespace std;
int main()
{
std::string s = "Hello World!Hello";
std::string s1 = "Hello";

vector<char> v(s.begin(), s.end());


vector<char> v1(s1.begin(), s1.end());
vector<char>::iterator ip;
// cout<<*v1<<" "<<*v;
// Using std::find_end
ip = std::find_end(v.begin(), v.end(), v1.begin(),v1.end());

// Displaying the index where the last common occurrence


// begins
cout << (ip - v.begin()) << "\n";

return 0;
}
////Value of PI
#include <vector>
#include<algorithm>
#include<iostream>

using namespace std;

// do not modify this‌


‌structure
typedef struct {
double x;
double y;
} Point;
bool isinside(Point p)
{

if (p.x * p.x + p.y * p.y<=1)


return true;
else
return false;
}
class Pi {
public:
// approximate pi using the given points
static double approx(vector<Point> &pts) {
int noEven = count_if(pts.begin(), pts.end(),
isinside);

return double(4 * noEven) / 100000;


}
};

You might also like