Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
9-line, very simple, without import solution in Clear category for What Is Wrong With This Family? by Koba_Rt
def is_family(tree):
fathers = [father[0] for father in tree] #all fathers
sons = [son[1] for son in tree] #all sons
if len(set(sons)) < len(sons): return False #one son can't have two fathers
if len(set(fathers) - set(sons)) != 1: return False #among the fathers, there can be only one with whom the homeland begins
for father,son in tree:
if father == son: return False
if [son,father] in tree: 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 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'
print("Looks like you know everything. It is time for 'Check'!")
Jan. 3, 2021
Comments: