0

I'm struggling to figure out exactly what this assignment is asking me to output. I built this python function but it isn't doing what the professor wants it to do. I'm very confused.

Here is the instructions:

define the function extract_negatives: The first argument, xs, contains integers. We will be removing any negative integers we find in xs, and appending them to the second argument, new_home. A reference to the list that received negatives must be returned. When no second argument is given, a list of all the extracted negatives should be created and returned.

Figuring out the signature of this function is part of the task. Careful: What default value do you want to use for new_home? What happens when we call the function multiple times in the same coding session? (try it out) Go ahead and use methods like .insert(), .pop(), .append()

You might need to iterate and update the list at the same time. Hint: if you need to traverse a list from front to back, but you don't always want to go to the next index location, while loops can be very useful – we get to control when (which iteration) to step forward.

Here are examples given:

input: xs = [1,-22,3,-44,-5,6,7] extract_negatives(xs) yeilds: [-22, -44, -5] #return a list of negatives xs
[1, 3, 6, 7] #remove negatives from xs

Here is the function I built:

def extract_negatives(xs,new_home):
    new_home=[]
    for num in range(len(xs)):
        if xs[num] <0:
            new_home.append(xs[num])
    return new_home 

I have tried asking the professor but it's been a few days with no response. Mayybe you can help make sense of what is being asked?

kg123
  • 47
  • 9

2 Answers2

1

What I understood is that you have two scenarios. First only one argument is passed, so you need to create the new_home and return it. Second is that you get the new_home as the second argument and you just append the negatives to it.

Besides, you need to remove the negatives from xs which I do not think your code is doing now.

For variable argument size look at here: Can a variable number of arguments be passed to a function?

Community
  • 1
  • 1
Peyman Mahdian
  • 522
  • 4
  • 15
  • I don't understand how to make it work IF the second argument is passed. If i include it, won't it affect the whole thing? – kg123 Mar 25 '17 at 23:07
  • You are still not removing the negatives from xs. And for variable argument size you can look at this: http://stackoverflow.com/questions/919680/can-a-variable-number-of-arguments-be-passed-to-a-function – Peyman Mahdian Mar 25 '17 at 23:10
  • Okay went to the link and read it. Do I need to use def extract_negatives(args)? I know how arguments work ish. I don't understand how I can input an argument and not use it if that makes sense – kg123 Mar 25 '17 at 23:14
1

I think this might be the main thing you need to know, if it's not too late. This bit of code shows how to determine how many positional parameters have been passed and how to determine their values. In your case, if there's only one parameter then you need to create the result list anew, if two then you must append values to the list in the second parameter.

>>> def extract_negatives(*args):
...     print (len(args))
...     
...     if len(args)==1:
...         print (args[0])
...     elif len(args) == 2:
...         print (args[0], args[1])
...     else: raise RuntimeError('Incorrect number of parameters')
...         
>>> extract_negatives([1,2,3])
1
[1, 2, 3]
>>> extract_negatives([1,2,3], [1,0])
2
[1, 2, 3] [1, 0]
>>> extract_negatives([1,2,3], [1,0], [5,6])
3
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "<interactive input>", line 8, in extract_negatives
RuntimeError: Incorrect number of parameters
Bill Bell
  • 21,021
  • 5
  • 43
  • 58