11
\$\begingroup\$

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):
    new-sadlist = [0]
    for i in list:
        if i == ():
            new-sadlist[0]+=1
        else:
            new-sadlist.append(sadlistfunc(i))

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

 ()
(  )

remove and add 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

\$\endgroup\$
3
  • \$\begingroup\$ 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 \$\endgroup\$ Jun 26 '17 at 0:45
  • \$\begingroup\$ Good ol' JavaScript for... in, making me remember why you never use it: Fiddle \$\endgroup\$
    – Stephen
    Jun 26 '17 at 1:17
  • \$\begingroup\$ I suppose ((((())())())(())()) = [1, [1, [1, [1]], [1]] should be ((((())())())(())()) = [1, [1, [1, [1]]], [1]]. \$\endgroup\$
    – Renzo
    Jun 26 '17 at 8:19

11 Answers 11

4
\$\begingroup\$

Pyth, 13 bytes

L+]/bYyM-b]Yy

Test suite.

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
\$\endgroup\$
1
  • \$\begingroup\$ You can remove first ]. \$\endgroup\$ Jun 26 '17 at 8:24
3
\$\begingroup\$

CommonLisp, 49 bytes

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

take input as list of lists.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Brachylog, 21 bytes

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

Try it online!

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

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 {}.

\$\endgroup\$
2
\$\begingroup\$

Retina, 42 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

Python 2, 69 46 45 bytes

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

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 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 \$\endgroup\$
    – Leo
    Jun 27 '17 at 9:56
  • \$\begingroup\$ @Leo You are right. \$\endgroup\$
    – ovs
    Jun 27 '17 at 10:10
1
\$\begingroup\$

Jelly, 10 bytes

Tị߀;@ċ“”$

Try it online!

Takes input as list of lists of lists...

Of course it uses the algorithm the other answers use. ;)

\$\endgroup\$
3
  • \$\begingroup\$ That's not 10 bytes. It's 10 characters. The number of bytes will depend on what encoding you use. \$\endgroup\$
    – vktec
    Jun 26 '17 at 18:40
  • 2
    \$\begingroup\$ @Samadi No, Jelly has a dedicated character set which is its default, and it can represent those characters as one byte each. See here. \$\endgroup\$
    – Adám
    Jun 26 '17 at 23:46
  • \$\begingroup\$ I see. Thanks for the clarification! \$\endgroup\$
    – vktec
    Jun 27 '17 at 7:40
1
\$\begingroup\$

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]
\$\endgroup\$
1
\$\begingroup\$

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>

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.