code: def coset_enumeration_r(
      fp_grp,
      Y,
      max_cosets=None,
      draft=None,
      incomplete=False,
      modified=False,
  ):
      """
      This is easier of the two implemented methods of coset enumeration.
      and is often called the HLT method, after Hazelgrove, Leech, Trotter
      The idea is that we make use of ``scan_and_fill`` makes new definitions
      whenever the scan is incomplete to enable the scan to complete; this way
      we fill in the gaps in the scan of the relator or subgroup generator,
      that's why the name relator-based method.
  
      An instance of `CosetTable` for `fp_grp` can be passed as the keyword
      argument `draft` in which case the coset enumeration will start with
      that instance and attempt to complete it.
  
      When `incomplete` is `True` and the function is unable to complete for
      some reason, the partially complete table will be returned.
  
      # TODO: complete the docstring
  
      See Also
      ========
  
      scan_and_fill,
  
      Examples
      ========
  
      >>> from sympy.combinatorics.free_groups import free_group
      >>> from sympy.combinatorics.fp_groups import FpGroup, coset_enumeration_r
      >>> F, x, y = free_group("x, y")
  
      # Example 5.1 from [1]
      >>> f = FpGroup(F, [x**3, y**3, x**-1*y**-1*x*y])
      >>> C = coset_enumeration_r(f, [x])
      >>> for i in range(len(C.p)):
      ...     if C.p[i] == i:
      ...         print(C.table[i])
      [0, 0, 1, 2]
      [1, 1, 2, 0]
      [2, 2, 0, 1]
      >>> C.p
      [0, 1, 2, 1, 1]
  
      # Example from exercises Q2 [1]
      >>> f = FpGroup(F, [x**2*y**2, y**-1*x*y*x**-3])
      >>> C = coset_enumeration_r(f, [])
      >>> C.compress(); C.standardize()
      >>> C.table
      [[1, 2, 3, 4],
      [5, 0, 6, 7],
      [0, 5, 7, 6],
      [7, 6, 5, 0],
      [6, 7, 0, 5],
      [2, 1, 4, 3],
      [3, 4, 2, 1],
      [4, 3, 1, 2]]
  
      # Example 5.2
      >>> f = FpGroup(F, [x**2, y**3, (x*y)**3])
      >>> Y = [x*y]
      >>> C = coset_enumeration_r(f, Y)
      >>> for i in range(len(C.p)):
      ...     if C.p[i] == i:
      ...         print(C.table[i])
      [1, 1, 2, 1]
      [0, 0, 0, 2]
      [3, 3, 1, 0]
      [2, 2, 3, 3]
  
      # Example 5.3
      >>> f = FpGroup(F, [x**2*y**2, x**3*y**5])
      >>> Y = []
      >>> C = coset_enumeration_r(f, Y)
      >>> for i in range(len(C.p)):
      ...     if C.p[i] == i:
      ...         print(C.table[i])
      [1, 3, 1, 3]
      [2, 0, 2, 0]
      [3, 1, 3, 1]
      [0, 2, 0, 2]
  
      # Example 5.4
      >>> F, a, b, c, d, e = free_group("a, b, c, d, e")
      >>> f = FpGroup(F, [a*b*c**-1, b*c*d**-1, c*d*e**-1, d*e*a**-1, e*a*b**-1])
      >>> Y = [a]
      >>> C = coset_enumeration_r(f, Y)
      >>> for i in range(len(C.p)):
      ...     if C.p[i] == i:
      ...         print(C.table[i])
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  
      # example of "compress" method
      >>> C.compress()
      >>> C.table
      [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
  
      # Exercises Pg. 161, Q2.
      >>> F, x, y = free_group("x, y")
      >>> f = FpGroup(F, [x**2*y**2, y**-1*x*y*x**-3])
      >>> Y = []
      >>> C = coset_enumeration_r(f, Y)
      >>> C.compress()
      >>> C.standardize()
      >>> C.table
      [[1, 2, 3, 4],
      [5, 0, 6, 7],
      [0, 5, 7, 6],
      [7, 6, 5, 0],
      [6, 7, 0, 5],
      [2, 1, 4, 3],
      [3, 4, 2, 1],
      [4, 3, 1, 2]]
  
      # John J. Cannon; Lucien A. Dimino; George Havas; Jane M. Watson
      # Mathematics of Computation, Vol. 27, No. 123. (Jul., 1973), pp. 463-490
      # from 1973chwd.pdf
      # Table 1. Ex. 1
      >>> F, r, s, t = free_group("r, s, t")
      >>> E1 = FpGroup(F, [t**-1*r*t*r**-2, r**-1*s*r*s**-2, s**-1*t*s*t**-2])
      >>> C = coset_enumeration_r(E1, [r])
      >>> for i in range(len(C.p)):
      ...     if C.p[i] == i:
      ...         print(C.table[i])
      [0, 0, 0, 0, 0, 0]
  
      Ex. 2
      >>> F, a, b = free_group("a, b")
      >>> Cox = FpGroup(F, [a**6, b**6, (a*b)**2, (a**2*b**2)**2, (a**3*b**3)**5])
      >>> C = coset_enumeration_r(Cox, [a])
      >>> index = 0
      >>> for i in range(len(C.p)):
      ...     if C.p[i] == i:
      ...         index += 1
      >>> index
      500
  
      # Ex. 3
      >>> F, a, b = free_group("a, b")
      >>> B_2_4 = FpGroup(F, [a**4, b**4, (a*b)**4, (a**-1*b)**4, (a**2*b)**4, \
              (a*b**2)**4, (a**2*b**2)**4, (a**-1*b*a*b)**4, (a*b**-1*a*b)**4])
      >>> C = coset_enumeration_r(B_2_4, [a])
      >>> index = 0
      >>> for i in range(len(C.p)):
      ...     if C.p[i] == i:
      ...         index += 1
      >>> index
      1024
  
      References
      ==========
  
      .. [1] Holt, D., Eick, B., O'Brien, E.
             "Handbook of computational group theory"
  
      """
      # 1. Initialize a coset table C for < X|R >
      C = CosetTable(fp_grp, Y, max_cosets=max_cosets)
      # Define coset table methods.
      if modified:
          _scan_and_fill = C.modified_scan_and_fill
          _define = C.modified_define
      else:
          _scan_and_fill = C.scan_and_fill
          _define = C.define
      if draft:
          C.table = draft.table[:]
          C.p = draft.p[:]
      R = fp_grp.relators
      A_dict = C.A_dict
      p = C.p
      for i in range(len(Y)):
          if modified:
              _scan_and_fill(0, Y[i], C._grp.generators[i])
          else:
              _scan_and_fill(0, Y[i])
      alpha = 0
      while alpha < C.n:
          if p[alpha] == alpha:
              try:
                  for w in R:
                      if modified:
                          _scan_and_fill(alpha, w, C._grp.identity)
                      else:
                          _scan_and_fill(alpha, w)
                      # if alpha was eliminated during the scan then break
                      if p[alpha] < alpha:
                          break
                  if p[alpha] == alpha:
                      for x in A_dict:
                          if C.table[alpha][A_dict[x]] is None:
                              _define(alpha, x)
              except ValueError as e:
                  if incomplete:
                      return C
                  raise e
          alpha += 1
      return C
  
imports: from sympy.combinatorics.free_groups import free_group
  from sympy.printing.defaults import DefaultPrinting
  from itertools import chain, product
  from bisect import bisect_left
label_desc: Use image node_id 1 for calling a global function with example usage: coset_enumeration_r(fp_grp, Y, max_cosets, draft, incomplete, modified) and returns: C, C