Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis+ 2
Error al borrar Listas / Error deleting lists
Al Eliminar los elementos que se encuentren en posiciĆ³n mayor a mitad del arreglo y que sea negativo. "Por que no borra el -2 ??? Remove items that are in the highest position in the middle of the array and are negative. Why not delete -2 https://code.sololearn.com/cv9bJWjasDWZ
7 Respostas
+ 5
So much efforts put in to have english in your replies as well! you guys are awesome!
+ 3
Aaaah, sĆ, culpa mĆa. Pero insisto en que no deberĆas recorrer un arreglo que se estĆ” modificando, ya que "pos" se estĆ” saltando algunos valores. Prueba esto:
---------
Aaaah, yes, my fault. But I insist that you should not traverse an array that's being modified, as "pos" skips some values. Try this:
# Eliminar los elementos que se encuentren en posiciĆ³n mayor a mitad del arreglo y que sea negativo
# Remove items that are in the highest position in the middle of the array and are negative
arreglo = [10,-19,-15,10,7,6,-78,9,-5,-2]
pos = 0
mitad = len(arreglo)/2
arr=[]
for valor in arreglo:
if not(pos > mitad and valor < 0):
arr+=[arreglo[pos]]
pos = pos + 1
print (arreglo)
print (arr)
+ 3
el ejercicio plantea eliminar los nĆŗmeros negativos que estĆ©n de la mitad hacia adelante ! porque al eliminar un valor las posiciones del arreglo se re-ordenan
es decir al eliminar el -78 en la posiciĆ³n 6 el valor siguiente toma esa posiciĆ³n acortando la longitud y cuando se llaga la variable pos=8 esta no existe ya que se a corrido el -2 a la posiciĆ³n 7!
+ 3
la teorĆa de ClĆ”udio es la misma que yo concluĆ las posiciones cambian y por eso no elimina el -2!
+ 2
No borra -2, pero tampoco 9 ni 6, que tambiĆ©n estĆ”n en la segunda mitad del arreglo. En general, es una mala prĆ”ctica (conduce a error) tener un bucle sobre un arreglo (en el ejemplo, "for valor in arreglo") y, dentro del bucle, modificar algo que haga alterar el propio arreglo. DeberĆas intentarlo recorriendo una copia del arreglo original.
-----------------------
It doesn't remove -2, but neither 9 nor 6, which belong also to the 2nd part of the array. Generally speaking, it's a bad practice (as it can lead to an error) to have a loop over an array (in the example, "for valor in arreglo") and, inside the loop, modify something that alters the array itself. You should try looping over a copy of the original array.
+ 1
El 9 y el 6 no se debe borrar, son positivos, el -2 es negativo y borra el -78 y el -5
The 9 and 6 should not be deleted, they are positive, the -2 is negative and delete the -78 and -5
0
yo pienso que no elimina el -2 ya que el codigo indica que el comienza a eliminar de la mitad hacia delante y al momento de eliminar el -2 cambia de posicion lo que hace que no se pueda eliminar.