2

I have written a code for "Towers of Hanoi" in python and I am trying to add a counter to show how many times it has run. I tried several things like a while loop and for loops etc. but it doesn't work. I am sure that the answer is pretty easy but my brain is running on the lowest setting right now. My code looks like this:

def Hanoi(n, src, dst, tmp):
if n > 0:
    Hanoi(n - 1, src, tmp, dst)
    print "Move disc", chr(64 + n), "From tower", src, "to tower", dst
    Hanoi(n - 1, tmp, dst, src)

Hanoi(4,0,2,1) #Just an example

I know that the code has to run (2^n)-1 times, but I just cant implement it. Because the way I wrote the code the value n changes so that would work for me. (I tried something like this:

def Hanoi(n, src, dst, tmp):

    a = 0
    while (a < (2**n)-1)
        a+=1

    if n > 0:
        Hanoi(n - 1, src, tmp, dst)
        print a, "Move disc", chr(64 + n), "From tower", src, "to tower", dst
        Hanoi(n - 1, tmp, dst, src)

but as I've said, the value n changes and I don't know how to fix it.

Edit:

To clarify, I want it to shop the number of steps like this: (If I call Hanoi(3,0,2,1))

1. Move disc A From tower 0 to tower 2
2. Move disc B From tower 0 to tower 1
3. Move disc A From tower 2 to tower 1
4. Move disc C From tower 0 to tower 2
5. Move disc A From tower 1 to tower 0
6. Move disc B From tower 1 to tower 2
7. Move disc A From tower 0 to tower 2
Zahand
  • 490
  • 3
  • 12
  • 23
  • 1
    Why have the while loop with the "a" variable? If I understand you want a counter to know how many times the function Hanoi() get's called right? If that is the case I can answer the question. – petermlm Feb 19 '14 at 09:38
  • Yes, I've added a clarification. I added a while loop because I know exaclty how many times the function will run ((2 * * n) - 1). But I want to know exactly how many times it has run while it is running if I decide to run it with a highly value as n (like f.ex. 30) – Zahand Feb 20 '14 at 10:31

4 Answers4

3

How about returning the number of calls from your function:

def Hanoi(n, src, dst, tmp):
    if n > 0:
        pre = Hanoi(n - 1, src, tmp, dst)
        print "Move disc", chr(64 + n), "From tower", src, "to tower", dst
        post = Hanoi(n - 1, tmp, dst, src)
        return pre + post + 1
    else:
        return 1

Note that this counts the number of calls to the Hanoi function, not the number of moves that actually need to be made if you were physically playing the game. If you want the number of moves, just change the return 1 in the base case (the last line) to return 0.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
2

There's another solution without using "global". Since not every language has it.

count = 0
def Hanoi(count , n , a, c, b):
    if(n == 1):
        print(count,": Moving disk 1 from",a,"to",c)
        count+=1
        return count
    count = Hanoi(count , n-1, a, b, c)
    print(count , ": Moving disk",n,"from",a,"to",c)
    count+=1
    count = Hanoi(count , n-1, b, c, a)
    return count

Hanoi(count , 3, 'A', 'C', 'B')
0

You can use a global counter to keep track of how many time the function is called.

runcount = 0 

def Hanoi(n, src, dst, tmp):

    if n > 0:
        global runcount
        runcount += 1 
        Hanoi(n - 1, src, tmp, dst)
        print "Move disc", chr(64 + n), "From tower", src, "to tower", dst 
        Hanoi(n - 1, tmp, dst, src)

Hanoi(4,0,2,1) #Just an example

print runcount

It prints 15 at the end.

Anthony Kong
  • 37,791
  • 46
  • 172
  • 304
0

1.def hanoi(n,s,t,b):

   assert n > 0
    if n == 1:
        print 'move',s,'to',t
        return 1     #######count number of iteration
    else:
        first=hanoi(n-1,s,b,t)
        second=hanoi(1,s,t,b)
        third=hanoi(n-1,b,t,s)
        return first+second+third#####return times of 1
    ###print it on calling

for i in range(1,5):

    print 'New Hanoi Example: hanoi(',i,',source, target, buffer)'
    print '----------------------'
    print hanoi(i,'source','target','buffer')