title: Python : Counting execution of recursive call
body: <p>I am using Euler problems to test my understanding as I learn Python 3.x. After I cobble together a working solution to each problem, I find the posted solutions very illuminating and I can "absorb" new ideas after I have struggled myself. I am working on Euler 024 and I am trying a recursive approach. Now, in no ways do I believe my approach is the most efficient or most elegant, however, I successfully generate a full set of permutations, increasing in value (because I start with a sorted tuple) - which is one of the outputs I want. In addition, in order to find the millionth in the list (which is the other output I want, but can't yet get) I am trying to count how many there are each time I create a permutation and that's where I get stuck. In other words what I want to do is count the number of recursive calls each time I reach the base case, i.e. a completed permutation, not the total number of recursive calls.  I have found on StackOverflow some very clear examples of counting number of executions of recursive calls but I am having no luck applying the idea to my code. Essentially my problems in my attempts so far are about "passing back" the count of the "completed" permutation using a return statement. I think I need to do that because the way my for loop creates the "stem" and "tail" tuples. At a high level, either I can't get the counter to increment (so it always comes out as "1" or "5") or the "nested return" just terminates the code after the first permutation is found, depending on where I place the return. Can anyone help insert the counting into my code?</p>
  
  <p>First the "counting" code I found in SO that I am trying to use:</p>
  
  <pre><code>def recur(n, count=0):
      if n == 0:
          return "Finished count %s" % count
  
      return recur(n-1, count+1)
  
  print(recur(15))
  </code></pre>
  
  <p>Next is my permutation code with no counting in it. I have tried lots of approaches, but none of them work. So the following has no "counting" in it, just a comment at which point in the code I believe the counter needs to be incremented.</p>
  
  <pre><code>#
  # euler 024 : Lexicographic permutations
  #
  import time
  startTime= time.time()
  #
  def splitList(listStem,listTail):
  
      for idx in range(0,len(listTail)):
          tempStem =((listStem) + (listTail[idx],))
          tempTail = ((listTail[:idx]) + (listTail[1+idx:]))
  
          splitList(tempStem,tempTail)
      if len(listTail) ==0:
          #
          # I want to increment counter only when I am here
          #
          print("listStem=",listStem,"listTail=",listTail)
  
  #
  inStem = ()
  #inTail = ("0","1","2","3","4","5","6","7","8","9")
  inTail = ("0","1","2","3")
  
  testStem = ("0","1")
  testTail = ("2","3","4","5")
  
  splitList(inStem,inTail)
  #
  print('Code execution duration : ',time.time() - startTime,' seconds')
  </code></pre>
  
  <p>Thanks in advance,</p>
  
  <p>Clive</p>