“HAPPINESS SHOULD BE A FUNCTION WITHOUT ANY PARAMETERS”

WEEK:10 PROGRAMMING ASSIGNMENT 2 SOLUTION (Jan 2020-Apr2020) An Introduction to Programming through C++ NPTEL 2020

  

               An Introduction To Programming Through C++


IIT Bombay.



Week 10- Programming Assignment 2:




Write a program that reads an integer n, and then n pairs of names, and finally two additional names S, B.  Each name is a sequence of non whitespace characters delimited by whitespace.

In each pair the first name is the name of an employee and the second name is the name of the boss of the employee.  Assume that each employee has at most one boss.

Say that an employee E is "above" an employee F if E is the boss of F, or E is the boss of the boss of F, or the boss of the boss of the boss of F, and so on.

Your program is to print 1 if employee B is above employee S and 0 otherwise.

Public Test Cases:

Public Test Cases: 3 / 3 Passed

Public Test Cases Input Expected Output Actual Output Status
Test Case 1
3
Michael
God
Gabriel
God
Lucifer
God
Gabriel
God
1\n
1\n
Passed

Test Case 2
5
Bruce
Tony
Peter
Bruce
Stephen
Peter
Clark
Bruce
Diana
Bruce
Tony
Stephen
0\n
0\n
Passed

Test Case 3
4
Castiel
Diptesh
Diptesh
Kevin
Kevin 
Rudra
Rudra
Pushpak
Castiel
Pushpak
1\n
1\n
Passed


WRITE/COPY THESE CODE DIRECTLY IN NPTEL TERMINAL AND COMPILE AND RUN THEN SUBMIT.

PROGRAM: COPY THE CODE AS PROGRAM IS TOO BIG.

using namespace std;

int main()
{
  map<string,string> boss;
  int n; cin >> n;
  repeat(n)
  {
    string s,b; cin >> s >> b;
    boss[s] = b;
  }

  string S,B; cin >> S >> B;
  while(true)
  {
    if(B == S)
    {
      cout << 1 << endl;
      break;
    }
    
    if(boss.count(S) == 0)
    {
      cout << 0 << endl;
      break;
    }
    
    S = boss[S];
  }
}
 
WEEK-10 :PROGRAMMING ASSIGNMENT 1 AND 3 
CLICK ON BELOW LINK.
<<CLICK HERE FOR  WEEK 10:PROGRAMMING -01>>
> <<CLICK HERE FOR  WEEK 10:PROGRAMMING-03>> FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100%  correct solutions.

No comments:

Post a comment

If you have any problems related to solutions or any concept please let me know.