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
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.and
and
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
prints
note that you are adding 0 to the list, i doubt that this is ok.