mirror of
https://github.com/onyx-and-iris/grokking-algorithms.git
synced 2024-11-15 17:30:52 +00:00
add summation
This commit is contained in:
parent
3a138acf93
commit
22545b8911
13
chapter4/summation.py
Normal file
13
chapter4/summation.py
Normal file
@ -0,0 +1,13 @@
|
||||
class Number:
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
|
||||
def summation(nums):
|
||||
if len(nums) == 1:
|
||||
return nums[0].value
|
||||
return nums[0].value + summation(nums[1:])
|
||||
|
||||
|
||||
nums = [Number(i) for i in range(0, 100, 3)]
|
||||
print(summation(nums))
|
Loading…
Reference in New Issue
Block a user