g | x | w | all
Bytes Lang Time Link
032Tcl171115T183651Zsergiol
002Vyxal S220724T005001ZnaffetS
037Wolfram Language Mathematica190812T205156ZRoman
070PHP190812T204547Z640KB
008Brachylog190812T202857ZUnrelate
002Stax180411T155407Zrecursiv
053JavaScript Node.js180411T145427Zl4m2
106Java 10180411T133749ZKevin Cr
029R180411T144222ZGiuseppe
005Pyth180411T132740Zhakr14
005Japt170821T140012ZShaggy
004APL Dyalog170821T134740ZAdá
079Prolog120529T142121Zmarinus
045Python120529T035407Zboothby
042Scala120529T022128ZPrince J
020Sed120528T070839ZPrince J
029bash120528T070928Zuser unk
nan120529T005451Zuser unk
013Perl120528T051737Zbreadbox
111Python120528T102021Zugoren
014Perl120528T124234ZToto
049Python 3120528T111435ZClueless
045C120528T080137ZAlexande
015Golfscript120528T071539ZCristian
nan120528T085621Zmarinus
012K120528T085440Ztmartin
038Ruby120528T050529ZVentero

Tcl, 32 bytes

proc D L {join [join [join $L]]}

Try it online!

Assuming 4-deep is something like {{{{5}}}}. As there is no example of such thing on test cases it may be something like {{{5}}}; if it is I can make my code shorter!


# [Tcl], 47 bytes
proc D L {concat {*}[concat {*}[concat {*}$L]]}

Try it online!


# [Tcl], 66 bytes
proc D L {lsearch -al -inl -not "[string map {\{ "" \} ""} $L]" 0}

Try it online!

Vyxal S, 2 bytes

fꜝ

Try it Online!

Wolfram Language (Mathematica), 37 bytes

Flatten/*DeleteCases[0]/*StringRiffle

Try it online!

PHP, 70 bytes

function($a){array_walk_recursive($a,function($a){echo$a?"$a ":'';});}

Try it online!

This isn't going to be the shortest (nor the longest), but figured it would be a chance to get to use array_walk_recursive(), which until today I can't think of ever having a use for it! At least it should be able to handle an arbitrary level deep nested lists.

Brachylog, 8 bytes

ċ∋↰|ℕ₁ẉ⊥

Try it online!

Takes input through the input variable and prints the output separated by newlines. Ordinarily I'd complain about the output format, but it actually saved me a byte in a way I might not have thought to otherwise--putting ẉ⊥ on the end is shorter than wrapping it in {}ᶠ.

ċ           If the input is a list,
 ∋          pick some element of it
  ↰         and recur with it as the input.
   |        Otherwise, if the input
    ℕ₁      is a natural number,
      ẉ     print it with a trailing newline
       ⊥    then trigger backtracking.

If the list items aren't restricted to being non-negative integers:

Brachylog, 11 bytes

ċ!∋↰|0!⊥|ẉ⊥

Try it online!

Stax, 2 bytes

$f

Run and debug it

$ is flatten. f is filter out falsey values and print on separate lines.

JavaScript (Node.js), 53 bytes

function(x){return(','+x+',').replace(/,(0,)*/g,' ')}

Try it online!

Java 10, 106 bytes

void c(Object i){for(var o:(Object[])i)try{System.out.print((int)o>0?o+" ":"");}catch(Exception e){c(o);}}

Input as nested Object[], output printed to STDOUT.

Try it online.


46 bytes

s->s.replaceAll("[^0-9 ]","").replace(" 0","")

Input and output both as String.

Try it online.

R, 29 bytes

function(l)(x=unlist(l))[!!x]

Try it online!

unlist converts the list to an atomic vector recursively, so we just need to filter out the zero elements.

Pyth, 5 bytes

jfT.n

Test suite

Japt, 5 bytes

c f ¸

Test it


Explanation

Implicit input of array U. Flatten the array with c. Filter it with f to remove the 0s. Join it to a string using spaces with ¸. Implicit output of resulting string.

APL (Dyalog), 4 bytes

0~⍨∊

Try it online!

0~⍨ zeros removed from

 the ϵnlisted (flattened) data

Prolog (79)

It inputs the list as a term, so you need to put a '.' after the list in the input.

Actually does list flattening.

x([H|T]):-x(H),x(T).
x(0). x([]).
x(M):-write(M),put(32).
:-read(X),x(X),halt.

Python, 45

w00, exception handling in golf!

def d(x):
 try:map(d,x)
 except:print`x`*(x!=0)

Scala, 42 chars

Tokenized the string by non digits and non-digit followed by zero.

print(readLine split"\\D|\\b0"mkString" ")

Sed, 20 chars

Solution is based on POSIX Extended Regular Expression.

s;[^0-9]+0|[],[]+;;g

Output:

bash-3.2$ sed -rf sedFile <<< "[[[4, 5, 8]], [[5, 6, 20]], [[1, 20, 500]]]" 
4 5 8 5 6 20 1 20 500

Edit: POSIX Basic Regular Expression(@clueless's solution), 19 chars:

s/[^0-9][^1-9]*/ /g

bash: 29 chars

l=$(echo "[[[1, 0], [0, 0], [0, 0]], [[1, 0], [1, 2], [2, 0]], [[2, 0], [0, 0], [0, 0]]]")
echo $l|tr -d '][,'|sed 's/\b0\b/ /g'
1           1   1 2 2   2          

counting line 2 only withtout 'echo $l |'. Test for 3 samples:

  1    2    2 3
   4 5 8   5 6 20   1 20 500
   1               1    1 2  2     2            

Scala 147:

Working on real lists, not on strings:

def f[A](l:List[_]):List[_]=l match{
case Nil=>l
case(l:List[_])::s=>(f(l):::f(s))
case e::s=>e::f(s)}
def p(l:List[_])=f(l)filter(!=0)mkString " "

Now the testdata:

val l1 = List (List (1, 0), List (2, 0), List (2, 3))
val l2 = List (List (List (4, 5, 8)), List (List (5, 6, 20)), List (List (1, 20, 500)))
val l3 = List (List (List (1, 0), List (0, 0), List (0, 0)), List (List (1, 0), List (1, 2), List (2, 0)), List (List (2, 0), List (0, 0), List (0, 0)))
val l4 = List (l1, l2, l3)

scala> l4.map(p)
res94: List[String] = List(1 2 2 3, 4 5 8 5 6 20 1 20 500, 1 1 1 2 2 2)

scala> p(l4)
res95: String = 1 2 2 3 4 5 8 5 6 20 1 20 500 1 1 1 2 2 2

Perl, 20 16 13 chars

perl -ple 's/\D+0?/ /g'

The -l switch is necessary to preserve the final newline in the output.

Here's an alternate version that actually works with the lists semantically (51 chars).

perl -E '$,=$";sub p{map{ref$_?p(@$_):$_||""}@_}say p eval<>'

Both of these programs take advantage of the problem's stipulation that it "can separate each item with any kind of whitespace", and replaces the zeros with blanks, instead of removing them outright.

Python, 99 111 chars

def d(l):
    if list==type(l):return[y for x in l for y in d(x)]
    return[str(l)]*(l!=0)
print" ".join(d(input()))

Previous 99 char version - fails when lists with zeros only are included:

d=lambda l:list==type(l)and[y for x in l for y in d(x)]or[str(l)]*(l!=0)
print" ".join(d(input()))

d(l) recursively flattens the list l, while filtering zeros and converting numbers to strings.

Perl 13, 14 char dit: p count for one char

s/\D+|\b0/ /g

usage:

cat '[[1, 0], [2, 0], [2, 3]]' | perl -pe 's/\D+|\b0/ /g'

Python 3, 49 chars

import re
print(*re.findall('[1-9]\d*',input()))

Python 2, 58 chars

import re
print re.sub('\D[^1-9]*',' ',raw_input())[1:-1]

C, 45 chars

for(;s=strtok(s,"[], ");s=0)atoi(s)&&puts(s);

It assumes that the input is given in a modifiable memory area pointed by s.

Golfscript 15

~{[]*}4*{},' '*

Input

Run from the command line, like so:

echo [[[1 0] [0 0] [0 0]] [[1 0] [1 2] [2 0]] [[2 0] [0 0] [0 0]]] | ruby golfscript.rb x.gs

(assumung that the x.gs file contains the code presented above).

Note that there are no commas (,) when defining the arrays; that's Golfscript syntax

Output

When the command described in the Input section is issued, the output is:

1 1 1 2 2 2

APL (10)

0~⍨⍎⍞~'[]'

Explanation:

K, 12

{x@?&x:,//x}

.

k){x@?&x:,//x}((1;0);(2;0);(2;3))
1 2 2 3
k){x@?&x:,//x}(((4;5;8));((5;6;20));((1;20;500)))
4 5 8 5 6 20 1 20 500
k){x@?&x:,//x}(((1;0);(0;0);(0;0));((1;0);(1;2);(2;0));((2;0);(0;0);(0;0)))
1 1 1 2 2 2

Ruby, 38 characters

puts eval(gets).flatten.reject &:zero?

The numbers are printed separated by a line break.