2
def f1(t):
    if t < 0:
        return 0
    result = 0
    result = 1.5 * f2(t-1) + 3.5 * f1(t-1)
    return result

def f2(t):
    if t < 0:
        return 1
    result = 0
    result = 1.5 * f1(t-1) + 3.5 * f2(t-1)
    return result

for i in range(10):
    result = 0
    result = f1(i) + f2(i)
    print(result)

Running the above code takes a lot of time. I want to limit the number of executions of f1() and f2() to 10 executions each for every iteration in the for-loop, then proceed to the next for-loop value. How to do so?

Edit: 1.original pseudo code for this part 2.actual functions

Sourav Kar
  • 31
  • 3
  • 1
    What's the base case (after each function has been run 10 times)? – Alex Mar 03 '18 at 16:52
  • If I add a return value, say 0 or 1, for t < 0, I get same return results for every value of t passed to f1()/f2(). This is a simpler representation of a part of the code for a project I am currently working on. – Sourav Kar Mar 03 '18 at 16:58
  • You may want to read [how recursion works](https://stackoverflow.com/questions/2783306/base-case-in-a-recursive-method). – Alex Mar 03 '18 at 17:01
  • I understand how recursion works. Just check the images in 'Edit'. There is no such base case given in the original pseudo code for this function. – Sourav Kar Mar 03 '18 at 17:16

2 Answers2

1

I sometimes need a similar method to debug recursive code. A very simple method is to declare a variable in a scope outside the recursive function.

iterations_f1 = 10
iterations_f2 = 10

Then add some logic temporarily inside the recursive function to return before another recursive call if the iterations have been met.

def f1(t):
  global iterations_f1
  iterations_f1 = iterations_f1 - 1
  if iterations_f1 == 0:
    return

  # rest of funtion
  ...
גלעד ברקן
  • 23,602
  • 3
  • 25
  • 61
1

You could use a keyword argument to pass on the recursion depth.

def f1(t, _max_depth=10):

    if _max_depth > 0:
        if t < 0:
            return 0
        result = 0
        result = 1.5 * f2(t-1, _max_depth=_max_depth - 1) + 3.5 * f1(t-1, _max_depth=_max_depth - 1)
        return result

    else:
        # return some default value here

You can do the same for f2.

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
  • 1
    The reason I prefer the global in this instance is that I don't modify the function (read: the number of arguments) in order to perform the debug. And the ugly code also reminds me to remove it when I'm done. – גלעד ברקן Mar 03 '18 at 19:32
  • It's fine, we can leave both answers and let OP pick whichever suits them best. As I said, your answer is not wrong, it's just bad practice. – Olivier Melançon Mar 03 '18 at 19:35
  • Cool :) (I personally don't believe in "bad practice," only use-in-context.) – גלעד ברקן Mar 03 '18 at 19:40
  • Given that all software is “use in context”, the practice of ignoring that some use in context is bad practice (i.e. sticking your head in the sand and putting fingers in your ears and saying very loudly “I’m not listending), is itself a VERY bad practice. I’m glad I don’t have anything to do with softare you write. – DisappointedByUnaccountableMod Mar 04 '18 at 17:23
  • this would work, but i think that if you know in advance that a recursive function will only be called X times, it might be the moment to change the code and make a simple loop of it (the simplified example in the question doesn't need recursion), recursion uses resources – Martijn Scheffer Dec 16 '21 at 07:48