0

Basically, I'm not sure what's wrong with my answer, versus the model answer. What I'm trying to achieve here is to produce a function that can do lambda x: f(g(f(g(f(g(f(x))))))) # for n == 7 or lambda x: f(g(f(g(x)))) # for n == 4

What I've tried

def dual_function(f, g, n):
    if n == 1:
        return f
    elif n % 2 == 0:
        return lambda x: dual_function(f,g, n-1)(g(x))
    else:
        return lambda x: dual_function(f,g, n-1)(f(x))
# the code seems to do the above from my understanding?
# it starts off at n == 7: new_x = f(x)
# n == 6: new_x = g(new_x)
# n == 5: new_x = f(new_x) 
# and so continues down...

The model answer (Sorry i got the wrong model answer for reference, here's the proper one, but now both actually works lol)

    def dual_function(f,g,n):
    def helper(x):
        f1,g1 = f,g
        if n%2==0:
            f1,g1 = g1,f1
        for i in range(n):
            x = f1(x)
            f1,g1= g1,f1
        return x
    return helper

Sample example

f = lambda x: x+1
g = lambda x: x/2
print(dual_function(f, g, 7)(1)) 
# correct answer is 0.9375, versus my wrong answer: 2.0
3
  • What's the range for n? Commented Mar 27, 2019 at 6:31
  • probably something around sys.getrecursionlimit() which defaults to 1000 Commented Mar 27, 2019 at 6:33
  • Your code gives 2 and f(g(f(g(f(g(f(1))))))) = 2. So the code matches the description. I guess you and the model simply solve slightly different problems. Commented Mar 27, 2019 at 7:47

1 Answer 1

1

Your code and the model code seem to be solving different problems. Your code always starts with f(...) as the outermost call (and the innermost call can vary depending on whether n is even or odd), while the reference code always has g(x) as the innermost call (and the outermost call can vary).

So the reason your function isn't matching for n=7 is that you're computing f(g(f(g(f(g(f(x))))))) while the other function is doing g(f(g(f(g(f(g(x))))))). Unfortunately, I can't tell you which of those is what you're actually supposed to be computing.

Sign up to request clarification or add additional context in comments.

7 Comments

As per the question, the objective is to have f(x) as the outermost function.
Replacing the f(x) and g(x) the elif and else block of OP's answer would be right as per the description of the question.
@Abhijeetk431: I don't understand your second comment. The questioner's recursive code does exactly what it is trying to do, it's the model solution that does something different. In my answer I wanted to raise the possibility that the questioner may have misunderstood their homework assignment. It's also possible that the model solution is wrong, but I have no way to know.
It is a recursive function, so OP's code has f(x) as the innermost call. Seems I made a mistake in my second comment and You made a mistake in your answer. :) I already upvoted your answer. Change that one thing that the OP's code has f(x) as innermost call always. Also, neglect my second comment as it is obviously not right.
No, double check how the OP's code works. It works from the inside out, with the base case being the outermost call. Try it yourself and you'll see that calling their code with n=4 you get f(g(f(g(x)))), not g(f(g(f(x)))) as you seem to be expecting.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.