-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcodechallenge_125.py
More file actions
71 lines (55 loc) · 2.03 KB
/
codechallenge_125.py
File metadata and controls
71 lines (55 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'''
Decorators return a closure. A closure is what is returned by a decorator.
Basically, a decorator takes in a function, adds some functionality and returns it.
Work-in-progress!!!
'''
class Solution:
def sumOfNumbers(self, nums: list[int]) -> int:
def listSum(nums):
if len(nums) == 1:
return nums[0]
else:
return nums[0] + listSum(nums[1:])
# call the helper function
return listSum(nums)
def productOfNumbers(self, nums: list[int]) -> int:
def listProduct(nums):
if len(nums) == 1:
return nums[0]
else:
return nums[0] * listProduct(nums[1:])
# call the helper function
return listProduct(nums)
@staticmethod
def fnc(self, nums: list[int]) -> int:
def listSum(nums):
if len(nums) == 1:
return nums[0]
else:
return nums[0] + listSum(nums[1:])
# call the helper function
return listSum(nums)
@staticmethod
def fnc2(self, nums: list[int]) -> float:
x = self.productOfNumbers(nums)
print(x)
return x/self.sumOfNumbers(nums)
def main():
nums = [1, 2, 3, 4, 5]
print("Test 1:")
print("Given array: '{}'".format(nums))
print("Sum of numbers in the array is {}".format(Solution().sumOfNumbers(nums)))
nums = [1, 2, 3, 4, 5]
print("Test 2:")
print("Given array: '{}'".format(nums))
print("Product of numbers in the array is {}".format(Solution().productOfNumbers(nums)))
nums = [1, 2, 3, 4, 5]
print("Test 3:")
print("Given array: '{}'".format(nums))
print("Sum of numbers in the array is {}".format(Solution.fnc(Solution, nums)))
# nums = [1, 2, 3, 4, 5]
# print("Test 4:")
# print("Given array: '{}'".format(nums))
# print("Product of numbers in the array divided by SumOfNums is {}".format(Solution.fnc2(Solution,nums)))
if __name__ == "__main__":
main()