Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Creative category for What Is Wrong With This Family? by DEADPOOL
def is_family(tree):
tree.sort()
son = []
for f_s in tree:
son.append(f_s[1])
if len(son) != len(set(son)):
return False
family = tree[0]
tree = tree[1:]
loop = 0
while True:
if loop > len(tree) or len(tree)==0:
break
if tree[0][0] in family and tree[0][1] not in family:
family.append(tree[0][1])
tree = tree[1:]
loop = 0
elif tree[0][1] in family and tree[0][0] not in family:
family.append(tree[0][0])
tree = tree[1:]
loop = 0
else:
tree.append(tree[0])
tree = tree[1:]
loop += 1
return not len(tree)
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'!")
Jan. 13, 2019
Comments: