Skip to content

Commit 1acfff9

Browse files
committed
feat(day 8): implement factorial calculator with loop-based solution
1 parent 67c9707 commit 1acfff9

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
=======================================================================> Factorializer <=====================================================================
3+
Given an integer from zero to 20, return the factorial of that number. The factorial of a number is the product of all the numbers between 1 and the given number.
4+
5+
The factorial of zero is 1.
6+
7+
====================================================================================================================================================================
8+
O/P : ====>
9+
10+
factorial(0) should return 1.
11+
"""
12+
13+
def factorial(n):
14+
fact = 1
15+
16+
if n < 0:
17+
raise ValueError("Factorial is not defined for negative numbers")
18+
if n == 0:
19+
return 1
20+
21+
# while n > 0:
22+
# fact *= n
23+
# n -= 1
24+
25+
# We can also use the for -loop here
26+
for i in range(1, n+1):
27+
fact *= i
28+
29+
return fact
30+
31+
32+
33+
if __name__ == "__main__":
34+
print(factorial(7))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from factorializer import factorial
3+
4+
class factorializerTest(unittest.TestCase):
5+
6+
def test1(self):
7+
self.assertEqual(factorial(0),1)
8+
9+
def test2(self):
10+
self.assertEqual(factorial(5),120)
11+
12+
def test3(self):
13+
self.assertEqual(factorial(20),2432902008176640000)
14+
15+
16+
if __name__ == "__main__":
17+
unittest.main()

0 commit comments

Comments
 (0)