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 , 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]
caaaaaaaaar, 2 => [[[[[[[[[[2]]]]]]]]]]

• Is the value we're given always a number? Can our array have values of other types? This can matter for using a constant value for the padding and guaranteeing it's different from the specified value.
– xnor
Aug 15 '21 at 6:32
• Am I correct to assume that the value cannot be a list, so the string necessarily ends in ar?
– att
Aug 15 '21 at 7:01
• I still don't understand how this is supposed to work when it ends in dr. This could be much better explained, but should really have at the minimum a test case. Aug 15 '21 at 11:52
• Still unclear if we must support 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?) Aug 15 '21 at 13:21
• Do we read the string of as and ds left-to-right or the other way? The test cases assume the former, but the linked question and Lisp itself the latter. Aug 15 '21 at 17:18

# Python 2, 40 bytes

f=lambda s,*l:s[3:]and f(s[1:],l,*l)or l


Try it online!

Doesn't bother telling car from cdr. Instead, makes a big nested tuple where every path ends at the right value, assuming it has the right length and ends in a. For example,

f("cdddar", 5) = ((((5,), 5), (5,), 5), ((5,), 5), (5,), 5)


is the same as f("caaaar", 5) or f("cdadar", 5). So, only the length of the input string matters.

This is done by repeating the transformation l -> (l,*l), which puts l as both the car and cdr of the new tuple. This happens once for each character in s except the first three, starting from a singleton of the input value.

# 05AB1E, 11 bytes

RA"¸  Ć"‡.V


Try it online!

R             # reverse the input
A            # push the lower case alphabet
"¸  Ć"‡     # transliterate, replace:
#  - "a" with "¸" (wrap)
#  - "c" with " " (noop)
#  - "d" with "Ć" (enclose, append the first value)
.V   # evaluate as 05AB1E code, the leading r reverses the empty stack


Porting xnor's construction comes in at 5 bytes:

g<GDš


Try it online!

# Vyxal, 10 9 bytes

(nH₂[w|⁰J


Try it Online!

Wow, using hexadecimal conversion actually helped save a byte. Exits with an error, but outputs the required list wrapped in a list.

## Explained

(nH₂[w|⁰J
(          # for each character n in the remnants:
₂       #     is that divisible by 2?
[      #     if so:
w     #         wrap the TOS in a list ([TOS])
|    #     else:
⁰J  #         join the TOS and the next input


# J, 47 41 bytes

4 :0
".'y',~(}.}:x)rplc a,:d,<'(,{.)'
)


Try it online!

• (}.}:x) Kill the c and r
• rplc a,:d,<'(,{.)' In what remains, replace a with enlist ,: (another nesting level) and replace d with (,{.) (append first element)
• ".'y',~ Evaluate the resulting verb on the right arg y

# Ruby, 48 bytes

f=->s,n{s[0]='';s<?r?s>?b?[0]+f[s,n]:[f[s,n]]:n}


Try it online!

• Just for completeness, ruby port of xnor is only 33 f=->s,*n{s[3]?f[s[1..-1],n,*n]:n}: Try it online!. And in 2.7 (TIO is 2.5) you can do s[1..] for 31. Aug 15 '21 at 17:19

# Charcoal, 23 bytes

Ｆ⮌η≡ιd≔⁺⟦⁰⟧θθa≔⟦θ⟧θＰ⭆¹θ


Try it online! Link is to verbose version of code. Explanation:

Ｆ⮌η≡ι


Loop over the cdadadr string in reverse and switch on each character.

d≔⁺⟦⁰⟧θθ


If it's a d then prepend a 0.

a≔⟦θ⟧θ


If it's an a then wrap the value in a list.

Ｐ⭆¹θ


Stringify the final result. (For some reason, outputting using Charcoal's default output format produces a meaningless result.)

# Jelly, 9 8 bytes

W;$L}¡ḢḢ  Try it online! Based on xnor's idea. -1 byte thanks to Jonathan Allan • W;$L}¡ḊḢ saves one. Aug 15 '21 at 14:13