educative.io

Educative

Can I have the solution for Challenge: Solve Hanoi recursively?

Can I have solution for “Challenge: Solve Hanoi recursively” in the Course A Visual Introduction to Algorithms ? I tried but I couldn’t make the correct solution. Thanks.


Course: A Visual Introduction to Algorithms - Free Interactive Course
Lesson: Challenge: Solve Hanoi recursively - A Visual Introduction to Algorithms

1 Like

@Thuc_Huynh any solution, I have a block with this challenge. thanks


Course: A Visual Introduction to Algorithms - Free Interactive Course
Lesson: Challenge: Solve Hanoi recursively - A Visual Introduction to Algorithms

Hi @Nicolas_Mena_Garzon and @Thuc_Huynh

This is a challenging task to check the concepts which you have covered in the lesson. So consider solving it by yourself.

Kind Regards.

What a stupid approach? I would get if free from Khan Academy. I should see the solutions if I am taking a publicly free course with a paid subscription.


Course: A Visual Introduction to Algorithms - Free Interactive Course
Lesson: https://www.educative.io/courses/visual-introduction-to-algorithms/k6qwr

Anyone who is interested in the solution. Please check the link https://www.cs.emory.edu/~cheung/Courses/170/Syllabus/13/hanoi.html for explanation and Java solution.

Here is the Python version

def solveHanoi(numDisks, fromPeg, toPeg):
	if numDisks == 1:
		return moveDisk(fromPeg, toPeg)

	sparePeg = getSparePeg(fromPeg, toPeg)
	solveHanoi(numDisks-1, fromPeg, sparePeg)
	moveDisk(fromPeg, toPeg)
	return solveHanoi(numDisks-1, sparePeg, toPeg)

Course: A Visual Introduction to Algorithms - Free Interactive Course
Lesson: https://www.educative.io/courses/visual-introduction-to-algorithms/k6qwr

1 Like