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 code-golf, so shorter is better
for... in
, making me remember why you never use it: Fiddle \$\endgroup\$((((())())())(())()) = [1, [1, [1, [1]], [1]]
should be((((())())())(())()) = [1, [1, [1, [1]]], [1]]
. \$\endgroup\$