Recursion Simply Explained

  Рет қаралды 7,870

NeuralNine

NeuralNine

Күн бұрын

In this video we will cover recursion and break this pretty complex concept down in a simple and straight-forward way.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: www.neuralnine.com/books/
💻 The Algorithm Bible Book: www.neuralnine.com/books/
👕 Programming Merch: www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: www.neuralnine.com/
📷 Instagram: / neuralnine
🐦 Twitter: / neuralnine
🤵 LinkedIn: / neuralnine
📁 GitHub: github.com/NeuralNine
🎙 Discord: / discord
🎵 Outro Music From: www.bensound.com/
Timestamps:
(0:00) Intro
(0:55) Theoretical Basics
(5:43) Example - Counting Zeros
(14:40) Example - Finding Minimum
(18:47) Example - Fibonacci
(24:29) Setting Python Recursion Limit
(27:25) Outro

Пікірлер: 23
@marcuswest4572
@marcuswest4572 7 ай бұрын
The best recursion tutorial I have seen, by some margin.
@hemantrastogi7889
@hemantrastogi7889 2 жыл бұрын
Very insightful and on point
@mhashmi3593
@mhashmi3593 2 жыл бұрын
Keep up the good work. Nicely explained.
@ahmadfaisal6356
@ahmadfaisal6356 2 жыл бұрын
thank you, this is what i need
@wko_
@wko_ 2 жыл бұрын
Thks for the video. I always wanted to know more about it. 🇧🇷
@crazychicken0378
@crazychicken0378 2 жыл бұрын
Note: NeuralNine might try to teach this later, but for those who want to know why anyone would use recursion, here's why... Hey, as a functional programming lover, Fibonacci and other recursive problems like it don't need to be inefficient. The way that one can maintain efficiency is via tail recursion optimization (TRO) or whatever other hundred different names one can call it. With TRO, the function shouldn't hang on itself by building on the stack. Instead it's cleared because all the edge cases are actually put into the function itself maintaining a linear recursive process instead of a tree recursive process. We would define our function with these edge cases as so def fib(edge1, edge2, n): But Since we don't need to call our edge case with different edges each time, we can actually wrap it in a higher--order function which automatically wraps the edges in our call to it. def fib(n): def fib_wrapped(a, b, n): if n == 1: return edge1 else: newa = a + b newb = a newn = n - 1 return fib_wrapped(a, b, n) return fib_wrapped(1, 0, n) Now when you call this, there's very little inefficiency because the stack is constantly being cleared. In fact, because technically we're changing the state on each call to fib_wrapped, this, while still being recursive is more representative of an iteration and people generally refer to these "wrapped" functions as iter functions because they're doing the real work while the wrapper is just making it easier for the user to use. And notice for a little more usability we can ditch the instantiation of new variables, since we're just using basic arithmetic to calculate the next "iteration" and also have a ternary for better readability. def fib(n): def fib_iter(a,b,n): return a if n == 1 else fib_iter( (a+b), a, (n-1) ) return fib_iter(1, 0, n) Now we have something succinct and in a language where we can separate our parameters with more whitespace(not sure if I can in python) between the parentheses it wouldn't look like clutter but even more beautiful in my opinion. And especially with things like factorial where you might have a basic algorithm but then some random mathematician finds even better edge cases, once you start converting math to code, you can make this recursive process even more (O(n)) fast. This is how functional programming with languages like Haskell still maintain speed with levels of even C or C++ NOTE: you will still need to change the recursion limit because in the python interpreter, no matter how much you've optimized. It's going to think it's kind of like an infinite loop despite the fact that you want a real number and it's not hanging up like a regular recursive process.
@SVSingam273
@SVSingam273 2 жыл бұрын
@NeuralNine. Please make videos where you are discussing Algorithms and Data Structures for technical interviews in Python. Btw, love the content you publish.
@DENZELLLLLL
@DENZELLLLLL 2 жыл бұрын
thanks it's crystal clear! even tho I don't get how it's summing the return values after each call (in recursive n°1)
@gge6021
@gge6021 2 жыл бұрын
thoose are all quite simple recursions im working on a problems with intense tree structures and recursion and i feel completly lost xd
@muhdmairaj8778
@muhdmairaj8778 2 жыл бұрын
Hey, for the find minimum example, why not just do: def find_min(lst): return min(lst) I mean there is a built is function to find minimum of an iterable, and we use it in the recursive example... But why not just use it directly like above?
@phsopher
@phsopher 2 жыл бұрын
And for the first example you could use the inbuilt list method count() : mylist.count(0). The point is not to teach the best way to solve these specific simple problems, but to use them to illustrate a programming concept, such as recursion. We are not using the function for a minimum of an iterable in the recursive example. We are using the function min(x,y) to compare two integers. It so happens that this function is also implemented for iterables but we are not using that functionality.
@muhdmairaj8778
@muhdmairaj8778 2 жыл бұрын
@@phsopher alright that makes sense. I just thought it was strange to use `min` like that cuz you could just so the whole thing... like if youre using it might as well use it fully. Maybe you could show the example by using if lst[0] > lst[1]: ... else: ... Or smt like that
@saivivek5563
@saivivek5563 2 жыл бұрын
Plz help me bro how to extract information from pdf with table to csv
@FilipCodes
@FilipCodes 2 жыл бұрын
"Recursions are awesome!" - said no one
@ahmedgamberli2250
@ahmedgamberli2250 2 жыл бұрын
BECAUSE THEY AREN'T
@gameofpj3286
@gameofpj3286 2 жыл бұрын
I'd say that
@kennedywaweru
@kennedywaweru 2 жыл бұрын
Hi @NeuralNine, I enjoy learning from your videos. In the second example where you find the minimum number in a list, it would be more elegant to use ternary operators as you did in the first example instead of using the min() function. Here's how I did it: def find_min(lst): min = lst[0] if len(lst) == 1: min = lst[0] if lst[0] < min else min return min min = find_min(lst[1:]) if find_min(lst[1:]) < min else min return min
@ahmedgamberli2250
@ahmedgamberli2250 2 жыл бұрын
First comment
@CubeRiser
@CubeRiser 2 жыл бұрын
ok
@christiankoch4627
@christiankoch4627 2 жыл бұрын
I hate recursion
@zombiekiller7101
@zombiekiller7101 2 жыл бұрын
Smol brain
@phsopher
@phsopher 2 жыл бұрын
I hate I hate recursion
@yusiferzendric1489
@yusiferzendric1489 2 жыл бұрын
@@phsopher so deep
What is The Walrus Operator in Python?
12:45
NeuralNine
Рет қаралды 14 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
Gym belt !! 😂😂  @kauermotta
00:10
Tibo InShape
Рет қаралды 18 МЛН
MISS CIRCLE STUDENTS BULLY ME!
00:12
Andreas Eskander
Рет қаралды 20 МЛН
Data Classes in Python Are The New Standard
20:34
NeuralNine
Рет қаралды 66 М.
This is how I recommend you learn Recursion in Javascript
24:39
Web Dev Cody
Рет қаралды 32 М.
What is a Monad? - Computerphile
21:50
Computerphile
Рет қаралды 598 М.
Ranking Programming Languages on Tiermaker
18:38
NeuralNine
Рет қаралды 15 М.
Recursion Simply Explained with Code Examples - Python for Beginners
9:07
Python Simplified
Рет қаралды 161 М.
What is Python's Main Function Useful For?
8:08
NeuralNine
Рет қаралды 75 М.
TCP vs UDP Sockets in Python
17:43
NeuralNine
Рет қаралды 30 М.
Recursion 'Super Power' (in Python) - Computerphile
12:18
Computerphile
Рет қаралды 489 М.
Recursion (3 hours course) - Inside code
3:03:45
Inside code
Рет қаралды 20 М.
Recursion for Beginners: A Beginner's Guide to Recursion
27:25
North Bay Python
Рет қаралды 106 М.
Look, this is the 97th generation of the phone?
0:13
Edcers
Рет қаралды 8 МЛН
Bluetooth connected successfully 💯💯
0:16
Blue ice Comedy
Рет қаралды 1,7 МЛН