Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
dictionary solution in Clear category for What Is Wrong With This Family? by kurosawa4434
def is_family(tree):
book = {}
# step 1:
# check tree & make family's dictionary.
for f, s in tree:
if (f == s or # father == son..
s in book and f in book[s] or # my father is my son..
sum(s in sons for sons in book.values())): # The son already exists other family.
return False
book[f] = book.get(f, []) + [s]
# if one family, done.
if len(book) == 1:
return True
# step 2:
# if two ro more families, check relation.
for f, sons in book.items():
if (sum(f in book[ff] for ff in book.keys()) or # The father is other family's son.
sum(s in book for s in sons)): # The son is other family's father.
continue
return False
return True
Oct. 6, 2016
Comments: