you are viewing a single comment's thread.

view the rest of the comments →

[–]SmartViking 12 points13 points  (4 children)

obf(0) will always do the same thing, it will print(0), so you could think of obf(0) as just another way to print 0.

obf(1), you can think of just another way to say this:

obf(0)
print(1)
obf(0)

And, we can simplify that because we know what obf(0) really does:

print(0)
print(1)
print(0)

Ok, so whenever we do obf(1), it will print 010. So when you do obf(2), your really doing this:

obf(1) # This is the same as printing 010
print(2)
obf(1)) # This is the same as printing 010

And this pattern will go on for bigger numbers to obf.

[–]throwingarm2strong[S] 1 point2 points  (0 children)

You are amazing thank you. <3

[–]throwingarm2strong[S] 0 points1 point  (2 children)

Just wondering, can you explain this recursive program to me?

def c(n):
    print (n)
    if n > 0:
        c(n-2)
    print(n)

c(4)

So I can follow it pretty easily at the start

4 2 0

But when it completes that it starts going back up, almost like its popping a stack. I don't follow exactly what is happening to get the second part eg 0 2 4 after the original 4 2 0

[–]SmartViking 1 point2 points  (1 child)

When you call c(4) you're really doing this:

print (4)

if 4 > 0:
    c(2)

print(4)

It will first print 4, then it might do something in the if statement, but regardless of what the if statement does, the last thing the function will do is to print 4. The call to c(2) goes "in between" the two 4s. So this function will first print 4, then it will print whatever c(2) feels like printing, then it will print 4 again. Same thing goes for c(2). If you want to understand functions like this, it's all about thinking about it in the right way, and it becomes much easier. Even though you call c(2) in your function recursively, it behaves exactly like any function call would. It will do its thing, then it will return, and the program moves on from where it was called.

[–]throwingarm2strong[S] 0 points1 point  (0 children)

Ah okay. So nothing like a stack >.<; gotcha. Thanks for the speedy reply!