Quick actions

cmd+k|ctrl+k

Navigation

Languages

Recursive

Snippet info

Language

Python

Visibility

public

Author

thanhtu250590

Created

2016-07-24T17:49:45Z

Updated

2016-07-24T17:49:56Z

def fib(n):
    if n == 1 or n == 2:
        return n
    else:
        return fib( n - 1) + fib( n -2)

print(fib(6))

A = [3,2,1]
B = []
C = []

def move(n, source, target, auxiliary):
    if n > 0:
        # move n-1 disks from source to auxiliary, so they are out of the way  
        move(n-1, source, auxiliary, target)

        # move the nth disk from source to target
        if( len(source) > 0):
            target.append(source.pop())

        # Display our progress
        print(A, B, C, '##############', sep='\n')  
        
        # move the n-1 disks that we left on auxiliary onto target
        move(n-1, auxiliary, target, source)
# initiate call from source A to target C with auxiliary B 
move(len(A), A, C, B)
INFO