Fun Python Project. Recursion and the Towers of Hanoi

Поділитися
Вставка
  • Опубліковано 26 лис 2024

КОМЕНТАРІ • 79

  • @aydinjalilov2328
    @aydinjalilov2328 4 роки тому +13

    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.

  • @jayant9151
    @jayant9151 4 роки тому +80

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

    • @kauuu4137
      @kauuu4137 4 роки тому

      Lmao, It took me a while to understand this.

  • @brodiewells
    @brodiewells 4 роки тому +24

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

    • @Bozemoto
      @Bozemoto 4 роки тому +2

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

  • @kevinjackson976
    @kevinjackson976 4 роки тому +7

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

  • @VarunVishwakarma1
    @VarunVishwakarma1 3 роки тому

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

  • @KenJee_ds
    @KenJee_ds 4 роки тому +6

    Love the new office!

  • @the3rdmaster311
    @the3rdmaster311 4 роки тому +1

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

  • @fbf3628
    @fbf3628 4 роки тому

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

  • @mustache2295
    @mustache2295 4 роки тому +3

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

  • @haroonsateerrahimi5229
    @haroonsateerrahimi5229 4 роки тому

    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.

  • @vikramvikrant5851
    @vikramvikrant5851 4 роки тому +1

    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.

  • @WanderWisdom731
    @WanderWisdom731 4 роки тому +1

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

  • @edgarduarte6926
    @edgarduarte6926 4 роки тому +1

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

  • @prem1prakash
    @prem1prakash 4 роки тому +1

    Super. Just love the Tower of Hanoi example

  • @master_zenrade
    @master_zenrade 3 роки тому

    Explained so good that I subscribed him.

  • @tobiasgorgen7592
    @tobiasgorgen7592 4 роки тому

    A new and exciting exercise from the Doctor.

  • @osamaishtiaq9894
    @osamaishtiaq9894 4 роки тому

    Simply excellent explanation!

  • @burningpotato105
    @burningpotato105 3 роки тому

    You explain really well

  • @syedrizvi2687
    @syedrizvi2687 3 роки тому

    Loved your explanation

  • @brankohorvat5721
    @brankohorvat5721 7 місяців тому

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

  • @ajithsdevadiga1603
    @ajithsdevadiga1603 4 роки тому +4

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

  • @petardenchev75
    @petardenchev75 9 місяців тому

    thanks! Finally I get it! cheers

  • @ColinBroderickMaths
    @ColinBroderickMaths 4 роки тому +8

    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?

  • @teacherinthailan6441
    @teacherinthailan6441 3 роки тому

    Cheers. Great teacher.

  • @moez3617
    @moez3617 4 роки тому

    amazing explanation ,thanks a lot

  • @abhishekdey9990
    @abhishekdey9990 4 роки тому

    Thank you Sir.. For such a nice explanation.

  • @jnstrategypersonalcoach5320
    @jnstrategypersonalcoach5320 4 роки тому +2

    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 2 роки тому

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

  • @heavydremer8716
    @heavydremer8716 3 роки тому

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

  • @adnanelouadghiri6880
    @adnanelouadghiri6880 8 місяців тому

    Pretty cool!

  • @kirandeepmarala5541
    @kirandeepmarala5541 4 роки тому

    Mind Blowing!Thanks a Lot Sir../.

  • @sohampatil6539
    @sohampatil6539 4 роки тому +5

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

    • @Fusionade
      @Fusionade 4 роки тому +1

      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 роки тому +2

      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.

  • @koosi987
    @koosi987 4 роки тому

    Helps alot !

  • @theraytech54
    @theraytech54 4 роки тому +6

    Seen on computerphile

    • @sergiomarinhodasilva4781
      @sergiomarinhodasilva4781 4 роки тому +3

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

  • @charg1nmalaz0r51
    @charg1nmalaz0r51 9 місяців тому

    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?

  • @alecdelu83
    @alecdelu83 3 роки тому

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

  • @johnnrandall
    @johnnrandall 4 роки тому

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

  • @agarwalamit081
    @agarwalamit081 4 роки тому

    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?

  • @stragalaki
    @stragalaki 4 роки тому

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

  • @KoryGraphic
    @KoryGraphic 4 роки тому +2

    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 4 роки тому +1

      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.

  • @ignaciol5748
    @ignaciol5748 4 роки тому +1

    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 4 роки тому

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

  • @ahmed.bhewary775
    @ahmed.bhewary775 4 роки тому

    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

  • @RahulRoy-qy8rk
    @RahulRoy-qy8rk 4 роки тому +1

    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?

  • @abishekaneese4034
    @abishekaneese4034 4 роки тому

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

  • @europebasedvlogs1251
    @europebasedvlogs1251 4 роки тому

    Did you just change the title?

  • @gobyg-major2057
    @gobyg-major2057 3 роки тому

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

  • @ChitranjanBaghiofficial
    @ChitranjanBaghiofficial 4 роки тому

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

  • @stefenleung
    @stefenleung 2 роки тому

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

  • @observer698
    @observer698 4 роки тому +1

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

  • @NgocAnhNguyen-si5rq
    @NgocAnhNguyen-si5rq 4 роки тому

  • @Praxss
    @Praxss 4 роки тому

    My computer only run (1,21)

  • @ganeshkumar269
    @ganeshkumar269 4 роки тому

    I want to thank the fuck out of you.

  • @qui3tstorm793
    @qui3tstorm793 4 роки тому

    I didn't know they taught this at Hogwarts?

  • @sumitk3
    @sumitk3 3 роки тому +1

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

  • @Saif-G1
    @Saif-G1 26 днів тому +1

    I have a better way that can solve any tower

  • @elieweiss1049
    @elieweiss1049 4 роки тому

    He looks like Harry Potter

  • @lightofor8206
    @lightofor8206 8 місяців тому

    Dude is recursion himself 😅, he really repeated himself

  • @fr3ud_4137
    @fr3ud_4137 4 роки тому +1

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

    • @mustache2295
      @mustache2295 4 роки тому +2

      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.

  • @minneso5424
    @minneso5424 3 місяці тому

    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}")

  • @fbf3628
    @fbf3628 4 роки тому

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