It is two days I am trying to know TOH but I could not understand. After watching this nice video I could understand and now I know what TOH is ? and what recursion means really? Thank You so much with this great work and very very clear explanation.
Hey,i have already taken your python udemy course & it is awesome,i already knew python but for me your videos are helping me to write a more better code. Thank you.
I've been subscribed to your channel for quite a long time now and this is the first time I've seen you actually write any Python, and it was about 10 seconds worth. What am I missing?
Hi, Thanks for a great video. I am new to coding, Qualified with Codecademy (Computer Science course). I tried out your code but the line where you used : move(x, A, C, B)- shows move not defined. And it's not. What that (move) stands for?
Soham Patil People argue for either, my professor claims it’s like religion. Some people swear by iteration, others swear by recursion. You can survive using either exclusively.
1. It is an elegant and concise approach to solving certain problems that are naturally recursive like Binary search, merge sort.. 2. Most importantly, using recursions helps avoid the usage of an explicit stack data structure for solving problems like 8-Queens, tree traversal using DFS, or the simple string reversal problem. Had we used Iteration for solving the aforementioned problems, we would've ended up using an explicit stack DS.
Simple: because it is less code and easier to write; recursion is actually just mathematical induction and recurrence equations look almost the same in code. Downside: Recursion uses up more memory while it descends into the tree, so it is prone to cause stack overflows. Recursion is also slower because it has a constant factor that gets multiplied for every recursive call.
@@Garentei Although just to be clear for anyone that reads these comments, things like memory are more of a problem with Python than recursion as such. In that languages that better handle recursion have features such as Tail Call Optimisation that avoid allocating new stack frames, sadly Python lacks in this area.
@@sticklebrix1756 Like what languages? In C and C++ recursion causes these problems as well, sometimes forcing you to implement your own stack to simulate recursion.
Can someone clarify something for me, so programmatically this works but logically its not following how a human would switch the discs between pegs right? So its not shuffling things back and fourth. Its literally cheating in a sense but getting to the outcomes using recursion and the way the stack unwinds itself to move the stacks onto the spare peg in the n-1 step?
Thanks for this interesting video. There is another aspect to recursive functions using memoization which reduces the number of function calls made. On the other hand, I wanted to know if there are any limitations in the tower of Hanoi with respect to the number of poles and disks or is a solution always feasible?
I am having troubles :/ -> NameError: name 'move' is not defined, and when I changed it to hanoi as @Carl Neale (see comments) i get another error -> NameError: name 'run_moves' is not defined. I run exactly the same code as you run, why I am getting an error??
Yrok KoryThat’s not how it works, when you say hanoi(n-1) you don’t immediately move n-1 disks. Instead the function keeps on calling itself until you move one disk. I recommend watching the CS50 video on call stacks.
Hello, nice video thanxs. I was wondering about taking the "Paython for Everybody" from Dr, Chuk in Coursera. It would be ~200-250$ (50$/month) wich is a lot of money to me and dont want to "lose" it ... would you definitely recommend it ??( i am a total beginner in programming). I also thought about paying "coursera plus" which is 400$/year with unlimited courses, although Python oriented to data analysis is what i want to learn right know (i am an industrial engineer), what do you think about Coursera subscription plan?). By the other hand, would you recommend any coursera´s fullstack course so i can justify the "plus" subscription also taking it ? Regards!
The point of UA-cam is to find someone who makes videos that you can connect with and enjoy. Everyone can find a different way to present the same material and others may learn better through different presentation methods. Truly, every sort of entertainment or education is actually just some reinactment of something that has already been created if you think about it.
def hanoi(n, source, target, spare, source_label, target_label, spare_label): if n > 0: # Move n-1 disks from source to spare using target as auxiliary hanoi(n-1, source, spare, target, source_label, spare_label, target_label) # Move the nth disk from source to target print(f"Move disk {source[-1]} from {source_label} ({source}) to {target_label} ({target})") target.append(source.pop()) # Move the n-1 disks from spare to target using source as auxiliary hanoi(n-1, spare, target, source, spare_label, target_label, source_label) # Initial configuration A = [3, 2, 1] # Peg A (source) B = [] # Peg B (target) C = [] # Peg C (spare) # Start solving the problem hanoi(len(A), A, B, C, "A", "B", "C") print("Final state:") print(f"A: {A}") print(f"B: {B}") print(f"C: {C}")
I've done this exercises for my MIT computer science introduction course on EDX and was blown away by how simple the algorithm is with recursion.
Next step after watching this video is to watch it again 😂
Lmao, It took me a while to understand this.
Knowing it and implementing it as often as I should are two different things.
There's almost always a faster and more readable iterating version of any recursive algorithm. Recursion is rarely the right choice in practice.
I subscribed to your channel right after watching this video. With such extraordinary ability to explain, you deserve way more subscribers!
The explanation is so simple and easy to understand. It can be understood well if you if really understand recursion.
Love the new office!
best video on the net ever about recursion, thumbs highly up, sooo clear precise and easy to understand. Thanks Giles
Wow! I never understood how towers of hanoi work... until now. Thank You!
I really enjoyed learning about the Towers of Hanoi! Thanks for the video 😊
It is two days I am trying to know TOH but I could not understand. After watching this nice video I could understand and now I know what TOH is ? and what recursion means really? Thank You so much with this great work and very very clear explanation.
Hey,i have already taken your python udemy course & it is awesome,i already knew python but for me your videos are helping me to write a more better code.
Thank you.
Wow... Its dream come true ... such a wonderful explanation of recursion...
Great explanation. It's the best I've already found. Thanks a lot.
Super. Just love the Tower of Hanoi example
Explained so good that I subscribed him.
A new and exciting exercise from the Doctor.
Simply excellent explanation!
You explain really well
Loved your explanation
thnx for the explanation!!!
now it makes sense!!!!
If u could try MEMOIZATION (LRU cache) probably it will increase the speed of function.
thanks! Finally I get it! cheers
I've been subscribed to your channel for quite a long time now and this is the first time I've seen you actually write any Python, and it was about 10 seconds worth. What am I missing?
Cheers. Great teacher.
amazing explanation ,thanks a lot
Thank you Sir.. For such a nice explanation.
Hi,
Thanks for a great video.
I am new to coding, Qualified with Codecademy (Computer Science course).
I tried out your code but the line where you used : move(x, A, C, B)- shows move not defined. And it's not.
What that (move) stands for?
That line should be calling the hanoi function: hanoi(x, A, B, C)
thank you for this video! It's helping me with a class project I need to do. sub 4 sure
Pretty cool!
Mind Blowing!Thanks a Lot Sir../.
What are some reasons you would use recursion rather than just using loops?
Soham Patil People argue for either, my professor claims it’s like religion. Some people swear by iteration, others swear by recursion. You can survive using either exclusively.
1. It is an elegant and concise approach to solving certain problems that are naturally recursive like Binary search, merge sort..
2. Most importantly, using recursions helps avoid the usage of an explicit stack data structure for solving problems like 8-Queens, tree traversal using DFS, or the simple string reversal problem. Had we used Iteration for solving the aforementioned problems, we would've ended up using an explicit stack DS.
Simple: because it is less code and easier to write; recursion is actually just mathematical induction and recurrence equations look almost the same in code.
Downside: Recursion uses up more memory while it descends into the tree, so it is prone to cause stack overflows. Recursion is also slower because it has a constant factor that gets multiplied for every recursive call.
@@Garentei Although just to be clear for anyone that reads these comments, things like memory are more of a problem with Python than recursion as such. In that languages that better handle recursion have features such as Tail Call Optimisation that avoid allocating new stack frames, sadly Python lacks in this area.
@@sticklebrix1756 Like what languages? In C and C++ recursion causes these problems as well, sometimes forcing you to implement your own stack to simulate recursion.
Helps alot !
Seen on computerphile
Me too, but i found the explanation here better. I really like his approach!
Can someone clarify something for me, so programmatically this works but logically its not following how a human would switch the discs between pegs right? So its not shuffling things back and fourth. Its literally cheating in a sense but getting to the outcomes using recursion and the way the stack unwinds itself to move the stacks onto the spare peg in the n-1 step?
First time I watch your videos, I feel like I love you already (not gay)
lol
Very helpful explanation thanks, could we have one on how to solve a sudoku?
Thanks for this interesting video. There is another aspect to recursive functions using memoization which reduces the number of function calls made. On the other hand, I wanted to know if there are any limitations in the tower of Hanoi with respect to the number of poles and disks or is a solution always feasible?
I am having troubles :/ -> NameError: name 'move' is not defined, and when I changed it to hanoi as @Carl Neale (see comments) i get another error -> NameError: name 'run_moves' is not defined. I run exactly the same code as you run, why I am getting an error??
I just don't get why you're moving n-1 disks at the time in the code when you said that moving more than one disks at the time is an illegal move.
Yrok KoryThat’s not how it works, when you say hanoi(n-1) you don’t immediately move n-1 disks. Instead the function keeps on calling itself until you move one disk. I recommend watching the CS50 video on call stacks.
Hello, nice video thanxs. I was wondering about taking the "Paython for Everybody" from Dr, Chuk in Coursera. It would be ~200-250$ (50$/month) wich is a lot of money to me and dont want to "lose" it ... would you definitely recommend it ??( i am a total beginner in programming). I also thought about paying "coursera plus" which is 400$/year with unlimited courses, although Python oriented to data analysis is what i want to learn right know (i am an industrial engineer), what do you think about Coursera subscription plan?). By the other hand, would you recommend any coursera´s fullstack course so i can justify the "plus" subscription also taking it ? Regards!
Dr Chuck offers his class on py4e. Come for free. You will mist the coursera certification though
in one of your videos you talked about a FB group to make challenges in Python and AI. Will please sir again shre the link of the group.
thanks to you
My question is if completing that Task in Vietnam by the priests will result in the end of Earth then why the hell are they trying to complete it?
Walked in hoping to love recursion.... Walked out hating the words discs
Did you just change the title?
Actually it’s not a temple in Hanoi Vietnam, but a temple in India.....
In the middle of the video I thought his own code got broke, but it was all fine it was a normal glitch.
The code is wrong tho. You need to rename the "move" function at line 21 to "hanoi".
Can you please solve rubix cube, too? :) I need to learn it to teach it to my son :)
My computer only run (1,21)
I want to thank the fuck out of you.
I didn't know they taught this at Hogwarts?
Repeating same sentence again and again... he is literally into recursion
I have a better way that can solve any tower
He looks like Harry Potter
Dude is recursion himself 😅, he really repeated himself
title should be: redoing an video i have seen somewhere else
The point of UA-cam is to find someone who makes videos that you can connect with and enjoy. Everyone can find a different way to present the same material and others may learn better through different presentation methods. Truly, every sort of entertainment or education is actually just some reinactment of something that has already been created if you think about it.
def hanoi(n, source, target, spare, source_label, target_label, spare_label):
if n > 0:
# Move n-1 disks from source to spare using target as auxiliary
hanoi(n-1, source, spare, target, source_label, spare_label, target_label)
# Move the nth disk from source to target
print(f"Move disk {source[-1]} from {source_label} ({source}) to {target_label} ({target})")
target.append(source.pop())
# Move the n-1 disks from spare to target using source as auxiliary
hanoi(n-1, spare, target, source, spare_label, target_label, source_label)
# Initial configuration
A = [3, 2, 1] # Peg A (source)
B = [] # Peg B (target)
C = [] # Peg C (spare)
# Start solving the problem
hanoi(len(A), A, B, C, "A", "B", "C")
print("Final state:")
print(f"A: {A}")
print(f"B: {B}")
print(f"C: {C}")
Wow! I never understood how towers of hanoi work... until now. Thank You!