human
Sum of all odd length subarrays | Function that finds the sum of all the element of subarrays of odd length.
Stores the sum.
Size of array.
Traverse the given array arr [ ].
Add to the sum for each contribution of the arr [ i ].
Return the final sum.
Given array arr [ ].
Function call"
How can the above be solved in Python?
assistant
#Here is code to sum of all odd length subarrays and the other steps required.
 def OddLengthSum ( arr ) :
	Sum = 0
	l = len ( arr )
	for i in range ( l ) :
		Sum += ( ( ( ( i + 1 ) * ( l - i ) + 1 ) // 2 ) * arr [ i ] )
	return Sum
arr = [ 1 , 5 , 3 , 1 , 2 ]
print ( OddLengthSum ( arr ) )