Parse a list of lists into a Sad-List

In this challenge, you must parse a list of lists, into a simpler list format.

This challenge is based on my sadflak parser. In my sadflak parser, it has all the () removed, replaced with the sum of the ()s at the start of the list, to make the program run faster.

To parse into a Sad-List, you have to do this (python implementation thing, uses a tuple of tuples):

def sadlistfunc(list):
for i in list:
if i == ():
else:


This is a recursive function. For a list, start a new list, starting with the number of () from the list input, then the rest of this list is sad-list versions of every list that was not a () from the list input, in order. return the list.

Input:

you may take input in a few different formats:

• you may take it as a list
• you may take it as a tuple
• you may take it as a string

if you take it as a string, you should use some set of brackets, as appear in brain-flak. you may not use characters 1 and 2

just be reasonable

Input will always be inside one list, but your program may assume an implicit list layer outside of the input, i.e. ()()() = (()()()), or it may choose not to. Examples will be with explicit outside list

output:

may be list or tuple or string, or whatever. you may use whatever reasonable output format, as is the meta consensus.

Example:

(()()()) = [3]
(((()))) = [0,[0,[1]]]
((())()(())) = [1, [1], [1]]
() = invalid input, if the outside bracket is explicit.
((((())())())(())()) = [1, [1, [1, [1]]], [1]]


note that input is not strict. these inputs could be:

[[],[],[]]
[[[[]]]]
[[[]],[],[[]]]
[]
[[[[[]],[]],[]],[[]],[]]


or some other reasonable format

explained test case:

(()()((())())())


to "sadify" this, first we count the number of ()

 ()()        ()
(    ((())())  )


3. then we remove these, and add a 3 at the start

(3,((())()))


there is one list in this list. we sadify this

((())())


how many ()?

     ()
((())  )


1. we remove and add a 1 at the start

(1,(()))


this has one list in it

(())


count

 ()
(  )


(1)


then we put this back into its list

(1,(1))


then we put this back into its list

(3,(1,(1)))


done

This is , so shorter is better

• Note that in the actual sad-flak parser, the number of () is actually the second item of the list, and the first item is the index of the command – Destructible Lemon Jun 26 '17 at 0:45
• Good ol' JavaScript for... in, making me remember why you never use it: Fiddle – Stephen Jun 26 '17 at 1:17
• I suppose ((((())())())(())()) = [1, [1, [1, [1]], [1]] should be ((((())())())(())()) = [1, [1, [1, [1]]], [1]]. – Renzo Jun 26 '17 at 8:19

Pyth, 13 bytes

L+]/bYyM-b]Yy


How it works

L+]/bYyM-b]Yy
L               define a function y with argument b:
/bY              list 1: count how many [] can be found in b
]                         wrap into singleton
-b]Y        list 2: filter out [] from b
yM                    apply y (this function) to each
+                  concatenate the two lists above
y   apply y to the input

• You can remove first ]. – Erik the Outgolfer Jun 26 '17 at 8:24

CommonLisp, 49 bytes

(defun s(l)(cons(count()l)(mapcar's(remove()l))))


take input as list of lists.

Try it online!

Brachylog, 21 bytes

;[[]]x{↰₀}ᵐA&{∋∅}ᶜg,A


Try it online!

• Bytes and characters are not equivalent. Using UTF-8, that would take up 33 bytes, despite being only 21 characters. – Samadi Jun 26 '17 at 18:41
• @Samadi Brachylog uses its own code page, which is allowed per this meta answer. – Leaky Nun Jun 27 '17 at 1:07
• Ah, I see. I was slightly confused. Thanks for clarifying! – Samadi Jun 27 '17 at 7:42
• @Samadi No problem, this question gets asked all the time. – Leaky Nun Jun 27 '17 at 7:43

Mathematica, 42 bytes

{#~Count~{0},##&@@#~DeleteCases~{0}}&//@#&


Avoids explicit recursion by using //@ (MapAll) which maps a function over every node in a tree. This also means that the functions are executed from the leaves upwards. However, it will also be applied to {} which get turned into {0} in the process. That's why we count and remove {0} instead {}.

Retina, 42 bytes

+(({()|}(?<-3>))*)(?!\3){}
1$1 {(1*) {$.1


Try it online!

Clojure, 59 bytes

(fn f[i](conj(map f(remove #{[]}i))(count(filter #{[]}i))))


Not much different from the CommonLisp answer. Its count and remove seem to accept a bit nicer construct, here I had to use sets.

Actually, 12 bytes

;[]@c@;░Q£Mo


Try it online!

Takes input as a comma-separated square-bracket list with explicit outer brackers.

Explanation:

;[]@c@;░Q£Mo
;[]@c         count the number of empty lists
@;░      filter out empty lists
Q£Mo  recurse with filtered list and append result


Python 2, 6946 45 bytes

f=lambda l:[l.count([])]+map(f,filter(len,l))


Try it online!

• I think you need to add f= to your bytecount, since you're using the function f, and naming it otherwise would break your solution – Leo Jun 27 '17 at 9:56
• @Leo You are right. – ovs Jun 27 '17 at 10:10

Tịß€;@ċ“”$ Try it online! Takes input as list of lists of lists... Of course it uses the algorithm the other answers use. ;) • That's not 10 bytes. It's 10 characters. The number of bytes will depend on what encoding you use. – Samadi Jun 26 '17 at 18:40 • @Samadi No, Jelly has a dedicated character set which is its default, and it can represent those characters as one byte each. See here. – Adám Jun 26 '17 at 23:46 • I see. Thanks for the clarification! – Samadi Jun 27 '17 at 7:40 Haskell, 102 bytes data L=I Int|T[L]deriving Show (I n:r)#m=r#(n+m) (x:r)#m=x:r#m _#m=[I m] f(T[])=I 1 f(T t)=T$map f t#0


Try it online!

Because Haskell is strictly typed, there are no arbitrarily nested lists. As a remedy data L=I Int|T[L]deriving Show declares tree-like nested lists with either Ints or empty lists as leafs.

Input is like in the second example format, with an additional constructor T before each opening brace: T[T[T[]],T[],T[T[]]]. The same goes for the output, with each number being preceded by a constructor I. Function f performs the saddening.

Outputs for the test cases:

T [I 3]
T [T [T [I 1],I 0],I 0]
T [T [I 1],T [I 1],I 1]
T [T [T [T [I 1],I 1],I 1],T [I 1],I 1]


Javascript (ES6), 77 bytes

Golfed:

let m=a=>!a.length||a.map(m).reduce((b,c)=>(c.length?b.push(c):b[0]++,b),[0])


Ungolfed:

const traverse = arr => !arr.length || arr
.map(traverse)
.reduce(
(accum, val) => (val.length ? accum.push(val) : accum[0]++, accum),
[0]
);


Demo

let m=a=>!a.length||a.map(m).reduce((b,c)=>(c.length?b.push(c):b[0]++,b),[0])

o.innerHTML = [
JSON.stringify(m([[],[],[]])),
JSON.stringify(m([[[[]]]])),
JSON.stringify(m([[[]],[],[[]]])),
JSON.stringify(m([])),
JSON.stringify(m([[[[[]],[]],[]],[[]],[]]))
].join('\n')
<pre id=o></pre>`