Hanoi (Tower of Hanoi) Python language implementation in list form

Source: Internet
Author: User

From yesterday and half the afternoon began to think about this problem, until now after 30 hours (not thinking is very concentrated, because the continuous thinking is tired and may not be effective), finally the program, the first time to think about the problem so long. The first self-thinking program is still a very fulfilling one.

Code advantages: modularity is done well, found by writing the first 4-5 times the mathematical expression, found the law and put it into code.

Code disadvantage: Poor rule, because Python without pointers (Online said, not sure), did not learn python similar to the C language pointer function, resulting in a module has to be decomposed into 6 modules to call each other. And the function is not well encapsulated, that is, because it does not have a pointer method, it uses a function to manipulate global variables .

On the code:

defac (k):GlobalaGlobalbGlobalCifk==2: b+=a[-1];a=a[:-1] C+=a[-1];a=a[:-1] C+=b[-1];b=b[:-1]    Else: AB (k-1)###############C+=a[-1];a=a[:-1] BC (k-1)defAB (k):GlobalaGlobalbGlobalCifk==2: C+=a[-1];a=a[:-1] B+=a[-1];a=a[:-1] B+=c[-1];c=c[:-1]    Else: ac (k-1) b+=a[-1];a=a[:-1] CB (k-1)defCB (k):GlobalaGlobalbGlobalCifk==2: A+=c[-1];c=c[:-1] B+=c[-1];c=c[:-1] B+=a[-1];a=a[:-1]    Else: CA (k-1) b+=c[-1];c=c[:-1] AB (k-1)defCA (k):GlobalaGlobalbGlobalCifk==2: b+=c[-1];c=c[:-1] a+=c[-1];c=c[:-1] a+=b[-1];b=b[:-1]    Else: CB (k-1) A+=c[-1];c=c[:-1] BA (k-1)defBa (k):GlobalaGlobalbGlobalCifk==2: C+=b[-1];b=b[:-1] a+=b[-1];b=b[:-1] a+=c[-1];c=c[:-1]    Else: BC (k-1) A+=b[-1];b=b[:-1] CA (k-1)defBC (k):GlobalaGlobalbGlobalCifk==2: A+=b[-1];b=b[:-1] C+=b[-1];b=b[:-1] C+=a[-1];a=a[:-1]    Else: BA (k-1) C+=b[-1];b=b[:-1] AC (k-1) A='fedcba9876543210'b="'C="'ac (Len (a))Print('a:{0}\nb:{1}\nc:{2}\n'. Format (A,B,C))

The output is:

A:
B:
c:fedcba9876543210

Extrapolate: The above code is a string form of Hanoi, limited capacity and processing more than 16 elements when it is inconvenient, followed by a list of the form of Hanoi.

Hanoi (Tower of Hanoi) Python language implementation in list form

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.