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 zhang9820110
def is_family(tree):
son = set([l[1] for l in tree])
father = set([l[0] for l in tree])
trees = set([frozenset(l) for l in tree])
if len(trees) != len(tree):
return False
fcount = 0
for f in father:
if f not in son:
fcount += 1
if fcount != 1:
return False
for s in son:
fset = set()
for t in tree:
if t[0] == t[1]:
return False
if t[1] == s:
fset.add(t[0])
if len(fset) != 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 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'!")
May 8, 2017