From 26cadb6fa0075ba0a99389e9349a556d0da8ac0d Mon Sep 17 00:00:00 2001 From: "s.ashour82" Date: Mon, 30 Dec 2024 22:46:38 +0200 Subject: [PATCH 1/2] factorial challenge factorial challenge is done --- .vscode/settings.json | 6 +++- solutions/factorial.py | 56 +++++++++++++++++++++++++++++ solutions/tests/test_factorial.py | 59 +++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 solutions/factorial.py create mode 100644 solutions/tests/test_factorial.py diff --git a/.vscode/settings.json b/.vscode/settings.json index 058679944..1b51e567f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -116,5 +116,9 @@ "markdown.extension.toc.updateOnSave": false, "[python]": { "editor.defaultFormatter": "ms-python.black-formatter" - } + }, + "cSpell.words": [ + "Ashour", + "Saad" + ] } diff --git a/solutions/factorial.py b/solutions/factorial.py new file mode 100644 index 000000000..6c9afb155 --- /dev/null +++ b/solutions/factorial.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +A module for Computing the factorial of a non-negative integer n. + +Module contents: + - factorial: is the product of all positive integers less than or equal to n. + +Created on XX XX XX +@author: Saad M. Ashour +""" + + +def factorial(n: int) -> int: + """ + Computes the factorial of a non-negative integer n. + + The factorial of a non-negative integer n + is the product of all positive integers less than or equal to n. + + Parameters: + n (int): A non-negative integer. + + Returns: + int: The factorial of the input integer n. + + Raises: + ValueError: If n is negative, as factorial is not defined for negative numbers. + TypeError: If n is not an integer, as factorials are only defined for integers. + + Examples: + >>> factorial(0) + 1 + + >>> factorial(1) + 1 + + >>> factorial(5) + 120 + + >>> factorial(3) + 6 + """ + # Validate input type and value + if not isinstance(n, int): + raise TypeError("Input must be an integer.") + + if n < 0: + raise ValueError("Factorial is not defined for negative numbers.") + + # Base case for recursion: 0! = 1 + if n == 0 or n == 1: + return 1 + + # Recursive case: n! = n * (n-1)! + return n * factorial(n - 1) diff --git a/solutions/tests/test_factorial.py b/solutions/tests/test_factorial.py new file mode 100644 index 000000000..0c3e3f40c --- /dev/null +++ b/solutions/tests/test_factorial.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Unit tests for factorial function. + +@author: Saad M. Ashour +""" + +import unittest +from solutions.factorial import factorial + + +class TestFactorial(unittest.TestCase): + """ + Unit tests for the factorial function. + + The factorial function computes the factorial of a non-negative integer using recursion. + These tests verify its correctness for various inputs, + including edge cases, valid cases, and invalid cases. + """ + + +def test_factorial_zero(self): + """It should evaluate 0 to 1""" + actual = factorial(0) + expected = 1 + self.assertEqual(actual, expected) + + +def test_factorial_one(self): + """It should evaluate 1 to 1""" + actual = factorial(1) + expected = 1 + self.assertEqual(actual, expected) + + +def test_factorial_positive(self): + """It should evaluate 5 to 120""" + actual = factorial(5) + expected = 120 + self.assertEqual(actual, expected) + + +def test_factorial_negative(self): + """factorial of negative integer will raise Error""" + with self.assertRaises(ValueError): + factorial(-1) + + +def test_factorial_non_integer(self): + """factorial of non integer will raise Error""" + with self.assertRaises(TypeError): + factorial(3.5) + with self.assertRaises(TypeError): + factorial("string") + + +if __name__ == "__main__": + unittest.main() From ba6952dbf29cfb08dd21900e710ac04df216cf5b Mon Sep 17 00:00:00 2001 From: "s.ashour82" Date: Tue, 31 Dec 2024 13:03:51 +0200 Subject: [PATCH 2/2] Update settings.json --- .vscode/settings.json | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1b51e567f..058679944 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -116,9 +116,5 @@ "markdown.extension.toc.updateOnSave": false, "[python]": { "editor.defaultFormatter": "ms-python.black-formatter" - }, - "cSpell.words": [ - "Ashour", - "Saad" - ] + } }