initial commit with 2024 materials
This commit is contained in:
commit
6fdfdbb8b7
66 changed files with 102457 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()
|
Loading…
Add table
Add a link
Reference in a new issue