python - Recursive function counting number of calls for another function -


in python recursive functions homework i'm required write code prints out letter 's' tracing of sleeping man steps. wrote code using 2 functions.....

import random   def rs():     """ rs chooses random step , returns          note call rs() requires parentheses         inputs: none @ all!     """     return random.choice([-1,1]) def rwsteps( start, low, hi ) :     '''      inputs :start    --> starting position of sleepwalker             low     --> nonnegative  smallest value allowed             hi       --> highest value allowed'''     if start <= low or start >= hi :         return      else :         print '|' + ' ' * (start - low) + 's' + ' ' * (hi - start) + '|'          return rwsteps( start + rs() , low, hi ) 

that works ok, i'm required add function print number of steps after printing steps self.i don't want count within function itself.thank in advance.

edit thank got idea , working here new code

import random      def rs():      """ rs chooses random step , returns           note call rs() requires parentheses          inputs: none @ all!      """      return random.choice([-1,1])    c = 0  def counter():      global c      c += 1       return c    def rwsteps( start, low, hi ) :      '''       inputs :start    --> starting position of sleepwalker              low     --> nonnegative  smallest value allowed              hi       --> highest value allowed'''            if start <= low or start >= hi :          print c - 1          return       else :          print '|' + ' ' * (start - low) + 's' + ' ' * (hi - start) + '|'          counter()                 return rwsteps( start + rs() , low, hi )    rwsteps( 10, 5, 15 )

you can use decorator function wrap rwsteps function , count number of calls:

the decorator function used count calls of wrapped function will:

  1. define wrapper function counter variable incremented whenever wrapper function called.
  2. call wrapped function.

from random import getrandbits import sys ​ sys.setrecursionlimit(10**6) ​ def counted(fn):     def wrapper(*args, **kwargs):         wrapper.count += 1         return fn(*args, **kwargs)     wrapper.count = 0     return wrapper ​ def rs():     return -1 if getrandbits(1) else 1 ​ @counted def rwsteps(pos, low, hi) :     if pos > low , pos < hi:         print "{0: <{l}}s{0: >{r}}".format('|', l=pos-low, r=hi-pos)         return rwsteps(pos+rs(), low, hi) ​ rwsteps(5, 0, 10) rwsteps.count 

|    s    | |   s     | |  s      | | s       | |s        | | s       | |  s      | | s       | |s        | | s       | |s        | | s       | |  s      | | s       | |s        | | s       | |s        | | s       | |  s      | | s       | |  s      | |   s     | |  s      | | s       | |  s      | | s       | |s        | 28 

using the @ decorator notation same as:

rwsteps = counted(rwsteps)  rwsteps(5, 0, 10) rwsteps.count 

which works:

|    s    | |     s   | |      s  | |       s | |      s  | |     s   | |    s    | |     s   | |    s    | |   s     | |  s      | | s       | |s        | 14 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -