0
how does this loop works?
def function (k): if k<2: return k else: return function(k-1)+function(k-2) n=8 z=function(n) print str (z) what is the outcome of this loop and how does it work? many thanks:)
10 Respostas
+ 6
Sal If you actually put effort into understanding what the function does, handtracing isn't complicated at all. It's just long, and you are merely fazed by the length of it.
+ 5
Here we go again, aight.
function(n)
function(8)
function(7)+function(6)
function(6)+function(5)+function(5)+function(4)
function(5)+function(4)+function(4)+function(3)+function(4)+function(3)+function(3)+function(2)
function(4)+function(3)+function(3)+function(2)+function(3)+function(2)+function(2)+function(1)+function(3)+function(2)+function(2)+function(1)+function(2)+function(1)+function(1)+function(0)
function(3)+function(2)+function(2)+function(1)+function(2)+function(1)+function(1)+function(0)+function(2)+function(1)+function(1)+function(0)+function(1)+function(0)+function(1)+function(2)+function(1)+function(1)+function(0)+function(1)+function(0)+function(1)+function(1)+function(0)+function(1)+function(1)+function(0)
+ 5
Sal And if you actually mind to explain what you don't understand, instead of just complaining about it, I can help clear your doubts.
+ 4
(cont)
function(2)+function(1)+function(1)+function(0)+function(1)+function(0)+function(1)+function(1)+function(0)+function(1)+function(1)+function(0)+function(1)+function(0)+function(1)+function(1)+function(0)+function(1)+function(0)+function(1)+function(1)+function(0)+function(1)+function(1)+function(0)+function(1)+function(0)+function(1)+function(1)+function(0)+function(1)+function(1)+function(0)
function(1)+function(0)+function(1)+function(1)+function(0)+function(1)+function(0)+function(1)+function(1)+function(0)+function(1)+function(1)+function(0)+function(1)+function(0)+function(1)+function(1)+function(0)+function(1)+function(0)+function(1)+function(1)+function(0)+function(1)+function(1)+function(0)+function(1)+function(0)+function(1)+function(1)+function(0)+function(1)+function(1)+function(0)
1+0+1+1+0+1+0+1+1+0+1+1+0+1+0+1+1+0+1+0+1+1+0+1+1+0+1+0+1+1+0+1+1+0
21
+ 1
Joel Kronqvist not really, Hatsy Rei description is to complicate
0
ok... i wont get it^^ but thank you:)
0
When the function is executed first time, k is 8, so it goes to the else-statement. There "function" returns itself two times and so on, like this:
k=8
Function executes:
k = (8-1)+(8-2)
= (7) + (6)
Function executes:
k = (7-1) + (6-2)
= (6) + (4)
Function executes:
k = (6-1)+(4-2)
= (5) + (2)
Function executes:
k = (5-1) + (2 - 2)
= (4) + (0)
Function executes:
k = (4 - 1) + (0-2)
= (3) + (-2)
=1
, so it goes to the if-statement, because k is lesser than 2.
return k;
= return 1;
So, it returns 1.
I'm not sure if I understood that rigth myself, but I hope it works like this😓
I hope this was helpful.
0
Oh, I'm sorry, I tested it, it isn't 1...
0
no the outcome for this function is 21😄 but thank you for tying to help;)
0
I hope you understood how it works from the other answers.