Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Algorithm Lab program, Lab Reports of Computer Science

Design AND ANALYSIS LAB PROGRAM

Typology: Lab Reports

2023/2024

Uploaded on 03/25/2024

varshini-anbalagan-1
varshini-anbalagan-1 🇮🇳

3 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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>=1
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
pf3
pf4

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