Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Just hardcore, just brute-force solution in Clear category for What Is Wrong With This Family? by V.Shkaberda
from collections import defaultdict
from itertools import chain, permutations, tee
def is_family(tree):
# come in from the eldest one, please
for new_tree in permutations(tree, len(tree)):
family = defaultdict(list) # cozy family storage
for (f, s) in new_tree:
combo = tee(chain(family.keys(), *family.values()), 2) # all in one
if family and (s in combo[0] or f not in combo[1]):
break # trashy tree
family[f].append(s) # next one to check, please
else:
return True # not a trashy tree
return False # all the trees are trashy
Aug. 13, 2018
Comments: