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