Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for What Is Wrong With This Family? by Kurush
def is_family(tree: list[list[str]]) -> bool:
family = dict()
for member in tree:
if (member[1] in family):
if family[member[1]] != "":
print("You can't have two parents.")
return False
else: family[member[1]] = member[0]
else: family[member[1]] = member[0]
if (member[0] not in family):
family[member[0]] = ""
cnt = [1 if value == "" else 0 for value in family.values()].count(1)
if cnt != 1:
print("You can't have two independent trees.")
return False
for member in family:
path = set()
current = member
path.add(current)
while family[current] != "":
if family[current] not in path:
path.add(family[current])
current = family[current]
else:
print("You can't have cycles.")
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'
assert is_family([
['Jack', 'Mike'],
['Logan', 'Mike'],
['Logan', 'Jack'],
]) == False, 'Two fathers'
print("Looks like you know everything. It is time for 'Check'!")
May 4, 2021
Comments: