Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for On the same path by tokiojapan55
from typing import Iterable, List, Tuple, Union
Node = Union[int, str]
Tree = Tuple[Node, List['Tree']]
def trace(path, tree, pairs, result):
name,children = tree
for i,pair in enumerate(pairs):
if set(pair) <= set(path + [name]):
result[i] = True
for child in children:
trace(path + [name], child, pairs, result)
return result
def on_same_path(tree: Tree, pairs: List[Tuple[Node, Node]]) -> Iterable[bool]:
return trace([], tree, pairs, [False for p in pairs])
June 17, 2020
Comments: