Docsity

Algorithm Lab program, Lab Reports of Computer Science

Partial preview of the text

Download Algorithm Lab program and more Lab Reports Computer Science in PDF only on Docsity!

Algorithm Lab Exp No 1 : Tower of Hanoi

Procedure:

Step 1: START

Step 2: TOH Parameter Lists(n,x,y,z)

Step 3: if n>=

Step 4: TOH(n-1,x,z,y)

Step 5: Move the top n disks from tower x to tower y

Step 6: TOH(n-1,z,y,x)

Step 7: END

def toh(n,x,y,z): create an function name if n>=1: move(x,z)  call the function move  in ordered to solve the function else: toh(n-1,x,z,y) move(x,z) toh(n-1,y,x,z) def move(x,y): print(“Move the top n disk from “, x,” tower x to tower y”, y) n=int(input(“Enter the number of disk”)) toh(n,”X”,”Y”,”Z”)


n = 3 # Number of disks source = 'A' temp = 'B' destination = 'C' stack = [(n, source, temp, destination)] while stack: n, source, temp, destination = stack.pop() if n == 1: print(f"Move disk 1 from {source} to {destination}") else: stack.append((n-1, temp, source, destination)) stack.append((1, source, temp, destination)) stack.append((n-1, source, destination, temp))

WORK TO BE DONE:

Palindrome for string Reverse Palindrome for prime number