(path_a, path_b)
return lowest_common, min_height
Find the element with the smallest height that appears in both given lists.

  The height of an element here is defined as the maximum over the indices where
  it occurs in the two lists. For example if path_a = [2, 3, 5] and
  path_b = [5, 6, 2] then the height of element 2 is max(0 + 2) = 2 since the
  element 2 occurs in position 0 in the first list and position 2 in the second.

  Args:
    path_a: A list.
    path_b: A list.

  Returns:
    lowest_common: The element with the smallest 'height' that is common between
      path_a and path_b.
    height: The height of lowest_common, computed as described above.
Find the element with the smallest height that appears in both given lists.
Find the element with the smallest height that appears in both given lists .
def find_lowest_common_in_paths(path_a, path_b):
  """Find the element with the smallest height that appears in both given lists.

  The height of an element here is defined as the maximum over the indices where
  it occurs in the two lists. For example if path_a = [2, 3, 5] and
  path_b = [5, 6, 2] then the height of element 2 is max(0 + 2) = 2 since the
  element 2 occurs in position 0 in the first list and position 2 in the second.

  Args:
    path_a: A list.
    path_b: A list.

  Returns:
    lowest_common: The element with the smallest 'height' that is common between
      path_a and path_b.
    height: The height of lowest_common, computed as described above.
  """
  # Maps elements that appear in both lists to their heights.
  common_elements, heights = [], []
  for element in path_a:
    if element in path_b:
      height = max(path_a.index(element), path_b.index(element))
      common_elements.append(element)
      heights.append(height)

  if not heights:
    raise ValueError('No common nodes in given paths {} and {}.'.format(
        [n.words for n in path_a], [n.words for n in path_b]))

  # Find the lowest common element.
  # There may be multiple common ancestors that share the same minimal height.
  # In that case the first one appearing in common_elements will be returned.
  min_height = min(heights)
  argmin_height = heights.index(min_height)
  lowest_common = common_elements[argmin_height]
  assert min_height > 0, ('The lowest common ancestor between two distinct '
                          'leaves cannot be a leaf.')
  return lowest_common, min_height
def find_lowest_common_in_paths ( path_a , path_b ) : # Maps elements that appear in both lists to their heights. common_elements , heights = [ ] , [ ] for element in path_a : if element in path_b : height = max ( path_a . index ( element ) , path_b . index ( element ) ) common_elements . append ( element ) heights . append ( height ) if not heights : raise ValueError ( 'No common nodes in given paths {} and {}.' . format ( [ n . words for n in path_a ] , [ n . words for n in path_b ] ) ) # Find the lowest common element. # There may be multiple common ancestors that share the same minimal height. # In that case the first one appearing in common_elements will be returned. min_height = min ( heights ) argmin_height = heights . index ( min_height ) lowest_common = common_elements [ argmin_height ] assert min_height > 0 , ( 'The lowest common ancestor between two distinct ' 'leaves cannot be a leaf.' ) return lowest_common , min_height