Saturday, 20 May 2017

DUMMY PROGRAMMER QUESTIONS

Questions made by DUMMY PROGRAMMER


Question-Convert a given Decimal number to binary
ANS-
#include<iostream>
#include<string.h>
using namespace std;
void take(long long int  n)
{
    int c=0,i=0,rem=0;
    int m[50];
    do{
        rem=n%2;
        n=n/2;
       m[i]=rem;
        i=i+1;
    }while(n!=0);

    for(int j=i-1;j>=0;j--)
    {
        cout<<m[j];
    }
}
int main()
{
    long long int n;
    string s;
    cin>>n;
    take(n);
    return(0);

}


_________________________________________________________________



Ques-A positive interger N is passes as the input. The program must print the number of 1s in the binary representation of N.
Input format-
input the number N.
Output format-
First line contains the count of 1s in the binary representation of N.
Boundary condition-
1<=N<=9000000000.

Example-
input-22
output-3
Explaination-the binary representation of 22 is 10110. There are three 1s in it.

____________________________________________________________


Solution-

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    unsigned long long int n;
    int i=0;
    cin>>n;
    while(n!=0)
    {
        if(n%2==1)
            i++;
        n=n/2;
    }
    cout<<i;
}



____________________________________________________________

Ques-Take input a string(with spaces) and for each word make sure that the word starts with uppercase and gives alternate lowercase

Example1-

input-Every BoY HaS hiS OWn LoVESTOry
expected output-EvErY BoY HaS HiS OwN LoVeStOrY

Example2-

input-this sEEMS CoOL
expected output-ThIs SeEmS CoOl

______________________________________________________________

Solution-


#include<iostream>

#include<string>
using namespace std;

void convert( string& s)

{
    for(int i=0;i<s.length();i++)
    {
        s[i]=tolower(s[i]);
    }
}

void convertevenodd( string &s)

{
    for(int i=0;i<s.length();i=i+2)
    {
        if(s[i]==' ')
        {
            i=i-1;
        }
        else
        {
        s[i]=toupper(s[i]);
        }
    }
}
int main()
{
    string s;
    getline(cin,s);
    convert(s);
    convertevenodd(s);
    cout<<s;
    return(0);
}


Monday, 1 May 2017

BONUS

BONUS PRACTICE PROBLEMS


Ques-FIND A ROUTE-

Rahul is fond of travelling and he visits cities and towns in the country whenever possible. All cities in the country are not connected to each other. Given the details of the cities that are connected to each city, source city from where he begins the travel and the destination city of the travel, design an algorithm and write a C++ code to list down the cities in the travel. Rahul must not visit a city more than once. When the destination city name is in the connected cities of the current city, chose it and complete the route. When the destination city name is not in the list of connected cities to current city and there are more than one city from the current city, he sorts the city names and include the first minimum city name that is not yet visited. For example, if the connection between the cities are given as follows, source city as A and destination city as F the list of cities in the travel are A, B, D and F.

City
Connected Cities
A
E, B
B
D
C
E,D
D
F
E
F

Use vectors, maps and algorithms such as sort, find in STL for implementation. Assume that the connection of cities is very simple and there is a path as described in the problem.
Input Format
Number of cities that are connected to other cities, 'n'
Name of city1
Number of neighbouring cities for city1, 'm'
First neighbouring city of city1
Second neighbouring city of city1
...
mth neighbouring city of city1
...
Name of cityn
Number of neighbouring cities for cityn, 'm'
First neighbouring city of cityn
Second neighbouring city of cityn
...
mth neighbouring city of cityn

Output Format
Name of cities in the list

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<map>
#include<algorithm>
class travel
{
    int num_Of_Cities;
    map<string,vector<string> > city_Connection;
    string source;
    string destn;
    vector<string> route;
    public:
    //In this function form the map that depicts the
    // the connection of cities,key is the name of the
    // city and value is a vector of cities that are
    // connected to the city
    void get();
    void find_Route();
    void print_Route();
};


ANSWER-





void travel::get()
{
    int t,i=0,j;
    vector<string> s;
    cin>>num_Of_Cities;
    for(; i<num_Of_Cities; i++)
    {
        cin>>source>>t;
        s.clear();
        for(j=0; j<t; j++)
        {
            cin>>destn;
            s.push_back(destn);
        }
        sort(s.begin(),s.end());
        cout<<endl;
        city_Connection[source]=s;
    }
    cin>>source>>destn;
    route.push_back(source);
}
void travel::find_Route()
    {
    vector<string> s;
    int j, k;
    while(source!=destn){
        j=0;
        s=city_Connection[source];
        for(k=0; k<route.size(); k++)
        if(route[k]==s[j])
        {
            j++;
            k=0;
        }
        for(k=0; k<s.size(); k++)
        if(destn==s[k])
        j=k;
        route.push_back(s[j]);
        source=s[j];
    }
}
void travel::print_Route()
{
for(int i=0; i<route.size(); i++)
cout<<route[i]<<endl;

}


PREWRITTEN-

int main()
{
travel t;
t.get();
t.find_Route();
t.print_Route();

}




QUESTION-

Jobs are submitted to an operating system. Operating system does something called job scheduling when a number of jobs are submitted to an operating system at the same time. Some of the jobs have only job id and time required to complete the job. Whereas some other jobs are having priority in addition to job id and time required. Given a set of jobs without priority print the id of the job that requires minimum time to execute and given a set of jobs with priority, print the id and priority of the job which requires minimum time to execute. Design a OOP model to solve the problem.
Input Format
Number of jobs without priority
Id of job1
Time required by job1
...
Id of job-n
Time required by job-n
Number of jobs with priority
Id of job1
Time required by job1
Priority of job1
...
Id of job-n
Time required by job-n
Priority of job-n
Output Format
Id of job with out priority that requires minimum time
Id of job with priority that requires minimum time  and its priority.

#include<iostream>
#include<iomanip>
#include<string.h>
using namespace std;
class job
{
    int id;
    float time;
    public:
    void get();
    //function to return time
    float gettime();
    int getid();
};
class job_Sch
{
int num;
job j[20];
public:
//function to print job with maximum time
void find_Max();
void get();
};


ANSWER-


bool pri=false;
int p[20];
void job :: get()
{
    cin>>id>>time;
}
float job :: gettime()
{
    return(time);
}
int job :: getid()
{
    return(id);
}
void job_Sch :: get()
{
    cin>>num;
    for(int i=0;i<num;i++)
    {
        j[i].get();
        if(pri)
        cin>>p[i];
    }
}
void job_Sch :: find_Max()
{
    int max=-1,pos;
    for(int i=0;i<num;i++)
    if(max<j[i].gettime())
    {
        pos=i;
        max=j[i].gettime();
    }
    cout<<j[pos].getid()<<"\n";
    if(pri)
    cout<<p[pos]<<"\n";
}
int main()
{
    job_Sch j;
    j.get();
    j.find_Max();
    pri=true;
    j.get();
    j.find_Max();
    return(0);
}




Jegan organizes a family function and invite some of his friends. Many of his friends come prior to the function. Jegan has arranged for their accommodation in a hotel. The rooms are booked as the guest arrive, so the room numbers are not continous and not in any order. Jegan has the room number of the friend who had arrived first. And he has requested his friends to have a room number of the person who arrives next to him. That is the friend who arrived third will have room number of the friend who arrived fourth. The last guest will not have any room number. Given the details of each guest such as name, his room number and room number of the guest who arrived next, and room number of that Jegan has, design an algorithm and write a C++ code to print the names and room numbers to serve a coffee. For example, if the details in the following table is given

Room Number of Guest
Name of Guest
Room number the guest who arrived next
125
John
210
157
Kannan
125
210
Kumar
-1
315
Anand
157

If Jegan has room number as 315, room number of the friend who arrived first, then the output should be
Anand 315
Kannan 157
John 125
Kumar 210
Hint: Map in STL can be used for representing the input, room number of the guest may be the key and other two details may be stored as value by representing them as a user defined data type.
Input Format
Number of friends
Room number of guest1
Name of guest1
Room number the guest who arrived next to guest1
Room number of guest2
Name of guest2
Room number the guest who arrived next to guest2
....
Room number of guestn
Name of guestn
-1 ( Last guest doesn't posses any room number)
Room number that Jegan has
Output Format
Name of guest1 and his Room number separated by tab
Name of guest2 and his Room number separated by tab
...
Name of guestn and his Room number separated by t



#include<iostream>
using namespace std;
#include<string>
#include<map>
struct guest
{
    int room_No;
    string name;
    int friend_Room_No;
    public:
    void get();
};
class hotel
{
    int num_Of_Guest;
    map<int,guest> stay_Det;
    //this room number is with jegan
    int first_Room_No;
    public:
    void get();
    //this function should print
    //details such as name and room number
    // of guest to serve coffee
    void serve_Coffee();
};

ANSWER-


void guest :: get()
{
    cin>>name>>friend_Room_No;
}
void hotel :: get()
{
    int val;
    guest g;
    cin>>num_Of_Guest;
    for(int i=0;i<num_Of_Guest;i++)
    {
        cin>>val;
        g.get();
        stay_Det.insert(pair <int,guest> (val,g));
    }
    cin>>first_Room_No;
}
void hotel :: serve_Coffee()
{
    int rm=first_Room_No;
    while(rm!=-1)
    {
        cout<<stay_Det[rm].name<<" "<<rm<<"\n";
        rm=stay_Det[rm].friend_Room_No;
    }
}



PREWRITTEN-

main()
{
hotel h;
h.get();
h.serve_Coffee();

}


Q-Given a set of names, design an algorithm and write a C++ code to form a sorted list, in the descending order of names that do  begin with a vowel.
Hint: Use STL for implementation
Input Format:
Number of names, 'n'
Name 1
Name 2

Name n
Output Format:

Names that do  begin with a vowel in a descending  order.

ANS-
int main()
{
    int num;
    string name;
    vector <string> names;
    cin>>num;
    for(int i=0;i<num;i++)
    {
        cin>>name;
        names.push_back(name);
    }
    sort(names.begin(),names.end());
    for(vector <string> :: iterator i=names.end();i!=names.begin();i--)
    {
        name=*(i-1);
        if(name[0]=='A' || name[0]=='E' || name[0]=='I' || name[0]=='O' || name[0]=='U')
        cout<<name<<"\n";
    }
    return(0);

}


Q-3-D points- multiply and increment

Given a point in three-dimensional space with x-coordinate, y-coordinate, z-coordinates,  Provide a function increment with one, two and three parameters to transform  the values of all the three coordinates. When the function with one parameter is called increment all the three , by same value .  When the function with two arguments is called multiply each coordinates by the sum  of the two parameters. when the function with three arguments is called, increment x-coordinate  by the value of first parameter, y-coordinate by the value of the second parameter and z-coordinate  by the value of the third parameter.
Input format:
Enter the coordinates of the point
X-coordinate
Y-coordinate
Z-coordinate
Enter the increment value for function with one parameter
Enter the values for the function with two parameters
Enter the  increment values for function with three parameters
Output format:
Display the coordinates of the point after calling function with one parameter
Display the coordinates of the point after calling function with two parameters

Display the coordinates of the point after calling function with three parameters

C++ code-


#include<iostream>

using namespace std;

class dim

{

    int x,y,z;

    public :

    void get()
    {
        cin>>x>>y>>z;
    }
   void increment(int val)
    {
        x+=val;
        y+=val;
        z+=val;
    }
    void increment(int val1,int val2)
    {        
x*=val1+val2;
        y*=val1+val2;
        z*=val1+val2;
    }
    void increment(int val1,int val2,int val3)
    {
        x+=val1;
        y+=val2;
        z+=val3;
    }
    void print()
    {
        cout<<x<<"\n"<<y<<"\n"<<z<<"\n";
    }
};

prewritten-

int main()
{
dim d1;
d1.get();
int inc;
cin>>inc;
int p1,p2;
cin>>p1>>p2;
int ix,iy,iz;
cin>>ix>>iy>>iz;
d1.increment(inc);
d1.print();
d1.increment(p1,p2);
d1.print();
d1.increment(ix,iy,iz);
d1.print();
}


PSeudocode-

start
step1-derive a class dim
step2-make functions get() which takes input of x,y,z.
step3-make another function increment which increment the values and returns them and a similar one using pointers.
step4-make another function called as print which prints the value of x,y,z.
stop.


Processing-

void increment(int val1,int val2)
    {        
x*=val1+val2;
        y*=val1+val2;
        z*=val1+val2;
    }
    void increment(int val1,int val2,int val3)
    {
        x+=val1;
        y+=val2;
        z+=val3;
    }

GPT4ALL - A new LLaMa (Large Language Model)

posted 29th March, 2023 - 11:50, GPT4ALL launched 1 hr ago  What if we use AI generated prompt and response to train another AI - Exactly th...