Python backtracking method to achieve array array output instance analysis

  • 2020-04-02 14:41:19
  • OfStack

This article illustrates an example of python backtracking method to achieve array array output. Share with you for your reference. Specific analysis is as follows:

Explanation of full arrangement: take m (m Or less n) elements from n different elements, arrange them in a certain order, it is called an arrangement of taking m elements from n different elements. All permutations when m is equal to n are called all permutations.


from sys import stdout
#code from //www.jb51.net/
def perm(li, start, end):
  if(start == end):
    for elem in li:
      stdout.write(elem)
    print ''
  else:
    for i in range(start, end):
      li[start], li[i] = li[i], li[start]
      perm(li, start+1, end)
      li[i], li[start] = li[start], li[i]
if __name__ == '__main__':
  li = ['a','b','c','d']
  perm(li, 0, len(li))

I hope this article has helped you with your Python programming.


Related articles: