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 Sim0000
from collections import defaultdict
def is_family(tree):
anscestor = defaultdict(set)
for father, son in tree:
if father == son: return False
if father in anscestor[son]: return False
if son in anscestor[father]: return False
if anscestor[father] & anscestor[son]: return False
anscestor[son] |= {father} | anscestor[father]
adam = [person for person in anscestor if not anscestor[person]]
return len(adam) == 1
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, 'grant father'
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'!")
Oct. 5, 2016
Comments: