Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for On the same path by _Chico_
from typing import Iterable, List, Tuple, Union
C=lambda f,x:any(r==x or C(f,x)for r,f in f)
D=lambda r,f,p,c:C(f,c)if r==p else any(D(r,f,p,c)for r,f in f)
on_same_path=lambda t,s:[D(*t,a,b)or D(*t,b,a)for a,b in s]
if __name__ == '__main__':
example = on_same_path(
('Me', [('Daddy', [('Grandpa', []),
('Grandma', [])]),
('Mom', [('Granny', []),
('?', [])])]),
[('Grandpa', 'Me'), ('Daddy', 'Granny')],
)
print('Example: it should be [True, False].')
print(list(example))
TESTS = (
(
('Me', [('Daddy', [('Grandpa', []),
('Grandma', [])]),
('Mom', [('Granny', []),
('?', [])])]),
[('Grandpa', 'Me'), ('Daddy', 'Granny')],
[True, False],
),
(
(1, [(2, [(4, []),
(5, [(7, []),
(8, []),
(9, [])])]),
(3, [(6, [])])]),
[(1, 5), (2, 9), (2, 6)],
[True, True, False],
),
(
(0, [(1, [(2, []),
(3, [])]),
(4, [(5, []),
(6, [])]),
(7, [(8, []),
(9, [])])]),
[(4, 2), (0, 5), (2, 3), (9, 2), (6, 4), (7, 8), (8, 1)],
[False, True, False, False, True, True, False],
),
)
for test_nb, (tree, pairs, answers) in enumerate(TESTS, 1):
user_result = list(on_same_path(tree, pairs))
if user_result != answers:
print(f'You failed the test #{test_nb}.')
print(f'Your result: {user_result}')
print(f'Right result: {answers}')
break
else:
print('Well done! Click on "Check" for real tests.')
June 5, 2021