What is the outcome for these loop? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the outcome for these loop?

def function (k):         if  k<2:                return k         else:                return  function(k-1)+function(k-2) n=8 z=function(n) print str (z) my outcome: 8-1 + 7-2 = 12 5-1 + 4-2 = 6 so outcome is 6 am i right? many thank for help

15th Jul 2018, 3:45 PM
Sal
28 Answers
+ 1
suprb nice answer...tqs ..kamboj
17th Jul 2018, 2:07 PM
lokesh reddy
lokesh reddy - avatar
+ 1
thnq u so much fr ur great answer..pulkit
18th Jul 2018, 4:27 PM
lokesh reddy
lokesh reddy - avatar
0
no you are wrong output is 21 for f(8) f(0)=0 f(1)=1 f(2)=f(0)+f(1)=1+0=1 f(3)=f(1)+f(2)=1+1=2 . . . f(8)=f(7)+f(6)=13+8=21 https://code.sololearn.com/cJAkxH7aaoI4/?ref=app
15th Jul 2018, 5:39 PM
Pulkit Kamboj
Pulkit Kamboj - avatar
0
hi..kamboj.....i have one dought what is diffrence between yield and return staments with one example
16th Jul 2018, 4:51 PM
lokesh reddy
lokesh reddy - avatar
0
hi lokesh reddy return statement will never come back once the return statement is executed but for yield, it executes till the end. for example in a function if you want to return 10 values and in order to do that you try execute return statement 10 times but you will see that once return statement is executed it will not be executed again but with help of yield you can return any number of values for a function. for e.g. in the function def test(): for i in range(5): yield i for j in test(): print(j) #output=0(here first time yield statement is executed, the cursor comes out of function and prints the value but still again enters inside the function fir the second execution of yield) 0 1(now as we can see that yield statement is executed again, cursor moves out of function and after printing the value again cursor goes back to the function) 2(this continues till yield is executed in function) 3 . 4
16th Jul 2018, 6:47 PM
Pulkit Kamboj
Pulkit Kamboj - avatar
0
in the code if you replace yield by return then the output is only 0 because once it comes out of the function(return statement is executed) then it will not go back to the function so return is only executed only once in a function.
16th Jul 2018, 6:53 PM
Pulkit Kamboj
Pulkit Kamboj - avatar
0
hi pulkit kamboj.. a=10 a=12 a=2 so output is 2 last variable is executed ,but why last 'a' is executed and, i need fisrt 'a' output..
18th Jul 2018, 11:19 AM
lokesh reddy
lokesh reddy - avatar
0
lokesh reddy ,here when the first line is executed the python interpreter assigns a particular memory of the system to the variable a and the memory of 'a' is occupied by the integer 10. now when second line(a=12) is executed, then you know that a particular amount of memory is assigned to variable 'a' and the memory is already occupied by the integer 10, so interpreter has no other option but to remove 10 from the memory and to keep 12 in that memory so 10 is removed from the memory and the similar thing occurrs when third line is executed). So now if you want to keep value a, so you should use b=12 and c=2 so that different memory is assigned to them or you can use something like lists.
18th Jul 2018, 2:01 PM
Pulkit Kamboj
Pulkit Kamboj - avatar
0
hi pulkit... what is diffrence between os module and sys module..plz one example kamboj
18th Jul 2018, 6:21 PM
lokesh reddy
lokesh reddy - avatar
0
these are two different modules which are used for doing different tasks, well the examples you can find from here, http://thomas-cokelaer.info/tutorials/JUMP_LINK__&&__python__&&__JUMP_LINK/module_os.html
19th Jul 2018, 10:14 AM
Pulkit Kamboj
Pulkit Kamboj - avatar
0
very very tqs pulkit... pulkit which book is best for learning python or send any link.
20th Jul 2018, 4:48 PM
lokesh reddy
lokesh reddy - avatar
0
Well i do not follow any book but you can get a lot of materials by using search bar, I learnt by watching videos of cs dojo and i daily practice coding on hackerrank
21st Jul 2018, 7:09 AM
Pulkit Kamboj
Pulkit Kamboj - avatar
0
okkk..tq kamboj.
21st Jul 2018, 9:47 AM
lokesh reddy
lokesh reddy - avatar
0
hi kamboj..how r u...my question what is iterator and one example.
1st Sep 2018, 8:17 PM
lokesh reddy
lokesh reddy - avatar
0
hi lokesh, i am fine, what about you An iterable is an object or container which can be iterated or we can use loops to transverse through it's elements. I don't want to explain in more as this example will make everything clear. Some examples are all containers such as list dictionary tuple etc. are iterator. Here is how list is an iterator l=[2,3,4,5] for j in l: print(l) Here l is a iterator and the process is known as iteration. So, in short an object to which we can apply the process of iteration is iterator. At the end, sorry for replying so late.
6th Sep 2018, 6:38 PM
Pulkit Kamboj
Pulkit Kamboj - avatar
0
i am fine kaboj, tqssss pulkit..good answer
8th Sep 2018, 9:06 AM
lokesh reddy
lokesh reddy - avatar
0
hi pulkit...one question, l=[1,4,3,5,9,8,1,3,4,9,5,8,1] i need output like this , [1,1,1,3,3,4,4,5,5,8,8,9,9] how pulkit.
11th Sep 2018, 9:06 AM
lokesh reddy
lokesh reddy - avatar
0
print(sorted(l))
12th Sep 2018, 5:37 PM
Pulkit Kamboj
Pulkit Kamboj - avatar
0
tqs pulkit..but without sorted method,how?
14th Sep 2018, 7:45 PM
lokesh reddy
lokesh reddy - avatar
0
hi pulkit.. what is class method and instance method and static method and what is exact diffrence beween the methods?
18th Sep 2018, 7:49 PM
lokesh reddy
lokesh reddy - avatar