Skip to content

Add recursive factorial method with tests #12800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add recursive factorial method with tests
  • Loading branch information
khushipy committed Jun 19, 2025
commit 4c7cc73ae2ff27cc1b00bff079a9c4cfe1eee926
Empty file added recursion/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions recursion/factorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Fibonacci
https://en.wikipedia.org/wiki/Fibonacci_number
"""
def factorial(number: int) -> int:
"""
Compute the factorial of a non-negative integer using recursion.

>>> factorial(5)
120
>>> factorial(0)
1
>>> factorial(1)
1
>>> factorial(3)
6
>>> factorial(10)
3628800
>>> factorial(-1)
Traceback (most recent call last):
...
ValueError: Input must be a non-negative integer.
"""
if number < 0:
raise ValueError("Input must be a non-negative integer.")
if number == 0:
return 1
return number * factorial(number - 1)
Empty file added recursion/tests/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions recursion/tests/test_factorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import unittest
from recursion.factorial import factorial


class TestFactorial(unittest.TestCase):
def test_factorial_valid_inputs(self):
self.assertEqual(factorial(0), 1)
self.assertEqual(factorial(1), 1)
self.assertEqual(factorial(5), 120)
self.assertEqual(factorial(10), 3628800)

def test_factorial_invalid_input(self):
with self.assertRaises(ValueError):
factorial(-1)


if __name__ == "__main__":
unittest.main()
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy