I need to check if there are any negative numbers in a list and if there are I have to return False.
def check_negative(vals):
if vals[0] < 0:
return False
else:
...
return True
So if my list is --> [1, 0, -7, 8, 21] it should return False and it should only return True if all numbers are positive.
edit: After receiving feedback I realized doing it recursively isn't efficient so I have edited my code.
def no_negatives(numbers):
positive = []
negative = []
for num in numbers:
if num >= 0:
positive.append(num)
else:
negative.append(num)
if len(negative) > 0:
return False
else:
return True
I got my expected output. Thanks everyone.
CodePudding user response:
Recursive
As others have pointed out you should not use recursion for this kind of thing in a production application as it consumes much more resources for the call stack (which is also limited in Python), but if you want to use it for training purposes you can do it like this.
You need two simple base cases here:
- List is empty: return
True
as there cannot be negative values in an empty list - List has at least one value and the value is smaller than 0: return
False
as then we have found a value which is negative in the array
In all other cases we just call the function recursively and return the result.
lst1 = [1, 0, -7, 8, 21]
lst2 = [1, 0, 234, 2342, 23423, 32, 324, 8, 21]
def check_has_negative_rec(arr):
"""
Checks recursively whether a list contains a negative element or not.
:param arr: list to check
:return: True, if list contains negative element, False otherwise
"""
if len(arr) == 0:
return False
elif arr[0] < 0:
return True
else:
return check_has_negative_rec(arr[1:])
print(check_has_negative_rec(lst1))
print(check_has_negative_rec(lst2))
Expected output:
True
False
Iterative
Here how you would do it the iterative way.
lst1 = [1, 0, -7, 8, 21]
lst2 = [1, 0, 234, 2342, 23423, 32, 324, 8, 21]
def check_has_negative_iter(arr):
for no in arr:
if no < 0:
return True
return False
print(check_has_negative_iter(lst1))
print(check_has_negative_iter(lst2))
Built-in
You can also use Python built-in any()
which would make your code more pythonic.
lst1 = [1, 0, -7, 8, 21]
lst2 = [1, 0, 234, 2342, 23423, 32, 324, 8, 21]
def check_has_negative_builtin(arr):
return any(no < 0 for no in arr)
print(check_has_negative_builtin(lst1))
print(check_has_negative_builtin(lst2))
All three will give you the same result.
CodePudding user response:
- Return
True
if the list is empty - Check if the first element is negative, return
False
if so - Otherwise, apply the same process to the rest of the elements
Implementation:
def all_positive(lst):
if not lst:
return True
if lst[0] < 0:
return False
rest = lst[1:]
return all_positive(rest)
This is not performant, I'm assuming you want to use recursion for educational reasons.
You can get around creating list slices by using iterators (this won't make the recursion fast, though).
def all_positive(lst):
lst = iter(lst) # NOOP if lst already iterator
try:
if next(lst) < 0:
return False
except StopIteration:
return True
return all_positive(lst)
CodePudding user response:
The is no recursion involved, you just need list processing.
def check(my_list):
for x in my_list:
if x < 0:
return False:
return True
Also you should share you attempt, not simply ask for a solution. It's about learning not having someone else do your homework.