La tarea es simple: consolidar una serie de entradas. La consolidación de esta matriz consta de lo siguiente:
- Todas las instancias de 0 deben moverse al final de la matriz.
- No debería haber ceros entre los enteros distintos de cero.
- Todos los índices distintos de cero deben conservar su orden.
Reto
Consolidar una matriz en la menor cantidad de bytes.
Está consolidando una matriz de longitud aleatoria con un tamaño hasta el máximo de su idioma con enteros aleatorios. La entrada puede ser cualquier forma natural para su idioma.
Ejemplos
Entrada
0 5 8 8 3 5 1 6 8 4 0 3 7 5 6 4 4 7 5 6 7 4 4 9 1 0 5 7 9 3 0 2 2 4 3 0 4 8 7 3 1 4 7 5 1 2 1 8 7 8 7 7 2 6 3 1 2 8 5 1 4 2 0 5 0 6 0 3
Salida
5 8 8 3 5 1 6 8 4 3 7 5 6 4 4 7 5 6 7 4 4 9 1 5 7 9 3 2 2 4 3 4 8 7 3 1 4 7 5 1 2 1 8 7 8 7 7 2 6 3 1 2 8 5 1 4 2 5 6 3 0 0 0 0 0 0 0 0
Entrada
-1 -7 -6 5 1 -5 -2 7 -3 -8 0 8 9 1 -8 -1 6 -4 1 -2 1 -7 5 4 -6 7 -3 9 8 3 -1 0 -5 -7 3 8 1 1 3 -3 -2 -2 0 -7 0 -4 8 6 -3 6 0 5 3 2 2 2 -2 -7 -3 9 -1 6 0 6 -7 9 4 -2 8 -8 -4 1 -8 4 3 7 3 5 1 0 3 3 7 -1 -5 1 -3 4 -7 0 3 2 -2 7 -3 0 0 2 -5 8 -3 -2 -7 -5 7 -3 -9 -7 5 8 -3 9 6 7 -2 4 7
Salida
-1 -7 -6 5 1 -5 -2 7 -3 -8 8 9 1 -8 -1 6 -4 1 -2 1 -7 5 4 -6 7 -3 9 8 3 -1 -5 -7 3 8 1 1 3 -3 -2 -2 -7 -4 8 6 -3 6 5 3 2 2 2 -2 -7 -3 9 -1 6 6 -7 9 4 -2 8 -8 -4 1 -8 4 3 7 3 5 1 3 3 7 -1 -5 1 -3 4 -7 3 2 -2 7 -3 2 -5 8 -3 -2 -7 -5 7 -3 -9 -7 5 8 -3 9 6 7 -2 4 7 0 0 0 0 0 0 0 0 0 0
Código de ejemplo (Java)
public class Consolidate {
public static void main(String[] args) throws Exception {
int[] toConsolidate = new int[args.length];
for (int i=0; i<args.length; i++){
toConsolidate[i]=Integer.parseInt(args[i]);
}
for (int i=0; i<toConsolidate.length; i++) {
for (int k=0; k<toConsolidate.length-1; k++) {
if (toConsolidate[k] == 0){
toConsolidate[k] = toConsolidate[k+1];
toConsolidate[k+1] = 0;
}
}
}
for (int i:toConsolidate)
System.out.print(i+" ");
}
}