Fun Python Project. Recursion and the Towers of Hanoi

Поделиться
HTML-код
  • Опубликовано: 15 ноя 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.

  • @fbf3628
    @fbf3628 4 года назад

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

  • @the3rdmaster311
    @the3rdmaster311 4 года назад +1

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

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

  • @mustache2295
    @mustache2295 4 года назад +3

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

  • @KenJee_ds
    @KenJee_ds 4 года назад +6

    Love the new office!

  • @edgarduarte6926
    @edgarduarte6926 4 года назад +1

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

  • @ajithsdevadiga1603
    @ajithsdevadiga1603 4 года назад +4

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

  • @master_zenrade
    @master_zenrade 3 года назад

    Explained so good that I subscribed him.

  • @prem1prakash
    @prem1prakash 4 года назад +1

    Super. Just love the Tower of Hanoi example

  • @WanderWisdom731
    @WanderWisdom731 4 года назад +1

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

  • @brankohorvat5721
    @brankohorvat5721 6 месяцев назад

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

  • @burningpotato105
    @burningpotato105 3 года назад

    You explain really well

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

  • @syedrizvi2687
    @syedrizvi2687 3 года назад

    Loved your explanation

  • @petardenchev75
    @petardenchev75 8 месяцев назад

    thanks! Finally I get it! cheers

  • @tobiasgorgen7592
    @tobiasgorgen7592 4 года назад

    A new and exciting exercise from the Doctor.

  • @osamaishtiaq9894
    @osamaishtiaq9894 4 года назад

    Simply excellent explanation!

  • @teacherinthailan6441
    @teacherinthailan6441 3 года назад

    Cheers. Great teacher.

  • @heavydremer8716
    @heavydremer8716 3 года назад

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

  • @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)

  • @abhishekdey9990
    @abhishekdey9990 4 года назад

    Thank you Sir.. For such a nice explanation.

  • @adnanelouadghiri6880
    @adnanelouadghiri6880 8 месяцев назад

    Pretty cool!

  • @moez3617
    @moez3617 4 года назад

    amazing explanation ,thanks a lot

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

  • @kirandeepmarala5541
    @kirandeepmarala5541 4 года назад

    Mind Blowing!Thanks a Lot Sir../.

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

  • @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!

  • @johnnrandall
    @johnnrandall 4 года назад

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

  • @alecdelu83
    @alecdelu83 3 года назад

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

  • @koosi987
    @koosi987 4 года назад

    Helps alot !

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

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

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

  • @charg1nmalaz0r51
    @charg1nmalaz0r51 8 месяцев назад

    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?

  • @abishekaneese4034
    @abishekaneese4034 4 года назад

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

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

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

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

  • @gobyg-major2057
    @gobyg-major2057 3 года назад

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

  • @observer698
    @observer698 4 года назад +1

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

  • @europebasedvlogs1251
    @europebasedvlogs1251 4 года назад

    Did you just change the title?

  • @stefenleung
    @stefenleung 2 года назад

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

  • @NgocAnhNguyen-si5rq
    @NgocAnhNguyen-si5rq 4 года назад

  • @ganeshkumar269
    @ganeshkumar269 4 года назад

    I want to thank the fuck out of you.

  • @Praxss
    @Praxss 4 года назад

    My computer only run (1,21)

  • @qui3tstorm793
    @qui3tstorm793 4 года назад

    I didn't know they taught this at Hogwarts?

  • @Saif-G1
    @Saif-G1 15 дней назад +1

    I have a better way that can solve any tower

  • @sumitk3
    @sumitk3 3 года назад +1

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

  • @elieweiss1049
    @elieweiss1049 4 года назад

    He looks like Harry Potter

  • @lightofor8206
    @lightofor8206 7 месяцев назад

    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 RUclips 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!