python - Keeping count in a recursive function -


I had seen some posts about calculating the number of iterations of a recursive but I could not follow them or Then they apologize to the application, though it is useless! I appreciate the help! I am trying to correct a function for a random walking condition in which the recursive function is used and to calculate the number of steps there is also a visual component that tells me that the actual function is working But counting is not part. The number of steps taken by Sleepwalker to reach the following or upper bound input: An integer start (the start position of the sleepy), An integer less (non-negotiable, the smallest value of our sleeper wandering), and an integer (the highest value sleeper can wander) "" "count = 0 newcount = count +1 '' * start + '~' print (' '* Start +' ~ ') If less & lt; get started and start & lt; hi: newstart = start + rs () newcou Nt = count + 1 returns rwSteps (newstart, low, hi) returns newcount elif start == less: finalcount = newcount +1 returns final count elif start == hi: finalcount = newcount +1 return final number

The function returns only the number of the phase that was taken in it and its lineage:

  def rwSteps (where, less, hi): print ("{} ~" .format ('' * where)) If less & lt; where & lt; Greetings: return 1 + rwt (where + RS (), Low, Hello) Other: Return 0   

However, this recursive one is bad Use - it is slow and it is very likely to run out of the stack space and it can easily be rewritten:

  Random Import Randint (RF): Return Rendin (-1, 1) def rw_steps (where, Low, Hi): "" Returns the number of steps to move the sleepwalker before killing the lower or upper bound input: Where: Sleepwalker Low: Integred; = 0, lower border Hi: Integrated; Lower, upper limit "" phase = 0 while lower & lt; Where & lt; Hello: Print ("{} ~" .format ('' * where)) Step + = 1 where + = RS () print ("" exits the range after "{} steps.") Format (phase)) Step    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -