简体   繁体   中英

Recursive function counting number of calls for another function

In python recursive functions homework I'm required to write a code that prints out letter 's' as a tracing of a sleeping man steps. I wrote my code using two functions.....

import random  
def rs():
    """ rs chooses a random step and returns it 
        note that a call to rs() requires parentheses
        inputs: none at 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, Now I'm required to add another function to print the number of steps after printing the steps it self.I don't want to count within the function itself.Thank you in advance.

EDIT thank you all I got an idea and its working so here is my new code

 import random def rs(): """ rs chooses a random step and returns it note that a call to rs() requires parentheses inputs: none at 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 a decorator function to wrap your rwsteps function and count the number of calls:

The decorator function used to count the calls of the wrapped function will:

  1. define a wrapper function with a counter variable that will be incremented whenever the wrapper function is called.
  2. call the 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 and 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 the @ decorator notation is the same as:

rwsteps = counted(rwsteps)

rwsteps(5, 0, 10)
rwsteps.count

Which also works:

|    S    |
|     S   |
|      S  |
|       S |
|      S  |
|     S   |
|    S    |
|     S   |
|    S    |
|   S     |
|  S      |
| S       |
|S        |
14

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM