Biorąc pod uwagę niepustą listę / tablicę zawierającą tylko nieujemne liczby całkowite takie jak to:
[0, 0, 0, 8, 1, 4, 3, 5, 6, 4, 1, 2, 0, 0, 0, 0]
Wypisuje listę z usuniętymi końcowymi i wiodącymi zerami.
Dane wyjściowe dla tego będą:
[8, 1, 4, 3, 5, 6, 4, 1, 2]
Niektóre inne przypadki testowe:
[0, 4, 1, 2, 0, 1, 2, 4, 0] > [4, 1, 2, 0, 1, 2, 4]
[0, 0, 0, 0, 0, 0] > nothing
[3, 4, 5, 0, 0] > [3, 4, 5]
[6] > [6]
Najkrótszy kod wygrywa
code-golf
array-manipulation
Lamaro
źródło
źródło
Nil
()
/[]
slip()
/Empty
Any
{}
niektóre z nich są niezdefiniowane, niektóre zdefiniowane, ale osobliwe, niektóre, które wpadają na inne listy, tak że nie zwiększają liczby elementów. (Istnieje tyle różnych odmian,Any
ile klas / typów i ról)"0,4,1,2,0,1,2,4,0" => "4,1,2,0,1,2,4"
EDYCJA: Zauważyłem, że wiele języków już to robi.Odpowiedzi:
Galareta , 2 bajty
Kod:
Wyjaśnienie:
Wypróbuj online!
źródło
JavaScript (ES6) 43
Mniej golfa
Test
źródło
f=(a,r=f(a,a))=>r.reverse().filter(x=>a|=x)
ma również 43 bajty.CJam, 13 bajtów
Po wprowadzeniu tablicy.
Dłuższa wersja:
źródło
Pyth, 4 bajty
Próbny:
Od Pytha
rev-doc.txt
:źródło
05AB1E, 4 bytes
Code:
Try it online!
Explanation:
Uses CP-1252 encoding.
źródło
Retina, 12 bytes
The trailing linefeed is significant.
Thanks to @Martin Büttner and @FryAmTheEggman for saving a few bytes.
Try it online
źródło
R, 43 bytes
or as read/write STDIN/STDOUT
This finds the cumulative maximum from the beginning and the end (reversed) string. The
&
operator converts these two vectors to logical one of the same size asx
, (zeroes will always converted toFALSE
and everything else toTRUE
), this way it makes it possible to subset fromx
according to the met conditions.źródło
Haskell, 29 bytes
źródło
Mathematica
3427 bytesThis repeatedly applies replacement rules until such action fails to provide a new output. 7 bytes saved thanks to Alephalpha.
The first rule deletes a zero at the beginning; the second rule deletes a zero at the end of the array.
źródło
#//.{0,a___}|{a___,0}:>{a}&
05AB1E, 4 bytes
Basically trimming leading then trailing zeroes of the input, given as an array.
Try it online !
źródło
Perl, 19 + 1 = 20 bytes
Requires
-p
flag:źródło
s/^0 | 0$//&&redo
?
as in the example - but that won't reduce"0"
..Jelly, 10 bytes
This doesn't use the builtin.
Try it here.
źródło
Perl, 38 bytes
Run with
perl -p
, (3 bytes added for-p
).Accepts numbers on STDIN, one per line; emits numbers on STDOUT, one per line, as a well-behaved unix utility should.
Only treats numbers represented exactly by '0' as zeroes; it would be possible to support other representations with a few more bytes in the regex.
Longer version, still to be run with
-p
:Expanded version, showing interactions with -p flag:
źródło
perl -E
, the-p
flag usually is only counted as one byte, since there's only one extra byte different between that andperl -pE
.Elixir, 77 bytes
l is the array.
Edit:wah! copy/pasta fail. of course one has to import Enum, which raises the byte count by 12 (or use Enum.function_name, which will make it even longer).
źródło
Vitsy, 13 bytes
Vitsy is slowly getting better... (I'm coming for you Jelly. ಠ_ಠ)
This exits with the array on the stack. For readability, the TryItOnline! link that I have provided below the explanation will output a formatted list.
Explanation:
Note that this will throw a StackOverflowException for unreasonably large inputs.
TryItOnline!
źródło
R, 39 bytes
Four bytes shorter than David Arenburg's R answer. This implementation finds the first and last index in the array which is greater than zero, and returns everything in the array between those two indices.
źródło
MATL, 9 bytes
Try it online!
Explanation
źródło
Dyalog APL, 15 bytes
Try it here.
źródło
{⌽⍵/⍨×+\⍵}⍣2
?Ruby,
4944 bytesThanks to manatwork for chopping off 5 bytes with a completely different method!
This just
drop
s the first element of the arraywhile
it's 0, reverses the array, repeats, and finally reverses the array to return it to the proper order.źródło
.drop_while()
based solution would be shorter (if using 2 functions):f=->a{a.drop_while{|i|i<1}.reverse};->a{f[f[a]]}
eval
ugliness:->a{eval ?a+'.drop_while{|i|i<1}.reverse'*2}
.<1
, anyway. Thanks!Vim 16 Keystrokes
The input is to be typed by the user between
i
andesc
, and does not count as a keystroke. This assumes that there will be at least one leading and one trailing zero. If that is not a valid assumption, we can use this slightly longer version: (18 Keystrokes)źródło
i
and<esc>
). In vim golf the golfer starts with the input already in a file loaded the buffer and the cursor in the top left corner, but the user also has to save and exit (ZZ
is usually the fastest way). Then you could do something liked[1-9]<enter>$NlDZZ
(13 keystrokes). NoteN
/n
instead of/<up><enter>
ES6, 51 bytes
t
is set to the index after the last non-zero value, whilef
is incremented as long as only zeros have been seen so far.źródło
Perl 6, 23 bytes
Usage:
źródło
Retina, 11 bytes
Quite simple. Recursively replaces zeroes at beginning and end of line.
Try it online!
źródło
JavaScript (ES6), 47 bytes
Where
a
is the array.źródło
a=>a.join(a="")...
.[14]
will return[1, 4]
.Python, 84 characters
źródło
for i in-1,0:
JavaScript (ES6), 34 bytes
Input and output are in the form of a space-delimited list, such as
"0 4 1 2 0 1 2 4 0"
.źródło
Javascript (ES6) 40 bytes
źródło
PHP,
565452 bytesUses Windows-1252 encoding
String based solution
Run like this:
If your terminal is set to UTF-8, this is the same:
Tweaks
źródło
Python 2,
6967 bytesźródło
for i in-1,0:
[space][space]while
with[tab]while
. And==0
can be<1
. mothereff.in/…PowerShell, 49 bytes
Takes input
$args[0]
and-join
s them together with commas to form a string. We then use the.Trim()
function called twice to remove first the trailing and then the leading zeros and commas. We then-split
the string on commas back into an array.Alternate version, without using conversion
PowerShell, 81 bytes
Since PowerShell doesn't have a function to trim arrays, we define a new function
f
that will do half of this for us. The function takes$a
as input, then loops through each item with a foreach loop|%{...}
. Each iteration, we check a conditional for$_ -or $b
. Since non-zero integers are truthy, but$null
is falsey (and$b
, being not previously defined, starts as$null
), this will only evaluate to$true
once we hit our first non-zero element in the array. We then set$b=1
and add the current value$_
onto the pipeline. That will then continue through to the end of the input array, with zeros in the middle and the end getting added onto the output, since we've set$b
truthy.We encapsulate and store the results of the loop all back into
$a
. Then, we index$a
in reverse order (i.e., reversing the array), which is left on the pipeline and thus is the function's return value.We call the function twice on the
$args[0]
input to the program in order to "trim" from the front, then the front again (which is the back, since we reversed). The order is preserved since we're reversing twice.This version plays a little loose with the rules for an input array of all zeros, but since ignoring STDERR is accepted practice, the program will spit out two (verbose)
Cannot index into a null array
errors to (PowerShell's equivalent of) STDERR and then output nothing.źródło