Print array all arrange Python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.