def rhs_replace(rhs, sublist, replacement):
  """Replace occurrences of sublist in rhs with replacement."""
  sublist = tuple(sublist)
  rhs = tuple(rhs)
  if len(sublist) > len(rhs):
    raise ValueError
  if not sublist:
    raise ValueError
  new_list = []
  idx = 0
  while idx < len(rhs):
    if rhs[idx:idx + len(sublist)] == sublist:
      new_list.append(replacement)
      idx += len(sublist)
    else:
      new_list.append(rhs[idx])
      idx += 1
  return tuple(new_list)
Replace occurrences of sublist in rhs with replacement.
language/compgen/nqg/model/induction/rule_utils.py