De-Nesting Lists

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

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.

Python, 45

w00, exception handling in golf!

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

Tags:

Code Golf