Generation

generate functionTue, 13 Dec 2022

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

arr = [2,1,10,5] def sum(arr): i = 0 res = 0 while i < len(arr): j = i + 1 while j < len(arr): res += arr[i] + arr[j] j += 1 i += 1 return res sum(arr)

Questions about programming?Chat with your personal AI assistant