Fun Python Project. Recursion and the Towers of Hanoi

  Рет қаралды 31,038

Python Programmer

Python Programmer

Күн бұрын

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
@jayant9151 4 жыл бұрын
Next step after watching this video is to watch it again 😂
@kauuu4137
@kauuu4137 4 жыл бұрын
Lmao, It took me a while to understand this.
@aydinjalilov2328
@aydinjalilov2328 4 жыл бұрын
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
@brodiewells 4 жыл бұрын
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
@kevinjackson976 4 жыл бұрын
I subscribed to your channel right after watching this video. With such extraordinary ability to explain, you deserve way more subscribers!
@the3rdmaster311
@the3rdmaster311 4 жыл бұрын
best video on the net ever about recursion, thumbs highly up, sooo clear precise and easy to understand. Thanks Giles
@mustache2295
@mustache2295 4 жыл бұрын
I really enjoyed learning about the Towers of Hanoi! Thanks for the video 😊
@fbf3628
@fbf3628 3 жыл бұрын
Wow! I never understood how towers of hanoi work... until now. Thank You!
@edgarduarte6926
@edgarduarte6926 3 жыл бұрын
Great explanation. It's the best I've already found. Thanks a lot.
@WanderWisdom731
@WanderWisdom731 4 жыл бұрын
Wow... Its dream come true ... such a wonderful explanation of recursion...
@prem1prakash
@prem1prakash 4 жыл бұрын
Super. Just love the Tower of Hanoi example
@vikramvikrant5851
@vikramvikrant5851 4 жыл бұрын
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.
@KenJee_ds
@KenJee_ds 4 жыл бұрын
Love the new office!
@tobiasgorgen7592
@tobiasgorgen7592 4 жыл бұрын
A new and exciting exercise from the Doctor.
@varunvishwakarma2901
@varunvishwakarma2901 3 жыл бұрын
The explanation is so simple and easy to understand. It can be understood well if you if really understand recursion.
@osamaishtiaq9894
@osamaishtiaq9894 3 жыл бұрын
Simply excellent explanation!
@kirandeepmarala5541
@kirandeepmarala5541 4 жыл бұрын
Mind Blowing!Thanks a Lot Sir../.
@haroonsateerrahimi5229
@haroonsateerrahimi5229 3 жыл бұрын
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.
@master_zenrade
@master_zenrade 3 жыл бұрын
Explained so good that I subscribed him.
@abhishekdey9990
@abhishekdey9990 4 жыл бұрын
Thank you Sir.. For such a nice explanation.
@syedrizvi2687
@syedrizvi2687 2 жыл бұрын
Loved your explanation
@moez3617
@moez3617 3 жыл бұрын
amazing explanation ,thanks a lot
@petardenchev75
@petardenchev75 3 ай бұрын
thanks! Finally I get it! cheers
@brankohorvat5721
@brankohorvat5721 27 күн бұрын
thnx for the explanation!!! now it makes sense!!!!
@ColinBroderickMaths
@ColinBroderickMaths 4 жыл бұрын
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?
@burningpotato105
@burningpotato105 3 жыл бұрын
You explain really well
@ajithsdevadiga1603
@ajithsdevadiga1603 4 жыл бұрын
If u could try MEMOIZATION (LRU cache) probably it will increase the speed of function.
@koosi987
@koosi987 3 жыл бұрын
Helps alot !
@teacherinthailan6441
@teacherinthailan6441 2 жыл бұрын
Cheers. Great teacher.
@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?
@adnanelouadghiri6880
@adnanelouadghiri6880 2 ай бұрын
Pretty cool!
@heavydremer8716
@heavydremer8716 2 жыл бұрын
thank you for this video! It's helping me with a class project I need to do. sub 4 sure
@johnnrandall
@johnnrandall 4 жыл бұрын
Very helpful explanation thanks, could we have one on how to solve a sudoku?
@69niggsy
@69niggsy 4 жыл бұрын
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
@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
@jnstrategypersonalcoach5320
@jnstrategypersonalcoach5320 3 жыл бұрын
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)
@sohampatil6539
@sohampatil6539 4 жыл бұрын
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
@theraytech54 4 жыл бұрын
Seen on computerphile
@sergiomarinhodasilva4781
@sergiomarinhodasilva4781 4 жыл бұрын
Me too, but i found the explanation here better. I really like his approach!
@NgocAnhNguyen-si5rq
@NgocAnhNguyen-si5rq 4 жыл бұрын
@ignaciol5748
@ignaciol5748 4 жыл бұрын
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
@charg1nmalaz0r51
@charg1nmalaz0r51 2 ай бұрын
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?
@RahulRoy-qy8rk
@RahulRoy-qy8rk 3 жыл бұрын
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?
@KoryGraphic
@KoryGraphic 4 жыл бұрын
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.
@stragalaki
@stragalaki 3 жыл бұрын
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??
@alecdelu83
@alecdelu83 3 жыл бұрын
First time I watch your videos, I feel like I love you already (not gay)
@Joe-jt2co
@Joe-jt2co 3 ай бұрын
lol
@observer698
@observer698 4 жыл бұрын
Can you please solve rubix cube, too? :) I need to learn it to teach it to my son :)
@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.
@abishekaneese4034
@abishekaneese4034 4 жыл бұрын
Walked in hoping to love recursion.... Walked out hating the words discs
@gobyg-major2057
@gobyg-major2057 3 жыл бұрын
Actually it’s not a temple in Hanoi Vietnam, but a temple in India.....
@ganeshkumar269
@ganeshkumar269 4 жыл бұрын
I want to thank the fuck out of you.
@qui3tstorm793
@qui3tstorm793 4 жыл бұрын
I didn't know they taught this at Hogwarts?
@stefenleung
@stefenleung Жыл бұрын
The code is wrong tho. You need to rename the "move" function at line 21 to "hanoi".
@europebasedvlogs1251
@europebasedvlogs1251 3 жыл бұрын
Did you just change the title?
@Praxss
@Praxss 3 жыл бұрын
My computer only run (1,21)
@elieweiss1049
@elieweiss1049 3 жыл бұрын
He looks like Harry Potter
@joegoosebass3958
@joegoosebass3958 4 жыл бұрын
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
@sumitk3
@sumitk3 3 жыл бұрын
Repeating same sentence again and again... he is literally into recursion
@lightofor8206
@lightofor8206 Ай бұрын
Dude is recursion himself 😅, he really repeated himself
@fr3ud_4137
@fr3ud_4137 4 жыл бұрын
title should be: redoing an video i have seen somewhere else
@mustache2295
@mustache2295 4 жыл бұрын
The point of KZbin 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
@fbf3628 3 жыл бұрын
Wow! I never understood how towers of hanoi work... until now. Thank You!
Recursion 'Super Power' (in Python) - Computerphile
12:18
Computerphile
Рет қаралды 487 М.
The ultimate tower of Hanoi algorithm
39:23
Mathologer
Рет қаралды 222 М.
¡Puaj! No comas piruleta sucia, usa un gadget 😱 #herramienta
00:30
JOON Spanish
Рет қаралды 22 МЛН
Glow Stick Secret 😱 #shorts
00:37
Mr DegrEE
Рет қаралды 139 МЛН
Normal vs Smokers !! 😱😱😱
00:12
Tibo InShape
Рет қаралды 107 МЛН
Key to the Tower of Hanoi - Numberphile
14:07
Numberphile
Рет қаралды 423 М.
The Tower of Hanoi (Recursive Formula and Proof by Induction)
9:49
Florian Ludewig
Рет қаралды 37 М.
I've Read Over 100 Books on Python. Here are the Top 3
9:26
Python Programmer
Рет қаралды 266 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
How I would learn to code (if starting again)
7:20
Python Programmer
Рет қаралды 74 М.
Have you read these FANTASTIC PYTHON BOOKS? LEARN PYTHON!
7:15
Python Programmer
Рет қаралды 90 М.
Towers of Hanoi: A Complete Recursive Visualization
21:13
Reducible
Рет қаралды 436 М.
How to solve tower of Hanoi - Inside code
8:13
Inside code
Рет қаралды 8 М.
6 Steps to Expertise  in ANYTHING - No Talent Required!
8:15
Python Programmer
Рет қаралды 336 М.
БОМБОЧКИ!!! БАХ- БА-БАХ
0:42
ЧЕСТНЫЙ ALIEXPRESS
Рет қаралды 5 МЛН
Последний Закат Кота Макса...
0:21
Глеб Рандалайнен
Рет қаралды 8 МЛН
КАК ЖЕ ОБОЙТИ СИСТЕМУ???😏
0:44
Чистая Линия
Рет қаралды 9 МЛН
My Girlfriend WILL HATE ME for THIS 😂 #shorts
0:25
WigoFellas
Рет қаралды 12 МЛН
Кто за дверью?🤯
0:29
Бутылочка
Рет қаралды 3,1 МЛН