-1

Write a function find_negatives that takes a list l of numbers as an argument and returns a list of all negative numbers in l. If there are no negative numbers in l, it returns an empty list.

def find_negatives(l):
    l_tmp = []
    for num in l:
        if num < 0:
            l_tmp.append(num)
    return l_tmp

#Examples
find_negatives([8, -3.5, 0, 2, -2.7, -1.9, 0.0])     # Expected output: [-3.5, -2.7, -1.9]
find_negatives([0, 1, 2, 3, 4, 5])                   # Expected output: []

This is part I am having trouble with below:

Write a function find_negatives2 that works the same as find_negatives with the following two additions:

If l is not a list, it returns the string "Invalid parameter type!" If any of the elements in l is neither an integer nor a floating pointer number, it returns the string "Invalid parameter value!"

What I have so far

def find_negatives2(l):
    l_tmp1 = []
    if type(l) != list:
        return "Invalid parameter type!"
    else:
       for num in l:

Examples:
find_negatives2([8, -3.5, 0, 2, -2.7, -1.9, 0.0])      # Expected output: [-3.5, -2.7, -1.9]
find_negatives2([0, 1, 2, 3, 4, 5])                    # Expected output: []
find_negatives2(-8)                                    # Expected output: 'Invalid parameter type!'
find_negatives2({8, -3.5, 0, 2, -2.7, -1.9, 0.0})      # Expected output: 'Invalid parameter type!'
find_negatives2([8, -3.5, 0, 2, "-2.7", -1.9, 0.0])    # Expected output: 'Invalid parameter value!'
find_negatives2([8, -3.5, 0, 2, [-2.7], -1.9, 0.0])    # Expected output: 'Invalid parameter value!'

I am not sure how to proceed. I am not sure how to check each type within the list

PythonNewb
  • 31
  • 5

3 Answers3

1

You're on the right track; you just need to make a loop for type comparisons:

# (...)
else:
    for num in l:
        if type(num) not in (int, float):
            return "Invalid parameter type!"
        # (...)
Rodrigo Rodrigues
  • 7,545
  • 1
  • 24
  • 36
QWERTYL
  • 1,355
  • 1
  • 7
  • 11
0

You can try code below:

def find_negatives2(l):
    l_tmp1 = []
    if not isinstance(l, list):
        return "Invalid parameter type!"
    else:
       for num in l:
         if not (isinstance(num, float) or isinstance(num, int)):
           return "Invalid parameter value!"
         else:
           if num < 0:
             l_tmp1.append(num)
    return l_tmp1
assert find_negatives2([8, -3.5, 0, 2, -2.7, -1.9, 0.0])      == [-3.5, -2.7, -1.9]
assert find_negatives2([0, 1, 2, 3, 4, 5])                    == []
assert find_negatives2(-8)                                    == 'Invalid parameter type!'
assert find_negatives2({8, -3.5, 0, 2, -2.7, -1.9, 0.0})      == 'Invalid parameter type!'
assert find_negatives2([8, -3.5, 0, 2, "-2.7", -1.9, 0.0])    == 'Invalid parameter value!'
assert find_negatives2([8, -3.5, 0, 2, [-2.7], -1.9, 0.0])    == 'Invalid parameter value!'

All assertions will be validated.

Explanation

isinstance checks whether its first argument is an instance of its second argument or not. Using this function, you can check any variable type. Please note that using type(l) != list is not a good approach to check variable's type. If you are interested to know why, this link might help you.

TheFaultInOurStars
  • 3,464
  • 1
  • 8
  • 29
-1

You can achieve what you are looking for with:

def find_negatives(l):
    if type(l) != list or any(str(x).isnumeric() for x in l) == False:
        return 'Invalid parameter type!'
    l_tmp = []
    for num in l:
        if num < 0:
            l_tmp.append(num)
    return l_tmp

All you have to do is check whether the input is a list and if any element of the input is not a number.

  • hmmm... converting a value to text, to then check if it would make a valid number, doesn't sound like a great deal to me. Furthermore, as per the problem statement, the string "-1" should not be a valid number, because it is a string. – Rodrigo Rodrigues Feb 20 '22 at 23:26
  • `any(str(x).isnumeric() for x in l)` is equal to `False` if **none** of the strings you check is numeric. You should be checking `if not all` of them are numeric (if you want to do it this way). – khelwood Feb 20 '22 at 23:32