I'm quite new to Python, and doing a Udemy course on coding interview challenges. This one is a linear search through a 2D array in Python. If I enter 11 as an argument, it finds it at position [0][0] - which is correct. However, if I change the argument to another number that is in the array, it returns 'The element has not been found.' I am pretty sure that I have entered the same code as the instructor, but I must have done something wrong, as his works and mine doesn't! I would be really grateful if someone could help me find my error! Thanks so much!
Quick edit to say thanks to everyone who has taken the time to help me with this. So great to have the support! Have a great day!
twoDArray = np.array([[11, 15,10, 6], [10, 14, 11, 5], [12, 17, 12, 8], [15, 18, 14, 9]])
print(twoDArray)
def searchTDArray(array, value):
for i in range(len(array)): # Return number of rows.
for j in range(len(array[0])): # Return number of columns.
if array[i][j] == value:
return 'The value is located at index' +str([i])+str([j])
else:
return 'The element has not been found.'
print(searchTDArray(twoDArray, 11))
returnoutside the loops.'The element has not been found.'only once when you have visited all the indexes, that is outside both theforloop.[0][0]index, even if it is present in any other index, it will return as not present.