how does this loop works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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:)

16th Jul 2018, 1:35 PM
Sal
10 Answers
+ 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.
16th Jul 2018, 2:29 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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)
16th Jul 2018, 1:47 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
16th Jul 2018, 2:30 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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
16th Jul 2018, 1:51 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Joel Kronqvist not really, Hatsy Rei description is to complicate
16th Jul 2018, 2:27 PM
Sal
0
ok... i wont get it^^ but thank you:)
16th Jul 2018, 1:53 PM
Sal
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.
16th Jul 2018, 1:57 PM
Joel Kronqvist
Joel Kronqvist - avatar
0
Oh, I'm sorry, I tested it, it isn't 1...
16th Jul 2018, 2:20 PM
Joel Kronqvist
Joel Kronqvist - avatar
0
no the outcome for this function is 21๐Ÿ˜„ but thank you for tying to help;)
16th Jul 2018, 2:22 PM
Sal
0
I hope you understood how it works from the other answers.
16th Jul 2018, 2:23 PM
Joel Kronqvist
Joel Kronqvist - avatar