Not Yet Answered # Towes of Hanoi help (Dont understand)

pyTony 888 Gribouillis 1,313 pyTony 888 Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

Maybe trace prints from this would clarify?:

```
def hanoi(n, a='A', b='B', c='C'):
"""
move n discs from a to c using b as middle
"""
indent = ' '*(10-n)
print indent+'hanoi(%i, %s, %s, %s)' % (n,a,b,c)
if n == 0:
return
hanoi(n-1, a, c, b)
print indent,a, '->', c
hanoi(n-1, b, a, c)
print indent,'%i from %s through %s to %s moved' % (n,a,b,c)
print
hanoi(3)
```

Or this as it leaves out the immediate one move calls from tracing and gives less prints:

```
def hanoi(n, a='A', b='B', c='C'):
"""
move n discs from a to c using b as middle
"""
indent = ' '*(10-n)
if n == 0:
return
hanoi(n-1, a, c, b)
print indent,a, '->', c
hanoi(n-1, b, a, c)
print indent,'%i from %s through %s to %s moved' % (n,a,b,c)
print
hanoi(3)
```

As you can see the calls with n = 0 do nothing and moving through other peg one actually moves the disc directly without using the help peg.

**Very clever code** as it carries only one count instead of record of current situation of all pegs. Do the action instructions your self physically with say coins, to check that it really works.

*Edited 5 Years Ago by pyTony*: n/a

0

Another explanation is to translate in pseudo code

```
how to move n disks from a to c using b as middle ?
if n is 0:
do nothing
else:
move n-1 disks from a to b using c as middle
move the last disk from a to c
move n-1 disks from b to c using a as middle
```

Edit: I don't agree with tonyjv that the code is so clever, because the solution is forced: before moving the nth disk from a to c, one *must* have moved the n-1 top disks from a to b, and afterwards, one *must* move the n-1 disks from b to c. It's the only way to solve the towers of hanoi.

*Edited 5 Years Ago by Gribouillis*: n/a

0

Maybe it was just the use of count zero base case, but for new to programming this usually **looks magic** that can not work.

In the book about LISP programming similar function is given and after it says:

Problem 7-1: the above solution may not be very convincing, since everything seems to be done blindly, without checking whether disks are ever placed on smaller disks or for that matter, whether an disks are left on the pin from which they are supposed to be moved

I would though do the "move one" the base case like Winston-Horn

```
>>> def hanoi(n, a='A', b='B', c='C'):
"""
move n discs from a to c using b as middle
"""
indent = ' '*(10-n)
if n == 1:
print indent,a, '->', c
return
print indent,'GOAL: %i from %s through %s to %s' % (n,a,b,c)
if n>2: print indent,'%i from %s through %s help peg to %s' % (n-1, a,c,b)
hanoi(n-1, a, c, b)
hanoi(1, a, b, c)
if n>2: print indent,'%i from %s through %s help peg to %s' % (n-1, b,a,c)
hanoi(n-1, b, a, c)
print indent,'%i from %s through %s to %s moved' % (n,a,b,c)
print
>>> hanoi(3)
GOAL: 3 from A through B to C
2 from A through C help peg to B
GOAL: 2 from A through C to B
A -> C
A -> B
C -> B
2 from A through C to B moved
A -> C
2 from B through A help peg to C
GOAL: 2 from B through A to C
B -> A
B -> C
A -> C
2 from B through A to C moved
3 from A through B to C moved
```

*Edited 5 Years Ago by pyTony*: n/a

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...