old_contents: from collections import namedtuple
  
  
  Match = namedtuple('Match', ['start', 'end', 'dist'])
  
  
  class GroupOfMatches(object):
      def __init__(self, match):
          self.start = match.start
          self.end = match.end
          self.matches = set([match])
  
      def is_match_in_group(self, match):
          return match in self.matches or \
              not (match.end <= self.start or match.start >= self.end)
  
      def does_overlap_group(self, other):
          return not (other.end <= self.start or other.start >= self.end)
  
      def add_match(self, match):
          self.matches.add(match)
          self.start = min(self.start, match.start)
          self.end = max(self.end, match.end)
  
  
  def group_matches(matches):
      groups = []
      for match in matches:
          overlapping_groups = [g for g in groups if g.is_match_in_group(match)]
          if not overlapping_groups:
              groups.append(GroupOfMatches(match))
          elif len(overlapping_groups) == 1:
              overlapping_groups[0].add_match(match)
          else:
              new_group = GroupOfMatches(match)
              for group in overlapping_groups:
                  for match in group.matches:
                      new_group.add_match(match)
              groups = [g for g in groups if g not in overlapping_groups]
              groups.append(new_group)
  
      return [group.matches for group in groups]
  
  
  def get_best_match_in_group(group):
      # return longest match amongst those with the shortest distance
      return min(group, key=lambda match: (match.dist, -(match.end - match.start)))
  
new_contents: from collections import namedtuple
  
  
  Match = namedtuple('Match', ['start', 'end', 'dist'])
  
  
  class GroupOfMatches(object):
      def __init__(self, match):
          assert match.start < match.end
          self.start = match.start
          self.end = match.end
          self.matches = set([match])
  
      def is_match_in_group(self, match):
          return not (match.end <= self.start or match.start >= self.end)
  
      def does_overlap_group(self, other):
          return not (other.end <= self.start or other.start >= self.end)
  
      def add_match(self, match):
          self.matches.add(match)
          self.start = min(self.start, match.start)
          self.end = max(self.end, match.end)
  
  
  def group_matches(matches):
      groups = []
      for match in matches:
          overlapping_groups = [g for g in groups if g.is_match_in_group(match)]
          if not overlapping_groups:
              groups.append(GroupOfMatches(match))
          elif len(overlapping_groups) == 1:
              overlapping_groups[0].add_match(match)
          else:
              new_group = GroupOfMatches(match)
              for group in overlapping_groups:
                  for match in group.matches:
                      new_group.add_match(match)
              groups = [g for g in groups if g not in overlapping_groups]
              groups.append(new_group)
  
      return [group.matches for group in groups]
  
  
  def get_best_match_in_group(group):
      # return longest match amongst those with the shortest distance
      return min(group, key=lambda match: (match.dist, -(match.end - match.start)))
  
subject: simplify and add assertion in GroupOfMatches