9
\$\begingroup\$

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]
cadadadadar, 4 => [[0,[0,[0,[0,[4]]]]]]
caaaaaaaaar, 2 => [[[[[[[[[[2]]]]]]]]]]
\$\endgroup\$
10
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – xnor
    Aug 15, 2021 at 6:32
  • 6
    \$\begingroup\$ Am I correct to assume that the value cannot be a list, so the string necessarily ends in ar? \$\endgroup\$
    – att
    Aug 15, 2021 at 7:01
  • 9
    \$\begingroup\$ 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. \$\endgroup\$
    – Wheat Wizard
    Aug 15, 2021 at 11:52
  • 6
    \$\begingroup\$ 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?) \$\endgroup\$ Aug 15, 2021 at 13:21
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – pajonk
    Aug 15, 2021 at 17:18

7 Answers 7

14
\$\begingroup\$

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.

\$\endgroup\$
6
\$\begingroup\$

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!

\$\endgroup\$
5
\$\begingroup\$

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:
 nH        #     convert to hexadecimal
   ₂       #     is that divisible by 2?
    [      #     if so:
     w     #         wrap the TOS in a list ([TOS])
      |    #     else:
       ⁰J  #         join the TOS and the next input
\$\endgroup\$
4
\$\begingroup\$

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

Ruby, 48 bytes

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

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Jonah
    Aug 15, 2021 at 17:19
2
\$\begingroup\$

Charcoal, 23 bytes

F⮌η≡ιd≔⁺⟦⁰⟧θθa≔⟦θ⟧θP⭆¹θ

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

F⮌η≡ι

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.

P⭆¹θ

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

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

Jelly, 9 8 bytes

W;$L}¡ḢḢ

Try it online!

Based on xnor's idea.

-1 byte thanks to Jonathan Allan

\$\endgroup\$
1
  • 1
    \$\begingroup\$ W;$L}¡ḊḢ saves one. \$\endgroup\$ Aug 15, 2021 at 14:13

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