From 22545b8911594c5988e6b22f1199659c70673007 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sun, 7 Jan 2024 21:19:42 +0000 Subject: [PATCH] add summation --- chapter4/summation.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 chapter4/summation.py diff --git a/chapter4/summation.py b/chapter4/summation.py new file mode 100644 index 0000000..a540f74 --- /dev/null +++ b/chapter4/summation.py @@ -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))