Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Use FP, Luke! solution in Creative category for On the same path by obone
from typing import Iterable, List, Tuple, Union
Node = Union[int, str]
Tree = Tuple[Node, List['Tree']]
def on_same_path(tree: Tree, pairs: List[Tuple[Node, Node]]) -> Iterable[bool]:
"""For each given pair of tree's nodes, say if there are on a same path."""
find = lambda x, t: t if not t or t[0] == x else \
([[]] + [y for t1 in t[1] for y in [find(x, t1)] if y])[-1]
return (bool(find(p[0], find(p[1], tree)) or find(p[1], find(p[0], tree))) for p in pairs)
May 8, 2020