0

I am trying to implement a recursive function in python, let's call such function F, the input of such function is a list, lets call it a. The logic of the realization of F should be as following,

def F(a):
    if this is first time I run this function (in other words if this is not the first time I run this function, leave a as it is):
        a.reverse()
    then do something recursively calling F

I want to ask that what is the simplest way of achieving such goal. Does python actually support such logic?

7
  • 1
    You either make a wrapper function to call F and do what you need to first or something like F(a, first_time=False) and call it via F(a, True) Commented Sep 2, 2021 at 14:38
  • @Sayse Wrapper solution is cleaner, it avoids having to evaluate first_time in every recursive call. Wrappers are a good solution to one-time setup issues or data cleaning such as bounds checks. Commented Sep 2, 2021 at 14:40
  • Hi, thx for your comment. I don't want any extra variable in the function. So can you elaborate on what you mean by wrapper function in this case? Commented Sep 2, 2021 at 14:40
  • 1
    You make def Fx() that calls F after doing whatever presetup you need Commented Sep 2, 2021 at 14:41
  • @pjs - Yes it is assuming you're in a position where you're able to refactor easily to introduce a wrapper function, the latter is for times when thats not the case Commented Sep 2, 2021 at 14:42

3 Answers 3

4

Typically, you make F a wrapper around a separate recursive helper function.

def F(a):
    def helper(a):
        "Do recursive stuff"

    a.reverse()
    return helper(a)

If, for whatever reason, it is prohibitively expensive to re-define _F_helper each time F is called, you can define it outside F:

def _F_helper(a):
    "Do recursive stuff"

def F(a):
    a.reverse()
    return _F_helper(a)

The benefit of either approach is that you don't spend a lot of time trying to decide whether or not a.reverse() should be called. It gets called unconditionally once, and then you proceed with the recursion.

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

Comments

0

you can try something like this

def F(a):
    F.counter += 1
    print(F.counter)
    if F.counter == 1:
       a.reverse() 
    F(a)
F.counter = 0
F(a)

you no need to define a global variable or any other

3 Comments

This looks very expensive compared to using a simple wrapper.
Very unfortunate, this method doesn't seem to work. currentframedoesn't accept any input.
@CoolGas sorry for that i changed my approach . try this will works this is not expensive compared to other
-1

you can input root into your function:

def F(a:list,radical = 0):
    old_a = a.copy()
    if radical == 0:
        a.reverse()
    # do sometihing ......
    return F(old_a,radical + 1)

1 Comment

Can potentially be a solution, but I don't want any extra variable in the function.

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.