Програмата ми изкарва решението едно към едно с примера във ВС - код, предполагам аз не съм рабрал нещо. И затова идвам за запитване къде е проблема понеже не ми дава точки.
Array Sort
Given an array integers, write a program that moves all of the zeroes to the end of it, while maintaining the relative order of the non-zero elements.
Input
Read from the standard input:
- There is one line of input, containing N amount of integers, seperated by a comma (",")
Output
Print to the standard output:
- There is one line of outpit, containing the sorted integers, seperated by a comma (",")
Constraints
- 5 <= N <= 1000
Sample Tests
Input
0,1,0,3,12
Output
1,3,12,0,0
Input
0,0,0,5,0,3,2,3
Output
5,3,2,3,0,0,0,0
let input = ['0','1','0','3','12'] ;
let print = this.print || console.log;
let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
let result = [];
let n = input.length;
let j = 0;
for (let i = 0; i < n; i++) {
if (input[i] != 0) {
swap(input, j, i);
j++;
}
}
for (let i = 0; i < n; i++) {
result.push(input[i]);
}
function swap(input, a, b) {
let temp = input[a];
input[a] = input[b];
input[b] = temp;
}
print(result.join(","));