Testing Class Material
This commit is contained in:
commit
05b1f6cdd5
85 changed files with 102796 additions and 0 deletions
33
hands_on/factorial/factorial.py
Normal file
33
hands_on/factorial/factorial.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
""" Compute the factorial of a set of numbers stored in a file. """
|
||||
|
||||
def factorial(n):
|
||||
if n == 0:
|
||||
return 1
|
||||
else:
|
||||
return factorial(n-1) * n
|
||||
|
||||
|
||||
def read_data(filename):
|
||||
numbers = []
|
||||
with open(filename, 'r') as f:
|
||||
for line in f:
|
||||
number = int(line)
|
||||
numbers.append(number)
|
||||
return numbers
|
||||
|
||||
|
||||
def compute_factorials_for_list(numbers):
|
||||
factorials = []
|
||||
for number in numbers:
|
||||
result = factorial(number)
|
||||
factorials.append(result)
|
||||
return factorials
|
||||
|
||||
|
||||
def main():
|
||||
numbers = read_data('numbers.txt')
|
||||
factorials = compute_factorials_for_list(numbers)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
100000
hands_on/factorial/numbers.txt
Normal file
100000
hands_on/factorial/numbers.txt
Normal file
File diff suppressed because it is too large
Load diff
13
hands_on/factorial/test_factorial.py
Normal file
13
hands_on/factorial/test_factorial.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
""" Tests for the factorial function. """
|
||||
|
||||
from factorial import factorial
|
||||
|
||||
|
||||
def test_factorial():
|
||||
factorial_cases = [(1, 1),
|
||||
(0, 1),
|
||||
(5, 2*3*4*5),
|
||||
(30, 265252859812191058636308480000000)]
|
||||
|
||||
for n, fact_n in factorial_cases:
|
||||
assert factorial(n) == fact_n
|
Loading…
Add table
Add a link
Reference in a new issue