Program to find out the inheritance order in a family in Python

PythonServer Side ProgrammingProgramming

Suppose, there is a family that consists of members from different generations. Such as the family has a father, his children, and their grandmother. But births and deaths happen in each family.

The eldest member of the family is considered the head. So, when the 'head' member dies, their direct successor or their children becomes the head. We implement three functions, the first one is used when a child is born into the family. The function takes the parent's name and child's name as input and adds them to the record.

The second function is used when there is a death. It takes the name of the deceased family member as input and removes them from the record.

The third function gives the order of inheritance. The current order of inheritance is printed whenever it is called.

So, for a set of inputs; we have to find out the order of inheritance. So, if the order of input is like birth, birth, birth, birth, birth, death, inheritance, death, inheritance, then the output will be ['Zach', 'Jesse', 'Ursula', 'Ryan', 'Thea'] ['Jesse', 'Ursula', 'Ryan', 'Thea']

At first the head of the family is Paul.

Then Paul had children named Zach and Jesse respectively.

Jesse then had three children; Ursula, Ryan, and Thea, Ursula being the eldest and Thea being the youngest.

Then Paul dies. The order of inheritance is ['Zach', 'Jesse', 'Ursula', 'Ryan', 'Thea'].

Then Zach dies, the order of inheritance becomes ['Jesse', 'Ursula', 'Ryan', 'Thea'].

To solve this, we will follow these steps −

• family := a new map that contains lists as values

• head := the current head of the family

• dead := a set

• Define a function birth() . This will take p_name, c_name

• insert c_name at the end of family[p_name]

• Define a function death() . This will take name

• Define a function inheritance() . This will take

• ans := a new list

• return ans

• Define a function depth_search() . This will take current

• if current is not in dead, then

• insert current at the end of ans

• for each child in family[current], do

• depth_search(child)

Example

Let us see the following implementation to get better understanding

from collections import defaultdict
class Solution:

self.family = defaultdict(list)

def birth(self, p_name, c_name):
self.family[p_name].append(c_name)

def death(self, name):

def inheritance(self):
self.ans = []
return self.ans

def depth_search(self, current):
if current not in self.dead:
self.ans.append(current)
for child in self.family[current]:
self.depth_search(child)

ob = Solution('Paul')
ob.birth('Paul', 'Zach')
ob.birth('Paul', 'Jesse')
ob.birth('Jesse', 'Ursula')
ob.birth('Jesse', 'Ryan')
ob.birth('Jesse', 'Thea')
ob.death('Paul')
print(ob.inheritance())
ob.death('Zach')
print(ob.inheritance())

Input

ob = Solution('Paul')
ob.birth('Paul', 'Zach')
ob.birth('Paul', 'Jesse')
ob.birth('Jesse', 'Ursula')
ob.birth('Jesse', 'Ryan')
ob.birth('Jesse', 'Thea')
ob.death('Paul')
print(ob.inheritance())
ob.death('Zach')
print(ob.inheritance())

Output

['Zach', 'Jesse', 'Ursula', 'Ryan', 'Thea']
['Jesse', 'Ursula', 'Ryan', 'Thea']
Published on 06-Oct-2021 07:34:03