Page 1 of 1

Given two numpy 2-D positive integer arrays of the same dimension, a1 and a2, return the sum of all elements in a1 that

Posted: Tue Jul 12, 2022 8:11 am
by answerhappygod
Given two numpy 2-D positive integer arrays of the samedimension, a1 and a2, return the sum of all elements in a1 that areelement-wise greater than those in a2. If none of the elements ina1 is greater than the corresponding ones in a2, return 0. See afew examples below.
Given Two Numpy 2 D Positive Integer Arrays Of The Same Dimension A1 And A2 Return The Sum Of All Elements In A1 That 1
Given Two Numpy 2 D Positive Integer Arrays Of The Same Dimension A1 And A2 Return The Sum Of All Elements In A1 That 1 (16.13 KiB) Viewed 17 times
This is what I have so far. I can't figure out where Iwent wrong
from typing import Text
def sum_greater(a1, a2):
"""Sum of all elements in a 2D array that are element-wise greater
than those in another array
Args:
a1: a numpy 2D positive integer array
a2: a numpy 2D positive integer array that has the same dimension as a1
Returns:
sum of all elements in a1 that are element-wise greater than those in a2;
return 0 if none of the elements in a1 is greater than the corresponding
ones in a2
"""
# start your code here
result = [[0,0,0],
[0,0,0]]
for i in range(len(a1)):
for j in range(len(a1[0])):
if a1 > a2 and a1[j] > a2[j]:
result[j]=a1[j]+a2[j]
else:
return 0
for r in result:
print(r)
-----
#testing function:
a = np.array([[1,2,3],[4,5,6]])
b = np.array([[6,5,4],[3,2,1]])
sum_greater(a, b)
Input: a1 np.array([[1,2,3],[4,5,6]]) np.array([[6,5,4],[3,2,1]]) Ouput: 15 Input: al np.array([[1,2,3],[4,5,6]]) a2 = np.array([[9,9,9],[9,9,9]]) Ouput: 0