Learn to Program 7 : Recursive Functions Dictionaries

preview_player
Показать описание


In this part of the Learn to Program tutorial series we will look at Dictionaries and one of the most confusing topics in programming being Recursive Functions. A Recursive Function is a function that calls for itself to execute. We'll generate Factorials and Fibonacci numbers with recursive functions to help make them make more sense.

Thank you to Patreon supports like the following for helping me make this video

@kyleaisho
@thetwistedhat
vjFaLk
Рекомендации по теме
Комментарии
Автор

+Derek Banas the reason why your videos are so wonderful is because you are motivational, positive and try to get the concepts understood. All the profs at my university have never been so positive for new students in programming. Your video make me happy to code and try newer styles of code. Thank you for the encouragement! // I loved the Bubble sort program

terraflops
Автор

Recursion has given me trouble before...
I've inserted some print statements to try and understand how it's working but they just go crazy.

endlessetudes
Автор

Just as I've been starting to learn Python recently, I was heading my way over to your page to ask if you'd do some tutorials on some of it, but you had already started making them!! Awesome job on all of your videos. As a programmer myself, I find your videos very helpful in every case! Hope to see more Python soon. Goodluck! :)

Hunter-fytp
Автор

Hey Derek! Its a pleasure hearing your

This is my version of the code for fibonacci numbers using recursion:

fibonacciNums = [1, 1]

def fibonacci(firstNum, secondNum, terms):

if terms == 0:
pass
else:
nextNum = firstNum + secondNum
fibonacciNums.append(nextNum)
fibonacci(secondNum, nextNum, terms - 1)

KaranLobana
Автор

You sir are a genius. I can't even remember names of the languages you teach. But you remember the syntaxes and the implementation.
Wow!! You are the Sal khan in computer science!

gamerfela
Автор

def fib(n):
'''
take range of "n" numbers and prints the fib of them
'''

#we start with two veriables == 1
a = 1
b = 1
ls = []
for i in range(n):
(a, b) = (b, a+b)
ls.append(a)
print(ls)

aviadomeisi
Автор

Always here to listen his comforting voice. Happy Weekend, Derek.

TheOldmankk
Автор

Hi Derek,

As pointed out by robus gauli as well I don't quite understand your example @ 19:43.
1st: result = fib(2) + fib(1) -> we put fib(1) + fib(0) into fib(2), however fib(1) is simply 1
So fib(3) is = 2. As you moments later did it as well.

MrSarky
Автор

My only problem was that I didnt realize that I was right on first try and that the result of the function is the numbers place in fib numbers and not the value of it lol
Only when I tried to use the function in a for loop then I saw the magic. Great videos im so happy I found them here

ozshalev
Автор

Hi Derek,
I get confused as I try to understand the mechanics using a print statement.

def factorial(num):
print('num ', num)
if num == 1:
return 1
else:
res= num * factorial(num-1)
print('res ', res)
return res
print(factorial(4))

num 4
num 3
num 2
num 1
res 2
res 6
res 24
24

num reducing by one is understandable. However, why res is accumulating AFTER num reduce to one is not clear. Is there a loop between 'res= num * factorial(num-1)' and 'return res' ?? I expected print(res) to print one value(24).

Can you please explain?

regivm
Автор

fib(3) = fib(2) + fib(1)
fib(2) = fib(1) +fib(0)
therefore, fib(3) = fib(1) + fib(0) + fib(1) = 1 + 0 + 1 = 2

You said that fib(1) = fib(0) but it seems that fib(1) returns the value 1 instead of calling fib(0). Thank you for the great tutorial derek. I just wanted to know what do you think about that ?

robusgauli
Автор

If it makes you feel smarter, you can write one less else in the factorial code since you return 1 in the first condition so, the code would not continue either way in that last recursive call where the first condition is true.

TLDR / I can only read Python:

def fact(number):
if(number == 0):
return 1
return number * fact(number - 1)
print(fact(5))

omarmoataz
Автор

inp = int(input('Position of the Fibonacci number : '))
fib_num = [1, 1, 2]
for i in range(inp - 3):
fn = fib_num[-1] + fib_num[-2]
fib_num.append(fn)

print(fib_num[(inp - 1)])

pranavraj
Автор

How can you program so accurately. I am learning programming every time I go to run my code I say "here we go, what errors do we have" and the compiler never fails to to show me all the discrepancies. so i have to debug my code at least three times before it runs. one thing that I never forget to do is leave out the semicolons.

brod
Автор

AWESOME VIDEO!!!
Watched all the previous videos and will watch the rest till the end
you are really good at this derek

tamayospicysting
Автор

Great video - recently watched MIT lecture on same material also using fibonacci example while they covered more theory which was useful, in terms of practical application this video is much better.

chrissampson
Автор

I used functions


ctr_list = []

def AskName(*ctr_name):
print('Add customer name ? (y/n)')
response1 = input()

while response1 == 'y':

ctr_name = input('Enter the name : ')
ctr_list.append(ctr_name)
print('Add customer name ? (y/n)')
response1 = input()

if response1 == 'n':
break

return ctr_list

def PrintList(ctr_list):
for name in ctr_list:
print(name)

AskName()
PrintList(ctr_list)

prateekgurjar
Автор

Thank you so much for these videos, I was learning with Tree house but from these videos I am understand a lot more, quickly, keep up the spectacular work!

battledraw
Автор

This was my weird fib solution:

a = 0
b = 1
def fibonacci(n):
global a
global b
print b
c = a + b
a = b
b = c
if n >= 0:
fibonacci(n - 1)

fibonacci(5)

SongSeeker
Автор

well I took only a very little from this lesson - seems I need to spend time doing things recursively. One thing, when I do fib(50) it takes ages to get result wouldnt it be better to use a list and assign values to each new index based on the two previous values? and do it e.g. up to certain value?

sicc