-1

I have a function def recFib which returns a bunch of different numbers but I'd like to be able to count how many times it returns "Computing fib( 3 )". I tried to implement a counter but it just returns 0. Does anyone know how I would do this?

CODE:

# fib.py
def loopFib(n):
    # pre: n > 0
    # returns the nth Fibonacci number
    curr = 1
    prev = 1
    for i in range(n-2):
        curr, prev = curr+prev, curr
    return curr

def recFib(n):
    print("Computing fib" "(",n,")" )

    if n<3:
        print("Leaving fib" "(",n,")" "returning",1)
        return 1
    else:
        x=recFib(n-1)
        y=recFib(n-2)
        print("Leaving fib" "(",n,")" "returning",x+y)
        return x+y



n = 10
#print(recFib(n))



counter = []
if (recFib(n) == "Computing fib( 3 )"):
    counter.append('Three')

count3s = counter.count('Three')
print("Threes", count3s )

#Should return 21 instances of "Computing fib(3)"

OUTPUT: (example of what the output looks like)

Computing fib( 10 )
Computing fib( 9 )
Computing fib( 8 )
Computing fib( 7 )
Computing fib( 6 )
Computing fib( 5 )
Computing fib( 4 )
Computing fib( 3 )
Computing fib( 2 )
Leaving fib( 2 )returning 1
Computing fib( 1 )
Leaving fib( 1 )returning 1
Leaving fib( 3 )returning 2
Computing fib( 2 )
Leaving fib( 2 )returning 1
Leaving fib( 4 )returning 3
Computing fib( 3 )
AMC
  • 2,642
  • 7
  • 13
  • 35
Dorinas
  • 3
  • 2
  • Have you done any debugging? As an aside, variable and function names should generally follow the `lower_case_with_underscores` style. – AMC May 03 '20 at 00:35

2 Answers2

1

Your function never returns "Computing fib( 3 )", it only prints that. That's why your condition recFib(n) == "Computing fib( 3 )" never comes true. The easiest way to modify your code is:

counter = 0

def recFib(n):
    global counter
    print("Computing fib" "(",n,")" )
    if n==3:
        counter += 1
    if n<3:
        print("Leaving fib" "(",n,")" "returning",1)
        return 1
    else:
        x=recFib(n-1)
        y=recFib(n-2)
        print("Leaving fib" "(",n,")" "returning",x+y)
        return x+y

print(recFib(10))
print(counter)

which indeed outputs 21.

Błotosmętek
  • 12,717
  • 19
  • 29
1

Using a call counter decorator

Decorator

def call_counter(func):
    def helper(x):
        if x == 3:
          # Counts calls when argument is 3
          helper.calls += 1
        return func(x)
    helper.calls = 0
    return helper

Just add decorator to original code

@call_counter   # Add decorator
def recFib(n):
    print("Computing fib" "(",n,")" )

    if n<3:
        print("Leaving fib" "(",n,")" "returning",1)
        return 1
    else:
        x=recFib(n-1)
        y=recFib(n-2)
        print("Leaving fib" "(",n,")" "returning",x+y)
        return x+y

Usage

print(recFib(10))
print(f'Instances of computing fib 3 is {recFib.calls}')

Output

Instances of computing fib 3 is 21
DarrylG
  • 16,732
  • 2
  • 17
  • 23
  • Thank you! this solution worked perfectly, Ive never used or heard of a decorator in Python before so this was very insightful! – Dorinas May 02 '20 at 22:44
  • @Dorinas--glad I could help. Decorators are one of the popular features. [More info on decorators](https://www.programiz.com/python-programming/decorator) – DarrylG May 02 '20 at 23:03