Neural Sync Active
Jan 2026 - Python - Week 9 - WEEK9-GrPA 5
Registry Synced
Jan 2026 - Python - Week 9 - WEEK9-GrPA 5
745 words
4 min read
Graded Assignment
Course: Jan 2026 - Python
WEEK9-GrPA 5
Submission deadline has passed for this assignment
Due Apr 17, 2026 at 11:59 PM IST
Instructions
Use "Test Run" to verify your code with public test cases.
Press "Submit" to have your assignment evaluated.
You can submit your assignment multiple times up until the deadline.
Make sure to submit your final code by the deadline to receive your score.
Summary
100 out of100
Score
Public Tests
2/2 Passed
Submitted on Apr 17, 2026 at 5:35 AM IST
Private Tests
5/5 Passed
Submitted on Apr 17, 2026 at 5:35 AM IST
P is a dictionary of father-son relationships that has the following structure: for any key in the dictionary, its corresponding value is the father of key. As an example:
P = { 'Jahangir': 'Akbar', 'Akbar': 'Humayun', 'Humayun': 'Babur' }
If 'Jahangir' is the key, then 'Akbar', his father, is the value. This is true of every key in the dictionary.
Write a recursive function named ancestry that accepts the following arguments:
- P: dictionary of relationships
- present: name of a person, string
- past: name of a person, string
It should return the sequence of ancestors of the person named present, traced all the way back up to person named past. For example, ancestry(P, 'Jahangir', 'Babur') should return the list:
L = ['Jahangir', 'Akbar', 'Humayun', 'Babur']
In more Pythonic terms, L[i] is the father of L[i - 1], for 1≤i<len(L), with the condition that L[0] should be present and L[-1] should be past.
(1) You can assume that no two persons in the dictionary have the same name. However, a given person could either appear as a key or as a value in the dictionary.
(2) A given person could appear multiple times as one of the values of the dictionary. For example, in test-case-2, Prasanna has two sons, Mohan and Krishna, and hence appears twice (as a value).
(3) You do not have to accept input from the user or print output to the console. You just have to write the function definition.
Public Tests ( 2/2 )
Case 1
Input:
textpublic {'Jahangir': 'Akbar', 'Akbar': 'Humayun', 'Humayun': 'Babur'} Jahangir Babur
Expected Output:
text['Jahangir', 'Akbar', 'Humayun', 'Babur']
Actual Output:
text['Jahangir', 'Akbar', 'Humayun', 'Babur']
Case 2
Input:
textpublic {'Anil': 'Krishna', 'Mohan': 'Prasanna', 'Krishna': 'Prasanna', 'Prasanna': 'Mukesh'} Anil Prasanna
Expected Output:
text['Anil', 'Krishna', 'Prasanna']
Actual Output:
text['Anil', 'Krishna', 'Prasanna']
Private Tests ( 5/5 )
Case 1
Input:
textprivate {'Sam': 'Peter', 'George': 'Peter', 'Peter': 'John', 'John': 'Alex', 'Alex': 'Andrew', 'Andrew': 'Adam'} George Andrew
Expected Output:
text['George', 'Peter', 'John', 'Alex', 'Andrew'] Good, your function is recursive
Actual Output:
text['George', 'Peter', 'John', 'Alex', 'Andrew'] Good, your function is recursive
Case 2
Input:
textprivate {'Sam': 'Peter', 'George': 'Peter', 'Peter': 'John', 'John': 'Alex', 'Alex': 'Andrew', 'Andrew': 'Adam'} Sam Adam
Expected Output:
text['Sam', 'Peter', 'John', 'Alex', 'Andrew', 'Adam'] Good, your function is recursive
Actual Output:
text['Sam', 'Peter', 'John', 'Alex', 'Andrew', 'Adam'] Good, your function is recursive
Case 3
Input:
textprivate {'Jahangir': 'Akbar', 'Akbar': 'Humayun', 'Humayun': 'Babur'} Humayun Babur
Expected Output:
text['Humayun', 'Babur'] Good, your function is recursive
Actual Output:
text['Humayun', 'Babur'] Good, your function is recursive
Case 4
Input:
textprivate {'Anil': 'Krishna', 'Mohan': 'Prasanna', 'Krishna': 'Prasanna','Sachin': 'Prasanna', 'Prasanna': 'Sourav'} Mohan Sourav
Expected Output:
text['Mohan', 'Prasanna', 'Sourav'] Good, your function is recursive
Actual Output:
text['Mohan', 'Prasanna', 'Sourav'] Good, your function is recursive
Case 5
Input:
textprivate {'Anil': 'Krishna', 'Mohan': 'Prasanna', 'Krishna': 'Prasanna','Sachin': 'Prasanna', 'Prasanna': 'Sourav'} Anil Sourav
Expected Output:
text['Anil', 'Krishna', 'Prasanna', 'Sourav'] Good, your function is recursive
Actual Output:
text['Anil', 'Krishna', 'Prasanna', 'Sourav'] Good, your function is recursive
💻 IITM Official Solution
pythondef ancestry(P, present, past): if present == past: return [past] return [present] + ancestry(P, P[present], past)
💻 My Submitted Code
pythondef ancestry(P, present, past): """ A recursive function to compute the sequence of ancestors of person Arguments: P: dict, key and value are strings present: string past: string Return: result: list of strings """ # Base case if present == past: return [present] # Recursive case return [present] + ancestry(P, P[present], past)