Skip to content

Commit 8dbd53b

Browse files
committed
add factorial
1 parent ef36cc3 commit 8dbd53b

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

Factorial/Python/factorial.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
File: factorial.py
6+
Author: tdoly
7+
Description: http://en.wikipedia.org/wiki/Factorial
8+
'''
9+
10+
def factorial(num):
11+
if num < 0:
12+
raise "the input number is error"
13+
elif num == 0:
14+
return 1
15+
else:
16+
return reduce((lambda x, y: x*y), [i for i in range(num+1) if i > 0])
17+

Factorial/Python/factorial_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
File: factorial_test.py
6+
Author: tdoly
7+
Description: the factorial test
8+
'''
9+
10+
import unittest
11+
from factorial import factorial
12+
13+
class TestFactorial(unittest.TestCase):
14+
def template(self, test_list, test_result, method):
15+
for num, result in zip(test_list, test_result):
16+
self.assertEqual(method(num), result, msg="ERROR!factorial(%s) != %s" % (num, result))
17+
18+
def test_factorial(self):
19+
test_list = (0, 1, 2, 3, 4, 5, 10)
20+
test_result = (1, 1, 2, 6, 24, 120, 3628800)
21+
self.template(test_list, test_result, factorial)
22+
23+
if __name__ == '__main__':
24+
unittest.main()

0 commit comments

Comments
 (0)