Recursion Simply Explained

  Рет қаралды 8,179

NeuralNine

NeuralNine

Күн бұрын

Пікірлер: 23
@marcuswest4572
@marcuswest4572 11 ай бұрын
The best recursion tutorial I have seen, by some margin.
@crazychicken0378
@crazychicken0378 3 жыл бұрын
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.
@mhashmi3593
@mhashmi3593 3 жыл бұрын
Keep up the good work. Nicely explained.
@hemantrastogi7889
@hemantrastogi7889 3 жыл бұрын
Very insightful and on point
@wko_
@wko_ 3 жыл бұрын
Thks for the video. I always wanted to know more about it. 🇧🇷
@ahmadfaisal6356
@ahmadfaisal6356 3 жыл бұрын
thank you, this is what i need
@DENZELLLLLL
@DENZELLLLLL 3 жыл бұрын
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)
@SVSingam273
@SVSingam273 3 жыл бұрын
@NeuralNine. Please make videos where you are discussing Algorithms and Data Structures for technical interviews in Python. Btw, love the content you publish.
@gge6021
@gge6021 3 жыл бұрын
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 3 жыл бұрын
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 3 жыл бұрын
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 3 жыл бұрын
@@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 3 жыл бұрын
Plz help me bro how to extract information from pdf with table to csv
@FilipCodes
@FilipCodes 3 жыл бұрын
"Recursions are awesome!" - said no one
@ahmedgamberli2250
@ahmedgamberli2250 3 жыл бұрын
BECAUSE THEY AREN'T
@gameofpj3286
@gameofpj3286 3 жыл бұрын
I'd say that
@kennedywaweru
@kennedywaweru 3 жыл бұрын
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 3 жыл бұрын
First comment
@CubeRiser
@CubeRiser 3 жыл бұрын
ok
@christiankoch4627
@christiankoch4627 3 жыл бұрын
I hate recursion
@zombiekiller7101
@zombiekiller7101 3 жыл бұрын
Smol brain
@phsopher
@phsopher 3 жыл бұрын
I hate I hate recursion
@yusiferzendric1489
@yusiferzendric1489 3 жыл бұрын
@@phsopher so deep
Recursion for Python Beginners with Recursive Function Examples
17:54
Recursion 'Super Power' (in Python) - Computerphile
12:18
Computerphile
Рет қаралды 491 М.
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 7 МЛН
А я думаю что за звук такой знакомый? 😂😂😂
00:15
Денис Кукояка
Рет қаралды 6 МЛН
How many people are in the changing room? #devil #lilith #funny #shorts
00:39
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
What is The Walrus Operator in Python?
12:45
NeuralNine
Рет қаралды 14 М.
Python's permutations function (deep dive & implementation)
19:13
Python Sockets Simply Explained
39:33
NeuralNine
Рет қаралды 167 М.
What Is Recursion - In Depth
13:25
Web Dev Simplified
Рет қаралды 158 М.
JavaScript Algorithms - 12 - Recursive Fibonacci Sequence
8:44
Codevolution
Рет қаралды 51 М.
Binary Search in Python
16:54
NeuralNine
Рет қаралды 10 М.
Programming Problem #9 - Tower of Hanoi
15:35
Tech With Tim
Рет қаралды 17 М.
How to Understand Any Recursive Code
16:53
Byte by Byte
Рет қаралды 151 М.
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 7 МЛН