I. NET A, recently in the data structure and algorithm analysis, the middle involved in some of the more interesting algorithmic problems, intended to use Python to achieve the following. The reason for choosing Python is to familiarize yourself with the syntax of Python and pycharm basic applications.
In this article, the algorithm is: all permutations of the printed array are possible. Nonsense not much to say, directly on the code.
1 #automatically generate list2 defcreatalist (n):3numlist=[];4 forIinchrange (n):5 numlist.append (i);6 7 returnnumlist;8 #Copy list excludes an element9 defcopewithout (lst,index):Tennewlst=[]; One forIinchRange (len (LST)): A if(i==index): - Continue; - newlst.append (Lst[i]); the returnNewlst; - - #Print all permutations - defprintallchildlist (numlist,index,printlist,length): + if(index==length-1): -printlist[index]=numlist[0]; + Print(printlist); A Else: at forIinchRange (len (numlist)): -printlist[index]=Numlist[i]; -newnumlst=copewithout (numlist,i); -Printallchildlist (newnumlst,index+1, printlist,length); - #Main function - defdomain (num): innumlst=creatalist (num); -printlst=creatalist (num); to printallchildlist (numlst,0,printlst,num); + -Domain (3);
Here is the test result:
D:\Learning\Python\Test\venv\Scripts\python.exe d:/learning/python/test/2.13.py[0, 1, 2][0, 2, 1][1, 0, 2][1, 2, 0][2, 0 , 1, 0)
Print array all arrange Python