Recursive and iterative functions (python)


please help me i am stuck,
here is my code:

def recursive_factorial (n):
  if n == 0:
    return 1
    return n *recursive_factorial(n-1)
def iterative_factorial (n):
  product = 1
  for i in range(1,n + 1):
    product = product * i
    return product

this is the assignment i am doing:

Create both a recursive function called recursive_factorial and iterative function called iterative_factorial that does the following

Accepts as parameter an Integer n
Computes the factorial of n
Returns the factorial of n

this is the test i am using:

import unittest

class RecursiveTestCase(unittest.TestCase):

  def test_recursive_factorial_one(self):
    result = recursive_factorial(4)
    self.assertEqual(result, 24, msg="Inaccurate value")
  def test_recursive_factorial_two(self):
    result = recursive_factorial(0)
    self.assertEqual(result, 1, msg="Inaccurate value")
  def test_iterative_factorial_one(self):
    result = iterative_factorial(5)
    self.assertEqual(result, 120, msg="Inaccurate value")
  def test_iterative_factorial_two(self):
    result = iterative_factorial(0)
    self.assertEqual(result, 1, msg="Inaccurate value")

this is the error i am getting:
Total Specs: 4 Total Failures: 21
test_iterative_factorial_oneFailure in line 21, in test_iterative_factorial_one
self.assertEqual(result, 120, msg="Inaccurate value")
AssertionError: Inaccurate value
test_iterative_factorial_twoFailure in line 25, in test_iterative_factorial_two
self.assertEqual(result, 1, msg="Inaccurate value")
AssertionError: Inaccurate value

What might be the problem? please help me......


this is your base case(termination case) . Now you say n==0 what if a negative value is passed as argument eg.

print recursive_factorial(-6) # will this work?

you would need to tweak your base case to handle that. what is the factorial of a negative number?

what if i was to put a value less than one as the argument as before like this

print iterative_factorial (0)# will this work?
print iterative_factorial (-6)# will this one also work?

Re-evealuate that code and handle the magnitude of the inputs just as you attempted to do in the recursive case
Also watch out where you are calling the return statement


Sorry to butt in, but Rydan is wrong here: your iterative function is simply wrong, as your return statement should be de-indented, otherwise is part of the for loop and thus it make the function end at the first iteration.

Unless you are using some smartly compiled language like Haskell, then, avoid recursion as much as possible, as you will soon hit the ceiling of the call stack with this technique (in Haskell or similar, such a recursion is simply converted into an iterative approach).

I would then use something a tad smarter like
recursive_factorial=lambda n: (n or 1) if n<3 else n*recursive_factorial(n-1)
You can test it yourself here or just don't bother following the tip of a competitive coder showing off :wink:


def recursive_factorial(n):
n = int(n)
if n <= 1:
return 1
return (n * recursive_factorial(n - 1))

print (recursive_factorial())

def iterative_factorial(n):
result = 1
for i in range(1, n+1):
result *= i
return result

print (iterative_factorial())