0
How can I Find the first negative number in an array?
Let's say I have an array of X=[22, 41, 2, -10, 7, -55, 88, 4] and I want to find the first negative number. How can I do that in python?
4 Answers
+ 1
Karzan
Sort array and get 1st value.
You can do like this also
arr = [22, 41, 2, -10, 7, -55, 84, 4]
min = 0
for i in arr:
if i < 0:
min = i
break
print (min)
+ 1
This is what I did so others can see too.
for val in X:
if val <0:
print(val)
break
+ 1
đ
°đ
š (Challenge Accepted) your second solution is better. Sorting might rearrange the original order. Then you would find the lowest negative number, though not necessarily the first.
0
Nevermind, I solved it