code: def swap(tokens, maxdist=2):
      
      assert maxdist >= 2
      tokens = list(tokens)
      if maxdist > len(tokens):
          maxdist = len(tokens)
      l = len(tokens)
      for i in range(0,l - 1):
          for permutation in permutations(tokens[i:i+maxdist]):
              if permutation != tuple(tokens[i:i+maxdist]):
                  newtokens = tokens[:i]
                  newtokens += permutation
                  newtokens += tokens[i+maxdist:]
                  yield newtokens
          if maxdist == len(tokens):
              break
docs: Perform a swap operation on a sequence of tokens, exhaustively swapping all tokens up to the maximum specified distance. This is a subset of all permutations.