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 Magu
def is_family(tree: list[list[str]]) -> bool:
#'Can you be a father to yourself?' 'Can you be a father to your father?' Check
for n in tree:
if [n[1],n[0]] in tree:
return False
#'Who's Your Daddy?' Check
if len([n[1] for n in tree]) != len(set([n[1] for n in tree])):
return False
return len(tree)+1 == len(set(sum(tree, [])))
if __name__ == "__main__":
#These "asserts" using only for self-checking and not necessary for auto-testing
assert is_family([
['Logan', 'Mike'],
['Alexander', 'Jack'],
['Jack', 'Alexander']
]) == False, 'Can you be a father to your father?'
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 to your father?'
assert is_family([
['Logan', 'Mike'],
['Logan', 'Jack'],
['Mike', 'Jack']
]) == False, 'Can you be a father to your brother?'
assert is_family([
['Logan', 'William'],
['Logan', 'Jack'],
['Mike', 'Alexander']
]) == False, 'Looks like Mike is stranger in Logan\'s family'
assert is_family([
['Jack', 'Mike'],
['Logan', 'Mike'],
['Logan', 'Jack'],
]) == False, 'Two fathers'
print("Looks like you know everything. It is time for 'Check'!")
Sept. 10, 2021