Answered step by step
Verified Expert Solution
Question
1 Approved Answer
in python Task B: Combine Trees (7 Marks) Implement a function combine_trees (f1, f2) with the following specification. Input: Two family tree databases, f1 and
in python
Task B: Combine Trees (7 Marks) Implement a function combine_trees (f1, f2) with the following specification. Input: Two family tree databases, f1 and f2. Output: A new family tree database, sorted by name, that combines f1 and f2 together, merging any duplicate entries, or None if there is some conflict that prevents the two databases from being merged. For example, say we have the following two databases, duck_tree and duck_tree2, which contain overlapping information: >>> duck_tree = [['Donald Duck', 'Quackmore Duck', 'Hortense McDuck'], ['Della Duck', 'Quackmore Duck', 'Hortense McDuck'], ['Hortense McDuck', 'Fergus McDuck', 'Downy ODrake'], [Scrooge McDuck', None, 'Downy ODrake'], ['Huey Duck', None, Della Duck'], ['Dewey Duck', None, Della Duck'], [Louie Duck', None, Della Duck']] >>> duck_tree2 = [['Scrooge McDuck', 'Fergus McDuck', None], ['Matilda McDuck', 'Fergus McDuck', 'Downy ODrake'], ['Hortense McDuck', 'Fergus McDuck', 'Downy ODrake'], ['Fergus McDuck', 'Dingus McDuck', 'Molly Mallard']] Then the combine trees function should behave as follows: >>> combine_trees (duck_tree, duck_tree2) [['Della Duck', 'Quackmore Duck', 'Hortense McDuck'], ['Dewey Duck', None, 'Della Duck'), ['Donald Duck', 'Quackmore Duck', Hortense McDuck'], ['Fergus McDuck', 'Dingus McDuck', 'Molly Mallard'], ['Hortense McDuck', 'Fergus McDuck', 'Downy ODrake'], ['Huey Duck', None, 'Della Duck'], ['Louie Duck', None, 'Della Duck'], ['Matilda McDuck', 'Fergus McDuck', 'Downy ODrake'], ['Scrooge McDuck', 'Fergus McDuck', 'Downy ODrake']] Note, in the above example: the identical entries for 'Hortense McDuck' have been merged so that only a single entry is in the output database; and the non-identical but non-conflicting entries for 'Scrooge McDuck' have been merged to retain the max- imum amount of information (Scrooge's mother and father are both recorded in the output database, whereas in each of the input databases, at most one parent was recorded.) The two input databases should remain unchanged after function execution. That is, after calling the function, we still have: >>> duck_tree [['Donald Duck', 'Quackmore Duck', 'Hortense McDuck'], ['Della Duck', 'Quackmore Duck', 'Hortense McDuck'], ['Hortense McDuck', 'Fergus McDuck', 'Downy ODrake'], ['Scrooge McDuck', None, Downy ODrake'], ['Huey Duck', None, 'Della Duck'], ['Dewey Duck', None, 'Della Duck'], ['Louie Duck', None, 'Della Duck']] >>> duck_tree2 [['Scrooge McDuck', 'Fergus McDuck', None], ['Matilda McDuck', 'Fergus McDuck', 'Downy ODrake'], ['Hortense McDuck', 'Fergus McDuck', 'Downy ODrake'], ['Fergus McDuck', 'Dingus McDuck', 'Molly Mallard']] A conflict is defined as the same person having at least one entry in both of the input databases such that either their mother or father (or both) is listed differently in each database. An entry of None for a person's mother or father should not cause a conflict. In the case of a conflict, the function should return None. For example: >>> combine_trees(duck_tree, [['Donald Duck', 'Scrooge McDuck', 'Hortense McDuck']]) Combining non-conflicting family trees from different sources can then allow for more information to be obtained from relational searches: >>> grandparents('Scrooge McDuck', combine_trees (duck_tree, duck_tree2)) ['Dingus McDuck', 'Molly Mallard'] Task B: Combine Trees (7 Marks) Implement a function combine_trees (f1, f2) with the following specification. Input: Two family tree databases, f1 and f2. Output: A new family tree database, sorted by name, that combines f1 and f2 together, merging any duplicate entries, or None if there is some conflict that prevents the two databases from being merged. For example, say we have the following two databases, duck_tree and duck_tree2, which contain overlapping information: >>> duck_tree = [['Donald Duck', 'Quackmore Duck', 'Hortense McDuck'], ['Della Duck', 'Quackmore Duck', 'Hortense McDuck'], ['Hortense McDuck', 'Fergus McDuck', 'Downy ODrake'], [Scrooge McDuck', None, 'Downy ODrake'], ['Huey Duck', None, Della Duck'], ['Dewey Duck', None, Della Duck'], [Louie Duck', None, Della Duck']] >>> duck_tree2 = [['Scrooge McDuck', 'Fergus McDuck', None], ['Matilda McDuck', 'Fergus McDuck', 'Downy ODrake'], ['Hortense McDuck', 'Fergus McDuck', 'Downy ODrake'], ['Fergus McDuck', 'Dingus McDuck', 'Molly Mallard']] Then the combine trees function should behave as follows: >>> combine_trees (duck_tree, duck_tree2) [['Della Duck', 'Quackmore Duck', 'Hortense McDuck'], ['Dewey Duck', None, 'Della Duck'), ['Donald Duck', 'Quackmore Duck', Hortense McDuck'], ['Fergus McDuck', 'Dingus McDuck', 'Molly Mallard'], ['Hortense McDuck', 'Fergus McDuck', 'Downy ODrake'], ['Huey Duck', None, 'Della Duck'], ['Louie Duck', None, 'Della Duck'], ['Matilda McDuck', 'Fergus McDuck', 'Downy ODrake'], ['Scrooge McDuck', 'Fergus McDuck', 'Downy ODrake']] Note, in the above example: the identical entries for 'Hortense McDuck' have been merged so that only a single entry is in the output database; and the non-identical but non-conflicting entries for 'Scrooge McDuck' have been merged to retain the max- imum amount of information (Scrooge's mother and father are both recorded in the output database, whereas in each of the input databases, at most one parent was recorded.) The two input databases should remain unchanged after function execution. That is, after calling the function, we still have: >>> duck_tree [['Donald Duck', 'Quackmore Duck', 'Hortense McDuck'], ['Della Duck', 'Quackmore Duck', 'Hortense McDuck'], ['Hortense McDuck', 'Fergus McDuck', 'Downy ODrake'], ['Scrooge McDuck', None, Downy ODrake'], ['Huey Duck', None, 'Della Duck'], ['Dewey Duck', None, 'Della Duck'], ['Louie Duck', None, 'Della Duck']] >>> duck_tree2 [['Scrooge McDuck', 'Fergus McDuck', None], ['Matilda McDuck', 'Fergus McDuck', 'Downy ODrake'], ['Hortense McDuck', 'Fergus McDuck', 'Downy ODrake'], ['Fergus McDuck', 'Dingus McDuck', 'Molly Mallard']] A conflict is defined as the same person having at least one entry in both of the input databases such that either their mother or father (or both) is listed differently in each database. An entry of None for a person's mother or father should not cause a conflict. In the case of a conflict, the function should return None. For example: >>> combine_trees(duck_tree, [['Donald Duck', 'Scrooge McDuck', 'Hortense McDuck']]) Combining non-conflicting family trees from different sources can then allow for more information to be obtained from relational searches: >>> grandparents('Scrooge McDuck', combine_trees (duck_tree, duck_tree2)) ['Dingus McDuck', 'Molly Mallard']
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started