Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for What Is Wrong With This Family? by tom-tom
def is_family(tree):
fathers, sons = zip(*tree)
fathers_set, sons_set = set(fathers), set(sons)
relatives = [set(link) for link in tree]
family, family_len = relatives.pop(), 0
while len(family) > family_len:
family_len = len(family)
family.update(*(link for link in relatives if link & family))
return (family == fathers_set | sons_set and
len(fathers_set - sons_set) == 1 and
len(sons) == len(sons_set))
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'!")
Oct. 7, 2016