| Bytes | Lang | Time | Link |
|---|---|---|---|
| 032 | Tcl | 171115T183651Z | sergiol |
| 002 | Vyxal S | 220724T005001Z | naffetS |
| 037 | Wolfram Language Mathematica | 190812T205156Z | Roman |
| 070 | PHP | 190812T204547Z | 640KB |
| 008 | Brachylog | 190812T202857Z | Unrelate |
| 002 | Stax | 180411T155407Z | recursiv |
| 053 | JavaScript Node.js | 180411T145427Z | l4m2 |
| 106 | Java 10 | 180411T133749Z | Kevin Cr |
| 029 | R | 180411T144222Z | Giuseppe |
| 005 | Pyth | 180411T132740Z | hakr14 |
| 005 | Japt | 170821T140012Z | Shaggy |
| 004 | APL Dyalog | 170821T134740Z | Adá |
| 079 | Prolog | 120529T142121Z | marinus |
| 045 | Python | 120529T035407Z | boothby |
| 042 | Scala | 120529T022128Z | Prince J |
| 020 | Sed | 120528T070839Z | Prince J |
| 029 | bash | 120528T070928Z | user unk |
| nan | 120529T005451Z | user unk | |
| 013 | Perl | 120528T051737Z | breadbox |
| 111 | Python | 120528T102021Z | ugoren |
| 014 | Perl | 120528T124234Z | Toto |
| 049 | Python 3 | 120528T111435Z | Clueless |
| 045 | C | 120528T080137Z | Alexande |
| 015 | Golfscript | 120528T071539Z | Cristian |
| nan | 120528T085621Z | marinus | |
| 012 | K | 120528T085440Z | tmartin |
| 038 | Ruby | 120528T050529Z | Ventero |
Tcl, 32 bytes
proc D L {join [join [join $L]]}
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!
proc D L {concat {*}[concat {*}[concat {*}$L]]}
proc D L {lsearch -al -inl -not "[string map {\{ "" \} ""} $L]" 0}
PHP, 70 bytes
function($a){array_walk_recursive($a,function($a){echo$a?"$a ":'';});}
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
ċ∋↰|ℕ₁ẉ⊥
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!⊥|ẉ⊥
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.
46 bytes
s->s.replaceAll("[^0-9 ]","").replace(" 0","")
Input and output both as String.
R, 29 bytes
function(l)(x=unlist(l))[!!x]
unlist converts the list to an atomic vector recursively, so we just need to filter out the zero elements.
Japt, 5 bytes
c f ¸
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.
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:
⍞~'[]': User input (⍞) without (~) the characters'[]'
This gives something like'1,2,0,2,3'⍎: Evaluate this string. It so happens that,is the concatenation operator, so now we have a list:1 2 0 2 3(APL lists are whitespace-separated by default)0~⍨: Remove all the numbers 0 from this list. (It is a list of numbers, not of strings, by now, so zeroes within numbers are not removed.- This value is output (by default, because it's the value of the whole program, kind of like Golfscript). APL lists are whitespace-separated by default so it looks exactly like in the question.
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.