Asked by:
Coding Challenge Answers

General discussion
-
Hey people, The coding challege has finished, and now i think that we can discuss the coding problems that we faced...
IMP - The thread is not intended to cheat, as the coding competition has completed for this month... The reason for this thread is to make the coding habbits of people more efficient so that we can clear out our loop holes...Monday, September 24, 2007 4:51 AM
All replies
-
Starting with me, let me 1st put down the questions so that we can than discuss its solution....
NumberFormatingProblem is the problem which format the given number. For displaying the total price in most of the purchase order, the display format should be in comma separated format. The objective of this problem is to format the given number. For example the number 1000000 should be separated by commas 10,00,000.
The function getFormattedNumber contains the following component defined as input 'price'.
Write a program to calculate the number formatting for a given input and return the output as String.Example 1:
Input:
double price = 1012322
Output: Returns "10,12,322"
Example 2:
Input:
double price = 434552
Output: Returns "4,34,552"For C solutions
Header File
:
NumberFormatting.h
Function Name
:
char* getFormattedNumber(double price)
Directory Name
:
numberformatting
File Name
:
NumberFormatting.c
For C++ solutions
Header File
:
NumberFormatting.h
Class Name
:
numberformatting
Function Name
:
char* getFormattedNumber(double price)
Directory Name
:
numberformatting
FileName
:
NumberFormatting.cpp
Monday, September 24, 2007 4:53 AM -
There are two sections for grade 4 students and when moving to grade 5, the school management decided to merge them all into single section. You are required to write a program to merge students from both the sections in such a way that they are sorted in ascending order. Assume that no student names are repeated.
This program uses a unique method getSortedNames. The method getSortedNames is unique because the data type it uses as input parameter is "vector names1, vector names2". The output of this method is again a vector which contains names in both names 1 set as well as names2 set arranged in ascending order.
IMP: Remember to include header file #include <vector> for vector.
Example 1:
Input:
names1[] = {"Aakash","Rohit","Babu"};
names2[] = {"Uma","Vikram","Surya"}
Output: Returns {"Aakash", "Babu", "Rohit", "Surya", "Uma", "Vikram"}
Explanation: The method getSortedNames merges both the vectors in to a single vector as well arranges them in ascending order.
Example 2:
Input:
names1[] = {"Clark","Harris","Smith","Davis","King","Adams","Campbell","Mitchell","Jackson"};
names2[] = {"Barbara","Donald","Betty","Taylor","Anderson"}
Output: Returns {"Adams", "Anderson", "Barbara", "Betty", "Campbell", "Clark", "Davis", "Donald", "Harris", "Jackson" , "King", "Mitchell", "Smith", "Taylor"}
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->For C solutions
Header File
:
NameList.h
Function Name
:
vector<string> getSortedNames(vector<string> names1, vector<string> names2)
Directory Name
:
namelist
File Name
:
NameList.c
For C++ solutions
Header File
:
NameList.h
Class Name
:
list
Function Name
:
vector<string> getSortedNames()
Directory Name
:
namelist
FileName
:
NameList.cpp
Monday, September 24, 2007 4:55 AM -
Please people follow the standing instructions...
1) Clearly state the subject lines... When you put ur question, put the question name in subject line, so that its easy to trace...
2) When you reply to any post, make it sure that you press the reply to that post, that will automatically put the subject as Re : Subject
This 2 instructions will enable us to handle multiple questions in single thread without confusion...Monday, September 24, 2007 5:00 AM -
I got the problem of Some Pho Costing based on number of copies. It was easier just with if and else if conditions !Monday, September 24, 2007 5:18 AM
-
Sol 1:
well the problem given to me was very very as compared to this.
But while trying this problem this is so far i got..
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
char *ret(double n1)
{
char *str=new char;
char *man=str;
double temp=n1;
int i=0;
while(temp>0)
{
int rem=(int)fmod(temp,10.0);
*man=(char)(rem+48);
man++;
temp=(double)floor(temp/10);
i++;if (i==3)
*(man++)=',';
else
if ((i>3)&&((i%2!=0)))
*(man++)=',';
}
*man='\0';
return str;
}
int main(int argc, char *argv[])
{
char *s=ret(100000);
puts(s);
system("PAUSE");
return EXIT_SUCCESS;
}Ok I m getting reverse answer can any body help me reverse this pointer string so that i get right answer.
Monday, September 24, 2007 5:29 AM -
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector <string> getSortedNames(vector <string> names1,vector <string> names2)
{
vector <string> res;
sort(names1.begin(),names2.end());
sort(names1.begin(),names2.end());
merge(names1.begin(),names1.end(),names2.begin(),names2.end(),res.begin());
return res;
}This Should be the solution for second.
Monday, September 24, 2007 5:39 AM -
This is the answer from my side.. Ready for the comments and suggestions....
NummberFormating.c#include<stdio.h>
#include<string.h>
char* getFormattedNumber(double price1)
{
int sign = 1;
long int price;
char n[25];
int tmp;
short position = 0;
if(price1 < 0) sign = -1;
price = price1 * sign;
while(price > 0)
{
tmp = price % 10;
price = price / 10;
n[position++] = char(tmp + 48);
if(position % 3 == 0)
n[position++] = ',';
}
if((position-1) % 3==0) position--;
if(sign == -1) n[position++] = '-';
n[position] = '\0';
strrev(n);
char *ans;
ans = new char[strlen(n) + 1];
strcpy(ans, n);
return ans;
}
void main()
{
double number = -123456789;
char *str;
str = getFormattedNumber(number);
printf("String %s = ", str);
}
Monday, September 24, 2007 6:02 AM -
I really need suggestions on this, because this area was not explored by me before..
NameList.cpp#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
class list
{
public:
vector<string> names1;
vector<string> names2;
vector<string> getSortedNames();
};
vector<string> list::getSortedNames()
{
vector<string> names3;
names3 = names1;
names3.insert(names3.end(),names2.begin(), names2.end());
sort(names3.begin(), names3.end());
return names3;
}
void main()
{
list l1;
int i;
string names1[9] = {"Clark","Harris","Smith","Davis","King","Adams","Campbell","Mitchell","Jackson"};
string names2[5] = {"Barbara","Donald","Betty","Taylor","Anderson"};
for(i = 0; i<9; i++)
{
l1.names1.push_back(names1[i]);
}
for(i=0; i<5; i++)
{
l1.names2.push_back(names2[i]);
}
vector<string> names3;
names3 = l1.getSortedNames();
for(i=0 ; i<names3.size(); i++)
cout<<i+1 <<" "<<names3[i]<<endl;
}Monday, September 24, 2007 6:06 AM -
imran_khan_cc9020 wrote: Sol 1:
well the problem given to me was very very as compared to this.
But while trying this problem this is so far i got..
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
char *ret(double n1)
{
char *str=new char;
char *man=str;
double temp=n1;
int i=0;
while(temp>0)
{
int rem=(int)fmod(temp,10.0);
*man=(char)(rem+48);
man++;
temp=(double)floor(temp/10);
i++;if (i==3)
*(man++)=',';
else
if ((i>3)&&((i%2!=0)))
*(man++)=',';
}
*man='\0';
return str;
}
int main(int argc, char *argv[])
{
char *s=ret(100000);
puts(s);
system("PAUSE");
return EXIT_SUCCESS;
}Ok I m getting reverse answer can any body help me reverse this pointer string so that i get right answer.
Use the strrev function, but it was not working on the site compiler, so i had manually reversed the string.. refer to my answer to get that....
Now some suggestions...
Allocate enough memory,
after declaring the pointer, you allocated memory for only 1 char, do it for atleast 20, like p = new char[20];
Now this double was giving a problem to me too, so i just took it into a long int, its of same length and will round of the decimal part.. so you have a integer now which makes your life easier...
I have also made adjustments for the negative number, you can see that too....Monday, September 24, 2007 6:19 AM -
imran_khan_cc9020 wrote: #include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector <string> getSortedNames(vector <string> names1,vector <string> names2)
{
vector <string> res;
sort(names1.begin(),names2.end());
sort(names1.begin(),names2.end());
merge(names1.begin(),names1.end(),names2.begin(),names2.end(),res.begin());
return res;
}This Should be the solution for second.
1st of all, i tried to run this code. it was giving memory problems and didnt worked....
2nd, if you have declared it like this in the final answer, in c++, then its wrong,
you had to declare the function for the class member...
so it had to be
vector<string> list::getSortedNames()
Please take a note of this when you use c++ for coding problems in future...
And please explain why you did the sorting 2 times.... and then merging, I am not so good at using the vector class, so m not able to understand what you did.. can you explain it??Monday, September 24, 2007 6:23 AM -
Ok my fault
So here`s the improved sol. to second problem
#include <algorithm>
#include <vector>
using namespace std;vector <string> getSortedNames(vector <string> names1,vector <string> names2)
{
vector <string> res(names1.size()+names2.size()); //Size must be declared otherwise memory problem is there ( I// don`t Know why???
sort(names1.begin(),names1.end()); // The Two Functions must be sorted one..
sort(names2.begin(),names2.end());
merge(names1.begin(),names1.end(),names2.begin(),names2.end(),res.begin());
return res;
}
int main(int argc, char *argv[])
{
vector <string> s1,s2,s3;
string names1[9] = {"Clark","Harris","Smith","Davis","King","Adams","Campbell","Mitchell","Jackson"};
string names2[5] = {"Barbara","Donald","Betty","Taylor","Anderson"};
for(int i = 0; i<9; i++)
{
s1.push_back(names1);
}
for(int i=0; i<5; i++)
{
s2.push_back(names2);
}
s3=getSortedNames(s1,s2);
for(int i=0;i<s3.size();i++)
cout<<s3<<endl;
system("pause");
}Monday, September 24, 2007 8:43 AM -
Ok learning from ur code for problem 1 i made few changes and the new code is..
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
char *ret(double n1)
{
int sn=(n1<0.0)?-1:1;
n1=n1*sn;
char *str=new char[12];
char *man=str;
double temp=n1;
int i=0;
while(temp>0)
{
int rem=(int)fmod(temp,10.0);
*man=(char)(rem+48);
man++;
temp=(double)floor(temp/10);
i++;if (i==3)
*(man++)=',';
else
if ((i>3)&&((i%2!=0)))
*(man++)=',';
}
if (sn==-1)
*man++='-';
*man='\0';
strrev(str);
return str;
}
int main(int argc, char *argv[])
{
char *s=ret(-100000);
puts(s);
system("PAUSE");
return EXIT_SUCCESS;
}Monday, September 24, 2007 8:54 AM -
didn't you get an error with "int main()" ??? They insist you write "void dsmain()"Monday, September 24, 2007 9:47 AM
-
Yeah i would hav got error if i had submitted this problem in quiz.
but here i have showed the logic not the atual way to submit the code...
Monday, September 24, 2007 10:18 AM -
gr8...i got error while submitting...........Monday, September 24, 2007 11:22 AM -
Good work Imran, this is the reason i started this thread.. so that we can learn and improve...Monday, September 24, 2007 4:24 PM