Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
simple recursion solution in Clear category for On the same path by kdim
from typing import Iterable, List, Tuple, Union
Node = Union[int, str]
Tree = Tuple[Node, List['Tree']]
def path(tree, pair):
pair = pair.copy()
node, tree = tree
if node in pair:
pair.remove(node)
if not pair:
return True
return bool(sum(path(branch, pair) for branch in tree))
def on_same_path(tree: Tree, pairs: List[Tuple[Node, Node]]) -> Iterable[bool]:
return [path(tree, list(pair)) for pair in pairs]
March 4, 2021