Skip to content
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

9 factorial #60

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 39 additions & 0 deletions solutions/factorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A module contains afunction to compute the factorial of a number

Module contents:
-factorial: a function to compute the factorial of a number

created on 9 Jan 2025
Author: Azza Ibrahim

"""


def factorial(number: int) -> int:
"""
A function computes a factorial of a number

parameters :
Number (int) : the number to compute its factorial

Return:
int : THE fsctorial of the inpiut number

Raises:
ValueError: Factotial is only defined for positive numbers

"""
if number == 0 or number == 1:
return 1

if number < 0:
raise ValueError("Factotial is only defined for positive numbers")

res = 1
for f in range(2, number + 1):
res *= f

return res
53 changes: 53 additions & 0 deletions solutions/tests/test_factorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A module contain test cases for factorial function

Test categories:
normal cases
bounry cases
defensice assertaion


created on 12 Jan 2025
Author: Azza Ibrahim
"""

import unittest
from ..factorial import factorial


class TestFactorial(unittest.TestCase):
"""
Class contiain unit tests for factorial function
"""

def test_factorial_positive(self):
"""test factorial for a positive input number"""
self.assertEqual(factorial(5), 120)

def test_factorial_zero(self):
"""TEst factorial functipn fo input of zero"""
self.assertEqual(factorial(0), 1)

def test_factorial_one(self):
"""test factorial for input one"""
self.assertEqual(factorial(1), 1)

def test_factorial_large(self):
"""test factorail for large input number"""
self.assertEqual(factorial(11), 39916800)

def test_negative(self):
"""test negative input will raise error"""
with self.assertRaises(ValueError):
factorial(-3)

def test_non_intger(self):
"""test case for non intger input"""
with self.assertRaises(TypeError):
factorial(5.5)


if __name__ == "__main__":
unittest.main()
Loading