Recursion in Python without using a local/internal variable -
I am not posting my exact problem in order to avoid space sake and confusion, but instead of identifying one I In the Python, print the sequence of numbers backwards. Restraint is that I should use recursion, and I should not have any local variables inside my function. For example, if the number 8 is given for the given function, then I want to print it 1,2,4,8. Also assume that the input is an integer, please.
def function (x) If (x! = 1) x = x / 2 I have dispelled my mind for a while, But I can not find anything that will actually print the final value (i can only achieve it to output 1,2,4) def function (x) if (x! = 1) x = x / 2 function (x) print x, code above 1,2,4 Prints, given that x is 8 in the beginning. However, if I put a recursive call anywhere in front of the "x = x / 2" line, then the value of x is constant 8
How can I accomplish everything I am trying to do? I'm sure there is something simple that I see, but I do not know how it is.
EDIT: Whoops, forgot to fill my print statement there.
Edit 2: Many thanks for both of you! Can not believe that this function calculates itself to avoid compiling the variable every time to calculate itself in the call.
Again, thanks a lot!
Everything is fine, but you really do not print anything. How (x! = 1): about function x (x / 2) print x
def (x)
Comments
Post a Comment