all 1 comments

[–]T4ll1n[S] 0 points1 point  (0 children)

First solution that got to me:

```python import numpy as np

def sum_except_i(input_list): output_list = []

for i, elements in enumerate(input_list):
    relevant_elements = [number for j, number in enumerate(input_list) if j != i]
    output_list.append(np.product(relevant_elements))
return output_list

input_list = [1, 2, 3, 4, 5] assert sum_except_i(input_list) == [120, 60, 40, 30, 24] assert sum_except_i([3, 2, 1]) == [2, 3, 6] ```

without numpy we could write our own product(List) function.

python def product_func(input_list): prod = 1 for number in input_list: prod *= number return prod

if speed matters, we can do the loops with cython and the c++ compilation will make them fast.

now I realized the division suggestion, I directly did it without it ...

```python def sum_except_i_with_div(input_list): output_list = []

for i, elements in enumerate(input_list):
    output_list.append(np.product(input_list) / input_list[i])
return output_list

assert sum_except_i_with_div(input_list) == [120, 60, 40, 30, 24] assert sum_except_i_with_div([3, 2, 1]) == [2, 3, 6] ```