3
def printMove(source, destination): 
    print('move From ' + str(source) + ' to destination ' + str(destination))
    count +=1
    print count

def Towers(n, source, destination, spare):
    if not count in  locals():
        count = 0
    if n == 1:
        printMove(source, destination)
        count +=1   
    else:
        Towers(n-1, source, spare, destination)
        Towers(1, source, destination, spare)
        Towers(n-1, spare, destination, source)

I wrote this script to solve the "Towers of Hanoi". The script works wonderfully, but I also want to print the number of moves it took to solve the problem. I just cannot figure out how I can put a counter kind of thing which will count:

  1. The number of moves it will take to solve.
  2. The number of times the "Towers" function was executed.

The if not count in locals(): condition is one of the failed attempts to count the number of moves it will take to solve. Am I on the right track anyway?

Also, is this algorithm efficient? Or is there a better way to solve this?

Moreover, can someone tell me some useful application of Towers of Hanoi and the advantage of recursion? The only one that I could figure out was its simplicity.

6 Answers 6

3

One way is to carry the counter through all of the calls like this:

def towers(n, source, destination, spare, count=0):
    if n == 1:
        count += 1
        print('move From', source, ' to destination ', destination, count)
    else:
        count = towers(n-1, source, spare, destination, count)
        count = towers(1, source, destination, spare, count)
        count = towers(n-1, spare, destination, source, count)
    return count

towers(3, 1, 2, 3)

yields

move From 1  to destination  2 1
move From 1  to destination  3 2
move From 2  to destination  3 3
move From 1  to destination  2 4
move From 3  to destination  1 5
move From 3  to destination  2 6
move From 1  to destination  2 7

Regarding efficiency, http://en.wikipedia.org/wiki/Tower_of_Hanoi#Recursive_solution says: "By means of mathematical induction, it is easily proven that the above procedure requires the minimal number of moves possible, and that the produced solution is the only one with this minimal number of moves.".

The main advantage of recursion is that these solutions tend to be elegant. For some kind of problems, the iterative solution is way more complicated to express than the recursive.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, I was just about to post this. Oh well, you got to it first :)
Oh.. Thanks .. .. This works well in Python. I am actually working to port this code on an 8051 based system now but "count =0" won't work in C :(
2

I like this version even better, without the extra parameter 'count':

def towers(n, source, destination, spare):
    count = 0
    if n == 1:
        print('move From', source, ' to destination ', destination)
        return 1
    else:
        count += towers(n-1, source, spare, destination)
        count += towers(1, source, destination, spare)
        count += towers(n-1, spare, destination, source)
        return count

print(towers(3, 1, 2, 3))

yields

move From 1  to destination  2
move From 1  to destination  3
move From 2  to destination  3
move From 1  to destination  2
move From 3  to destination  1
move From 3  to destination  2
move From 1  to destination  2
7

Comments

1

You could make a function object instead of a function:

class Towers:

    def __init__(self):
        self.count = 0

    def __call__(self, n, source, destination, spare):
        if n == 1:
            self.printMove(source, destination)
            self.count +=1   
        else:
            self(n-1, source, spare, destination)
            self(1, source, destination, spare)
            self(n-1, spare, destination, source)

    def printMove(self, source, destination):
        print('move From ' + str(source) + ' to destination ' 
              + str(destination))
        print(self.count)

towers = Towers()
towers(3, 1, 2, 2)

Comments

1

The simplest way is to use a global var:

count = 0

def runTowers(...):
    global count
    count = 0
    Towers(...)

def Towers(...):
    global count
    count += 1
    ...

1 Comment

Yea. but then it will start the count from the previous value when I run it again. :)
0

The answer above should give you your answer :)

In terms of efficiency, it looks like your solution will work for n towers. If you wish to solve the classic problem more efficiently, look at this link:

http://www.vogella.com/articles/JavaAlgorithmsTowersOfHanoi/article.html

Finally, recursion is designed to make very simplistic algorithms that can perform complex calculations with basic code. The disadvantage is that it is memory intensive (each call places a new memory address on the stack of the last call).

Comments

-1

Without time pausing it, it will quickly reach the recursion limit

import time
def loop_forever(count):
    if count == 0:
        print(count)
        time.sleep(1)
        loop_forever(1)
    if count > 0:
        print(count)
        time.sleep(1)
        loop_forever(count + 1)


loop_forever(0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.