-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepth_weighted_sum.py
More file actions
executable file
·55 lines (35 loc) · 1.13 KB
/
Copy pathdepth_weighted_sum.py
File metadata and controls
executable file
·55 lines (35 loc) · 1.13 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
def depth_weighted_sum(nested_list):
def solve(current_items, depth):
# base case: empty list
if not current_items:
return 0
head = current_items[0]
tail = current_items[1:]
if isinstance(head, list):
values = solve(head, depth + 1)
else:
values = head * depth
return values + solve(tail, depth)
return solve(nested_list, 1)
# base case:
# recursive step
# accumulator
# test cases
# dealing with arbitrarily deep nested structures, which recursion excels at
# return the sum of each integer, multipled by its depth
nested = [1, [2, 3], 4]
print(depth_weighted_sum(nested))
# depth 1: 1, 4
# depth 2: 2, 3
# result = (1 * 1) + (2 * 2) + (3 * 2) + (4 * 1) = 15
nested = [[1, 1], 2, [1, 1]]
print(depth_weighted_sum(nested))
# depth 1: 2
# depth 2: 1, 1, 1, 1
# result = (2 * 1) + (1 * 2) + (1 * 2) + (1 * 2) + (1 * 2) = 10
nested = [1, [4, [6]]]
print(depth_weighted_sum(nested))
# depth 1: 1
# depth 2: 4
# depth 3: 6
# result = (1 * 1) + (4 * 2) + (6 * 3) = 27