Find Generation Gap
Coding Problem Keys
Find Generation Gap
Problem Statement
The family hierarchy is given in N lines. Each lines contains parent and child separated by a space. The program must find the number of generations present between the first and last generation.
Boundary Condition(s)
1 <= N <=100
1 <= Length of parent, child <= 100
Input Format
The first line contains the N.
The next N lines contain the percent and child names separated by a space.
Output Format
The first line contains the specified output.
Example Input/Output 1
Input
3
son grandson
father son
grandfather father
Output
2
Explanation
The first generation member is grandfather.
The last generation member is grandson.
There are two generations between them (father and son) hence the output is 2.
Example Input/Output 2
Input
6
grandfather father
father son1
father son2
son1 grandson1
grandson1 greatgrandson1
grandson1 greatgrandson2
Output
3
Explanation
The first generation member is grandfather.
The last generation members are greatgrandson1 and greatgrandson2.
There are three generations between them (father, son1 and grandson1) hence the output is 3.
Note: Max Execution Time Limit: 5000 millisecs
Solution
Programming Language: Python 3 Language
# Published By PKJCODERSn=int(input()) d=dict() for i in range(n): x,y=input().strip().split() d[y]=x l=[] for x in d: q=0 y=x while(d[y] in d): q+=1 y=d[y] l.append(q) print(max(l))
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments