Python Loop-De-Loop Function!?? #python #coding #programming

preview_player
Показать описание
This video show a short Python example of recursion

Background Music:
Creative Commons Attribution 3.0 Unported License
Рекомендации по теме
Комментарии
Автор

Finally, an O(n) algorithm for finding 0

qtq
Автор

...First rule of using recursion: Avoid using recusrion if it is possible and while function call costs more time than basic algorythm step increment

dodikod
Автор

That "if x == 0" kinda hurts my eyes, because any negative number would create an infinite loop.
"if x <= 0" would be better in my opinion.

raydorepp
Автор

You do the Loop-de-loop,
Return,
Now your recursion's looking cool!

mulchiner
Автор

Oh THAT'S how recursion works? Geez that was way too complicated in my head.

Thanks dude, major help!

abetterfuture
Автор

Python has ternaries too, return 0 if x < 1 else f(x-1)

jollyjoker
Автор

Now I finally understand what does 'return' do

voxelHD
Автор

The output is {RecursionError} if you call it as 'a = foo(1000)' :)

MrAlexg
Автор

I always expect these to be a trick question, i know how the code should work but i immediately start doubting myself

the_th_sun
Автор

Got it. Your videos are great and super helpful by the way!

johnroddy
Автор

If the purpose of the func is to get any integer input returned to 0, I'd like to add and revise your else argument, first
elif x > 0:
return foo(x-1)
else:
return foo(x+1)

chandraw
Автор

u really saved my life thank you, this masterpiece made my day

khadijaaithmid
Автор

Excellent presentation! Why do programming channels that actually teach programming have low subscribers?

fhcoder
Автор

bro I spent too much time trying to find the trick, but there wasn't one it was just that.

charps
Автор

hmm...🤔 Nice video keep it up brada ✨️

togashiyuuta.
Автор

i new know function can loop but recrusion🤯

valerianus_
Автор

Stackover flow errors waiting for you around the corner

amitleffler
Автор

If you call the function with a value less than 0, you'll have infinite recursion...

josea.chacon
Автор

Wait wait. U can put a function inside its own function? Whaaaa

inferno
Автор

i think the explanation here is a bit unclear

so the answer is 0 not just because it was returned by the last call, but actually because this returned value (0 in our case) goes back to where it was called from in previous function, which then returns it over and over again until it gets to the very first foo() call. and since our returned value remains unchanged, it just stays 0

at the same time, if we'd written "return foo(x-1)+ 1" for example, the final output would have been 3, cause each time the recursion goes to the previous function call, it adds 1 to an already returned value
so foo(0) returns 0 (according to the if statement)
then this 0 is returned to the call of foo(1), which then adds 1 to that 0 and hence returns 1
the same way it goes all the way back to foo(3) which returns 3 as a final result

I don't know if that needed an explanation that much or if the way i tried to explain this is any good, but still

lopharb