The Lisp language has a family of functions car
, cdr
, cadr
, etc for accessing arrays. For each one, an a
defines taking the first item of an array, and a d
defines taking the rest.
For example, running cadr
on [[1,3,4],5,7]
will return [3,4]
as the a
gets the first item ([1,3,4]
) and the d
removes the first item.
We've already had a challenge regarding running a cadaddadadaddddaddddddr
on a list, but what about the reverse?
Your challenge is to, given a string of the type above (starting with a c
, ending with a r
, with only ad
in the middle), and a single value, create an array such that running the string as a cadaddadadaddddaddddddr
on the array returns said value.
For example, given the input cadar, 1
a possible output could be [[0,[1]]]
since running cadar
on that gives 1.
Scoring
This is code-golf, shortest wins!
Testcases
These are possible outputs, all that matters is that your program returns an array which works for the condition.
car, 3 => [3]
cdddar, 5 => [0,0,0,5]
cadadadadar, 4 => [[0,[0,[0,[0,[4]]]]]]
caaaaaaaaar, 2 => [[[[[[[[[[2]]]]]]]]]]
ar
? \$\endgroup\$dr
. This could be much better explained, but should really have at the minimum a test case. \$\endgroup\$c....dr
- and that we, therefore need to be able to take both integers and lists/arrays as the value input. (Perhaps allowing value to always be a list if the language is strongly typed?) \$\endgroup\$a
s andd
s left-to-right or the other way? The test cases assume the former, but the linked question and Lisp itself the latter. \$\endgroup\$