Fun Python Project. Recursion and the Towers of Hanoi

Ойын-сауық

This is a complete explanation of recursion. Recursion is a very useful tool in computer science and data science. Here I show you what recursion is and how to use recursion to solve the towers of hanoi problem using python. I also use recursion to calculate factorial.
👌 WANT TO LEARN PYTHON? Try my courses:
bit.ly/2JuehGa
👌 Want to ace the Data Science Interview? Over 1000 Data Science Practice Questions with model solutions: bit.ly/30ul0nX
👌 SUBSCRIBE 👌
/ @gilesmcmullen
/ gilesmcmullen

Пікірлер: 78

  • @jayant9151
    @jayant91514 жыл бұрын

    Next step after watching this video is to watch it again 😂

  • @kauuu4137

    @kauuu4137

    4 жыл бұрын

    Lmao, It took me a while to understand this.

  • @aydinjalilov2328
    @aydinjalilov23284 жыл бұрын

    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.

  • @brodiewells
    @brodiewells4 жыл бұрын

    Knowing it and implementing it as often as I should are two different things.

  • @Bozemoto

    @Bozemoto

    4 жыл бұрын

    There's almost always a faster and more readable iterating version of any recursive algorithm. Recursion is rarely the right choice in practice.

  • @kevinjackson976
    @kevinjackson9764 жыл бұрын

    I subscribed to your channel right after watching this video. With such extraordinary ability to explain, you deserve way more subscribers!

  • @the3rdmaster311
    @the3rdmaster3114 жыл бұрын

    best video on the net ever about recursion, thumbs highly up, sooo clear precise and easy to understand. Thanks Giles

  • @mustache2295
    @mustache22954 жыл бұрын

    I really enjoyed learning about the Towers of Hanoi! Thanks for the video 😊

  • @fbf3628
    @fbf36283 жыл бұрын

    Wow! I never understood how towers of hanoi work... until now. Thank You!

  • @KenJee_ds
    @KenJee_ds4 жыл бұрын

    Love the new office!

  • @prem1prakash
    @prem1prakash4 жыл бұрын

    Super. Just love the Tower of Hanoi example

  • @edgarduarte6926
    @edgarduarte69263 жыл бұрын

    Great explanation. It's the best I've already found. Thanks a lot.

  • @WanderWisdom731
    @WanderWisdom7314 жыл бұрын

    Wow... Its dream come true ... such a wonderful explanation of recursion...

  • @varunvishwakarma2901
    @varunvishwakarma29013 жыл бұрын

    The explanation is so simple and easy to understand. It can be understood well if you if really understand recursion.

  • @tobiasgorgen7592
    @tobiasgorgen75924 жыл бұрын

    A new and exciting exercise from the Doctor.

  • @vikramvikrant5851
    @vikramvikrant58514 жыл бұрын

    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.

  • @osamaishtiaq9894
    @osamaishtiaq98943 жыл бұрын

    Simply excellent explanation!

  • @master_zenrade
    @master_zenrade3 жыл бұрын

    Explained so good that I subscribed him.

  • @syedrizvi2687
    @syedrizvi26872 жыл бұрын

    Loved your explanation

  • @burningpotato105
    @burningpotato1053 жыл бұрын

    You explain really well

  • @petardenchev75
    @petardenchev753 ай бұрын

    thanks! Finally I get it! cheers

  • @kirandeepmarala5541
    @kirandeepmarala55414 жыл бұрын

    Mind Blowing!Thanks a Lot Sir../.

  • @brankohorvat5721
    @brankohorvat5721Ай бұрын

    thnx for the explanation!!! now it makes sense!!!!

  • @haroonsateerrahimi5229
    @haroonsateerrahimi52293 жыл бұрын

    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.

  • @ajithsdevadiga1603
    @ajithsdevadiga16034 жыл бұрын

    If u could try MEMOIZATION (LRU cache) probably it will increase the speed of function.

  • @teacherinthailan6441
    @teacherinthailan64412 жыл бұрын

    Cheers. Great teacher.

  • @abhishekdey9990
    @abhishekdey99904 жыл бұрын

    Thank you Sir.. For such a nice explanation.

  • @moez3617
    @moez36173 жыл бұрын

    amazing explanation ,thanks a lot

  • @koosi987
    @koosi9873 жыл бұрын

    Helps alot !

  • @heavydremer8716
    @heavydremer87162 жыл бұрын

    thank you for this video! It's helping me with a class project I need to do. sub 4 sure

  • @adnanelouadghiri6880
    @adnanelouadghiri68802 ай бұрын

    Pretty cool!

  • @agarwalamit081
    @agarwalamit0814 жыл бұрын

    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?

  • @ColinBroderickMaths
    @ColinBroderickMaths4 жыл бұрын

    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?

  • @johnnrandall
    @johnnrandall4 жыл бұрын

    Very helpful explanation thanks, could we have one on how to solve a sudoku?

  • @jnstrategypersonalcoach5320
    @jnstrategypersonalcoach53203 жыл бұрын

    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?

  • @toby709

    @toby709

    Жыл бұрын

    That line should be calling the hanoi function: hanoi(x, A, B, C)

  • @69niggsy
    @69niggsy4 жыл бұрын

    Hello Giles. Great video but I think there is a mistake in the code you showed. move(x, A, C, B) should read hanoi(x, A, C, B)? Keep up the great work!!

  • @gilesmcmullen

    @gilesmcmullen

    4 жыл бұрын

    Oh no! I'll double check! Thanks for flagging it.

  • @jccorman5848

    @jccorman5848

    3 жыл бұрын

    Thanks. You solved my problem

  • @teacherinthailan6441

    @teacherinthailan6441

    2 жыл бұрын

    Yeah. That stumped me too. Thanks. (Yes, I cheated...)lol

  • @ignaciol5748
    @ignaciol57484 жыл бұрын

    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!

  • @floriantochie9253

    @floriantochie9253

    3 жыл бұрын

    Dr Chuck offers his class on py4e. Come for free. You will mist the coursera certification though

  • @sohampatil6539
    @sohampatil65394 жыл бұрын

    What are some reasons you would use recursion rather than just using loops?

  • @Fusionade

    @Fusionade

    4 жыл бұрын

    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.

  • @sridharg3043

    @sridharg3043

    4 жыл бұрын

    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.

  • @Garentei

    @Garentei

    4 жыл бұрын

    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.

  • @sticklebrix1756

    @sticklebrix1756

    4 жыл бұрын

    @@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.

  • @Garentei

    @Garentei

    4 жыл бұрын

    @@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.

  • @theraytech54
    @theraytech544 жыл бұрын

    Seen on computerphile

  • @sergiomarinhodasilva4781

    @sergiomarinhodasilva4781

    4 жыл бұрын

    Me too, but i found the explanation here better. I really like his approach!

  • @charg1nmalaz0r51
    @charg1nmalaz0r513 ай бұрын

    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?

  • @ahmed.bhewary775
    @ahmed.bhewary7754 жыл бұрын

    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

  • @stragalaki
    @stragalaki3 жыл бұрын

    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??

  • @NgocAnhNguyen-si5rq
    @NgocAnhNguyen-si5rq4 жыл бұрын

  • @alecdelu83
    @alecdelu833 жыл бұрын

    First time I watch your videos, I feel like I love you already (not gay)

  • @Joe-jt2co

    @Joe-jt2co

    3 ай бұрын

    lol

  • @abishekaneese4034
    @abishekaneese40344 жыл бұрын

    Walked in hoping to love recursion.... Walked out hating the words discs

  • @KoryGraphic
    @KoryGraphic4 жыл бұрын

    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.

  • @anuragsuresh5867

    @anuragsuresh5867

    3 жыл бұрын

    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.

  • @RahulRoy-qy8rk
    @RahulRoy-qy8rk3 жыл бұрын

    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?

  • @ChitranjanBaghiofficial
    @ChitranjanBaghiofficial4 жыл бұрын

    In the middle of the video I thought his own code got broke, but it was all fine it was a normal glitch.

  • @observer698
    @observer6984 жыл бұрын

    Can you please solve rubix cube, too? :) I need to learn it to teach it to my son :)

  • @gobyg-major2057
    @gobyg-major20573 жыл бұрын

    Actually it’s not a temple in Hanoi Vietnam, but a temple in India.....

  • @europebasedvlogs1251
    @europebasedvlogs12513 жыл бұрын

    Did you just change the title?

  • @ganeshkumar269
    @ganeshkumar2694 жыл бұрын

    I want to thank the fuck out of you.

  • @stefenleung
    @stefenleung2 жыл бұрын

    The code is wrong tho. You need to rename the "move" function at line 21 to "hanoi".

  • @qui3tstorm793
    @qui3tstorm7934 жыл бұрын

    I didn't know they taught this at Hogwarts?

  • @Praxss
    @Praxss3 жыл бұрын

    My computer only run (1,21)

  • @elieweiss1049
    @elieweiss10493 жыл бұрын

    He looks like Harry Potter

  • @sumitk3
    @sumitk33 жыл бұрын

    Repeating same sentence again and again... he is literally into recursion

  • @joegoosebass3958
    @joegoosebass39584 жыл бұрын

    No one is going to get recursion if they are a data scientist.. leave subjects like these to web developers / low level library maintainers / OS devs etc

  • @lightofor8206
    @lightofor82062 ай бұрын

    Dude is recursion himself 😅, he really repeated himself

  • @fr3ud_4137
    @fr3ud_41374 жыл бұрын

    title should be: redoing an video i have seen somewhere else

  • @mustache2295

    @mustache2295

    4 жыл бұрын

    The point of KZread 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.

  • @fbf3628
    @fbf36283 жыл бұрын

    Wow! I never understood how towers of hanoi work... until now. Thank You!

Келесі