code: def generate_permutations(elements, n):
      
      c = [0] * n
      yield elements
      i = 0
      while i < n:
          if c[i] < i:
              if i % 2 == 0:
                  elements[0], elements[i] = elements[i], elements[0]
              else:
                  elements[c[i]], elements[i] = elements[i], elements[c[i]]
              yield elements
              c[i] += 1
              i = 0
          else:
              c[i] = 0
              i += 1
docs: Heap's algorithm for generating all n! permutations in a list
      https://en.wikipedia.org/wiki/Heap%27s_algorithm