-3

The statement says:

Write a nonrecursive (negative) function which given a list of integers (possibly disordered) function returns the same list with negative numbers to positive head and back (regardless of the order between them). This algorithm can be solved in the form requested a similar strategy (though a bit more simple) partition in quicksort.

I put this code:

def negatius(a):

    fin = len(a) - 1
    i = 0
    b = [i]

    for i in range(len(a)):
        if a[i] < 0:
            b[fin] = a[i]
            i += 1

        else:
            b[fin] = a[i]
            fin += 1

    print "La llista és",b[fin]


a=[1,-2,3,-4,-3,5,6]
negatius(a)

And appears an error: local variable 'i' referenced before assignment. I dont understand this

David
  • 27
  • 1
  • 7
  • Don't use `int` as a variable name, you hide the built in `int` – Daenyth Nov 19 '14 at 18:33
  • Ok but my int += 1 is removed – David Nov 19 '14 at 18:36
  • 1
    I would suggest going through [Google's Python Class](https://developers.google.com/edu/python/) or a similar tutorial to better understand Python basics. Also, please don't change your code while your question is being answered. Your current code includes the fix for the error you asked about, so now the question doesn't make any sense. – skrrgwasme Nov 19 '14 at 19:06
  • Could you give some examples of possible inputs and the correct outputs? It's not entirely clear to me what the code should do. – Bill Lynch Nov 19 '14 at 19:10
  • @skrrgwasme, of course it is ok to alter code. – vikingosegundo Nov 19 '14 at 19:13
  • @Christian: what is a negative function? – vikingosegundo Nov 19 '14 at 19:13
  • @vikingosegundo Perhaps I phrased it too strongly. Yes editing code is sometimes appropriate, but not in a way that completely invalidates the question. The OP is asking about an undefined variable error. The new version of the code throws an entirely different error. – skrrgwasme Nov 19 '14 at 19:16
  • I'd say it is rather ok to do that than create several question for this trivial thing. – vikingosegundo Nov 19 '14 at 19:18
  • 1
    @vikingosegundo I suppose that's where we disagree. Your answer below is a perfect example of why [chameleon questions](http://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions) should be discouraged. Your answer below is a good example of fixing the OPs broken code, but if some future visitor opens this question because they googled "local variable referenced before assignment", your answer does nothing for them. Each SO question should have a well-defined focus. Opening new questions when the focus shifts helps keep everything organized. – skrrgwasme Nov 19 '14 at 19:27
  • That's why we can fix other people's questions, – vikingosegundo Nov 19 '14 at 20:51

1 Answers1

1

You are incrementing fin from the highest index on, and use it to access elements in lists with this index. that can't be right, as these indices do not exist.

b[fin] = a[i]

and

b[fin] = a[i]
fin += 1

and

print "La llista és",b[fin]

Also in if and else you are append to the list. That doenst make much sense too. You should append once and prepend in the other case

def negatius(a):
    fin = len(a) - 1
    i = 0
    b = [i]
    for i in range(fin):
            if a[i] < 0:
                    b = [a[i]] + b  # prepend a to b
            else:
                    b += [a[i]]     # append a to b

    print "La llista és",b

a=[1,-2,3,-4,-3,5,6]
negatius(a)

prints

La llista és [-3, -4, -2, 0, 1, 3, 5]

note that you are adding 0 to the list, i doubt that this is ok.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • The element 0 aren't in my list. The element that have to appears is 6. I try to take out -1 in variable fin but 0 appears in my output i dont want this, how can i take out the 0? – David Nov 19 '14 at 19:22
  • Why do u add it in the first place? – vikingosegundo Nov 19 '14 at 20:50