Generation

generate functionTue, 13 Dec 2022

a list is given arr = [2, 1, 10, 5] Write a function with a for loop that sums all the numbers in the list with each other The result should be this: (2+1)+(2+10)+(2+5)+(1+10)+(1+5)+(10+5) = 54

#create a list arr = [2, 1, 10, 5] def sum_from_list(arr): result = [] for idx, num in enumerate(arr): for idx_2, num_2 in enumerate(arr): if idx_2 > idx: result.append(num + num_2) return result sum_from_list(arr)

Questions about programming?Chat with your personal AI assistant