1

please help me understand where i went wrong:

this is the question: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 for the question:

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 code i have written:

def recursive_factorial(n):
    if n == 0:
        return 1
    else:
        return n * recursive_factorial(n-1)
def iterative_factorial(n):
    x = 1
    li = list(range(1, n + 1))
    for each in li:
        x = x * each

this is the error i am getting:

1 . test_iterative_factorial_one

Failure in line 21, in test_iterative_factorial_one self.assertEqual(result, 120, msg="Inaccurate value") AssertionError: Inaccurate value

2 . test_iterative_factorial_two

Failure in line 25, in test_iterative_factorial_two self.assertEqual(result, 1, msg="Inaccurate value") AssertionError: Inaccurate value 

please help me understand where i went wrong.

2
  • 2
    iterative_factorial(n): has no return Commented Jan 8, 2016 at 11:46
  • Can you add some debug to print the result of recursive_factorial(0). The code looks correct. Commented Jan 8, 2016 at 11:53

3 Answers 3

5

You're forgetting to return x from iterative_factorial(), so the function is implicitly returning None.

As an aside, you can iterate of the result of range() directly:

for each in range(1, n + 1):
   ...

Finally, this might be a good opportunity to learn about Python's reduce() function.

import operator

def another_iterative_factorial(n):
   return reduce(operator.mul, range(1, n + 1))
Sign up to request clarification or add additional context in comments.

1 Comment

and if you are using python3 reduce was moved to the functools module. Also, good chance to learn about lambda function as you can write reduce(lambda x,y:x*y,range(1,n+1))
1

iterative_factorial needs to end with

    return x


Also, iterative_factorial doesn't need li. Better to just write:

    for each in range(1,n+1):

3 Comments

test_recurisve_factorial_two tests 0!, not 2!
D'oh! In that case I don't understand.
@Davismuriungi - make sure to accept NPE's answer. It was written first; it didn't start out with errors; and it includes reduce.
0
def recursive_factorial(n):
    if n == 0:
        return 1
    else:
        return n * recursive_factorial(n-1)


def iterative_factorial(n):        
    x = 1
    li = list(range(2, n+1))
    for each in li:
        x = x*each
        yield x

2 Comments

You need to fix the indentation of the return x sentence in iterative_factorial.
You could simplify iterative_factorial by writing for each in range(2, n + 1):

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.