update,
This commit is contained in:
33
max015/CDS2003_assignment02/src/P01.py
Normal file
33
max015/CDS2003_assignment02/src/P01.py
Normal file
@@ -0,0 +1,33 @@
|
||||
'''
|
||||
Draft a python script to solve the following questions. thanks
|
||||
|
||||
# Requirement
|
||||
Please use recursion to define and test a function to calculate the sum of a list of numbers.
|
||||
|
||||
# procedure
|
||||
- let's think it step by step
|
||||
- leave me comment with your code
|
||||
- solve with recursion
|
||||
- write your code in `def list_sum_Recur(num_list):` , with comment `# implementation here`
|
||||
- write a sample function to use it, with comment `# test here`
|
||||
'''
|
||||
|
||||
import sys
|
||||
sys.setrecursionlimit(999999)
|
||||
|
||||
def list_sum_Recur(num_list):
|
||||
# implementation here
|
||||
if len(num_list) == 1:
|
||||
return num_list[0]
|
||||
else:
|
||||
return num_list[0] + list_sum_Recur(num_list[1:])
|
||||
|
||||
# test here
|
||||
def test_list_sum_Recur():
|
||||
print(list_sum_Recur([1, 2, 3, 4, 5]))
|
||||
print( (1+5)*5 / 2)
|
||||
|
||||
print(list_sum_Recur(list(range(1,9999+1))))
|
||||
print( (1+9999)*9999 / 2)
|
||||
|
||||
test_list_sum_Recur()
|
Reference in New Issue
Block a user