Tugasnya sederhana: mengkonsolidasikan array int. Konsolidasi array ini terdiri dari:
- Semua instance 0 harus dipindahkan ke akhir array.
- Seharusnya tidak ada 0s antara bilangan bulat bukan nol.
- Semua indeks bukan nol harus mempertahankan pesanan mereka.
Tantangan
Konsolidasi array dalam jumlah byte terkecil.
Anda mengkonsolidasikan array dengan panjang acak dengan ukuran hingga maks bahasa Anda dengan bilangan bulat acak. Masukan mungkin merupakan cara alami untuk bahasa Anda.
Contohnya
Memasukkan
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
Keluaran
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
Memasukkan
-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
Keluaran
-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
Kode Contoh (Jawa)
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+" ");
}
}
code-golf
array-manipulation
Addison Crump
sumber
sumber
Jawaban:
Pyth, 3 bytes
Explanation:
Try it here.
sumber
Q
can be implicit at the end of any Pyth script (assuming it's outside of a lambda, which this is), making this 2 bytes.Jelly, 3 bytes
Sorts the list by the logical NOT of its values. Try it online!
How it works
sumber
¬Þ
, even!Octave, 18 bytes
sort()
takes too many bytes. I'll just use logical indexing.Examples on ideone.
sumber
R,
292321 bytesAs noted by MarcoBreitig, we can shorten it to 21 bytes if we don't need to provide it as a function:
Previous versions:
The function takes a vector as input and orders by the logical vector that results from negating the input.
Original answer:
The function takes a vector as input and the concatenates (
c()
) the non-zero values and then the zero-values.sumber
Retina, 15
Simple repeated regex substitution:
Try it online.
sumber
ES6, 23 bytes
It used to be the case that
sort
wasn't stable, in which case you needed 41 bytes:sumber
Python byte code (2.7.9), 252 bytes, 33 opcodes, 0.0228 seconds
This was build when the contest was still a fastest-code contest
Opens a file in the current directory called
'SourceArray'
for useThe
co_code
(The actual codey bit)Or a .pyc file version
03F3
You can try to compile my source code yourself using my library on github. I just posted a commit to it that allowed comments so I hope this is still competing as far as fastest-code goes ;)
Roughly equivalent to
sumber
Python, 32 bytes
Takes argument as any iterable (list, tuple, etc.). Thanks to @xnor for teaching me a new trick!
sumber
key=0..__eq__
(yes, two dots)."abc".__eq__("abc")==True
. It's what's called when you do"abc"==
. For reasons, Python integers don't have it but floats do, and since0. == 0
, we can substitute its equality operator., which is0..__eq__
..__eq__
method, but the double dots were confusing me. I didn't catch that the first one was the decimal point in a float literal.Matlab: 21 bytes
Prints nonzero elements first, then concatenates with zero elements
@(a)____
create an anonymous function with one input argumenta
[___,___]
concatenates horizontally vectors inside brackets, separated by commasa(a~=0)
returns vector with all nonzero elements of vectora
a(a==0)
returns vector with all zero elements of vectora
sumber
Haskell, 26 bytes
Take all non-zero numbers followed by all zeros. Filtering constants (here:
0
) is quite short when using a list comprehension:[0|0<-x]
.sumber
Zsh, 22 bytes
(input passed as arguments to the script/function (
$@
aka$argv
array), output on stdout as space separated list, newline terminated)<<< string
: here-string here passed as stdin to the$NULLCMD
command (cat
by default).${@:#0}
$@
except elements being 0.${(M)@:#0}
reverse of the aboveThat assumes (like several other answers here) that zeroes in the input are all expressed as
0
(no00
nor0x0
nor36#0
).sumber
Javascript,
525451 bytessumber
Mathematica, 14 bytes
sumber
Sort[#!=0&]
should be enough.APL: 8 bytes
a~0 remove zeros from a (read "a without 0")
(⍴a) original length of a (read "shape of a")
↑ pad a without zeros to a's original length
Try it in http://ngn.github.com/apl/web/index.html
Test data: a←1 0 1 2 3 4 0 1 0 0 0 0 1 2 3 4 5
sumber
⍴↑{⍵~0}
and that's even shorter.Java 7, 78 bytes
I'm not sure why the other Java entries are using strings. If you want to filter an integer array, it seems best to use an integer array. This modifies the input in place by keeping two indices, then just filling the remaining slots with zeros.
sumber
o
withint c=0,o;for(o:a)...
. You can also convert to Java 8 lambda syntax:a->{int c=0;for(int o:a)a[o==0?c:c++]=o;for(;c<a.length;a[c++]=0);}
and state that it expects input as an int array.Common Lisp, 46 bytes
Sort the array so that for each couple (a,b), we have a < b if b is zero. When neither a < b or b < a, the sort is stable: the original order between elements is retained.
I also tried with adjust-array and remove, but this was too long:
sumber
PHP,
73717052494846 bytes - BIG thanks to Ismael Miguelsumber
$v==0
can be replaced with!$v
, saving you 2 bytes.foreach($argv as$v)$v?$f.=" $v":$b.=" $v";echo$f.$b;
. It is.... some bytes, I don't know...foreach($a as$v)$v?print("$v "):$b.="$v ";echo$b;
for a more neat way, that looks exactly the sameBash + GNU utilities, 23
Assumes input is newline-separated entries in a file called
a
. Score includes +1 for this filename.sumber
Perl 5, 26 bytes
23 plus three for
-an
(-E
is free)Thanks to Dennis for reminding me of
-a
, saving two bytes.sumber
CJam, 6 bytes
An anonymous function. Sort using “whether or not an element is zero” as a key.
sumber
MATL, 7 bytes
Try it online!
sumber
Seriously, 12 bytes
Try it online!
Explanation:
sumber
Swift, 13 bytes
sumber
Perl6, 11 bytes
Produces a Block - which can be called on an array:
Although it would be more natural (and shorter) to write:
How it works: if the perl6 sort routine is called with a block which accepts only one argument, the list elements are sorted according to
by($a) cmp by($b)
. In this case, the block is!*
, i.e. a negation of the whatever operator.I notice that:
sumber
TeX (Plain format), 160 bytes
Make the
0
character active (that is, make the interpreter process it as a command), then define that command to skip the character and increment a counter. At the end of the string, print as many zeros as were counted.Save this as
zero.tex
and give the input through the command line with this command:(Newlines added for clarity)
sumber
J, 4 bytes
Explanation:
The sort function in J is guaranteed to be stable by the specification.
Alternative solution, 6 bytes:
sumber
Straw,
3029 bytesUse the CP437 encoding
Explanation
Try it online! (The added code is to test all test cases)
sumber
JavaScript ES6, 16 bytes
Works on firefox
sumber
Ruby, 25 bytes
Try it online!
sumber
05AB1E,
1514 bytesCode:
Explanation:
Uses CP-1252 encoding. Takes an array like this:
sumber