Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for What Is Wrong With This Family? by Askanio234
def is_family(tree):
family_tree = dict()
for human in tree:
if human[0] not in family_tree:
family_tree[human[0]] = [human[1]]
else:
family_tree[human[0]].append(human[1])
patriarchs = 0
for human in family_tree:
# Exclude when you are your father's father
for child in family_tree[human]:
if family_tree.get(child) is not None:
if human in family_tree.get(child):
return False
# Exclude you are father of your brother
if len(family_tree[human]) > 1:
children = set(family_tree[human])
for child in children:
if family_tree.get(child) is not None:
if set(family_tree.get(child)).intersection(children):
return False
# Ensure there is only one patriarch
has_father = False
for childs in family_tree.values():
if human in childs:
has_father = True
if not has_father:
patriarchs += 1
if patriarchs > 1:
return False
return True
if __name__ == "__main__":
# These "asserts" using only for self-checking and not necessary for auto-testing
assert is_family([
['Logan', 'Mike']
]) == True, 'One father, one son'
assert is_family([
['Logan', 'Mike'],
['Logan', 'Jack']
]) == True, 'Two sons'
assert is_family([
['Logan', 'Mike'],
['Logan', 'Jack'],
['Mike', 'Alexander']
]) == True, 'Grandfather'
assert is_family([
['Logan', 'Mike'],
['Logan', 'Jack'],
['Mike', 'Logan']
]) == False, 'Can you be a father for your father?'
assert is_family([
['Logan', 'Mike'],
['Logan', 'Jack'],
['Mike', 'Jack']
]) == False, 'Can you be a father for your brather?'
assert is_family([
['Logan', 'William'],
['Logan', 'Jack'],
['Mike', 'Alexander']
]) == False, 'Looks like Mike is stranger in Logan\'s family'
print("Looks like you know everything. It is time for 'Check'!")
assert is_family([["Logan","William"],["William","Jack"],["Jack","Mike"],["Mike","Alexander"]]) == True
July 20, 2018