7
\$\begingroup\$

An S-expression is a way of writing a nested list or tree of data, which you may recognize from their use in Lisp, which uses them for source code.

An S-expression is made up of atoms, in this challenge represented by single characters, and expressions, which are written enclosed in parentheses as an atom followed by one or more S-expressions. For example, * and 2 are atoms, and (* 2) is an expression. Similarly, (* (* 2) 2) is also an expression; these can be deeply nested.

Note that ((* 2) 2) is not a valid expression, since its first item is not an atom. This makes the structure distinct from an arbitrary binary tree. As such, your program will not need to handle such input.

Your task is to, given an S-expression, output an ASCII-art diagram representing it.

The diagram is made of diagonal lines of backslashes with forward slashes poking out at every child of the expression. If the child is an atom, it comes directly after the line coming out. If it is an expression, a new line is drawn starting at that point.

The following examples should make the description clearer.

Given the input (* 2 (+ 3 4)):

 /\
* /\
 2 /
  /\
 + /\
  3 /
   4

Given (* (+ 3 4) 2):

 /\
* /\
 /\ \
+ /\ \
 3 / /
  4 2

Given (+ (+ (+ 1 2) 3) 4):

 /\
+ /\
 /\ \
+ /\ \
 /\ \ \
+ /\ \ \
 1 / / /
  2 3 4

Given (+ 1 (* 2) 3 (- (+ 4 (- 5) 6) (+ 7)) 8):

 /\
+ /\
 1 /\
  /\ \
 * / /\
  2 3 /\
     /\ \
    - /\ \
     /\ \ \
    + /\ \ \
     4 /\ \ \
      /\ \ \ \
     - / / /  \
      5 6 /\  /
         + / 8
          7

Given (+ (+ (+ (+ (+ 1)))) 2):

 /\     
+ /\    
 /\ \   
+ /  \  
 /\   \ 
+ /    \
 /\    /
+ /   2 
 /\     
+ /     
 1

You can take the input in any parsed form, e.g. ['*', ['+', '3', '4'], '2'].

The atoms in the expression will be single characters, printable ASCII, and not \ or /.

\$\endgroup\$
2
  • \$\begingroup\$ Can they be extended more than enough? \$\endgroup\$
    – l4m2
    Commented Oct 5 at 11:03
  • \$\begingroup\$ @l4m2 No, each down+left line must be exactly 1 slash and each down+right line must be as short as possible while fitting all of its children underneath. \$\endgroup\$ Commented Oct 5 at 11:08

1 Answer 1

4
\$\begingroup\$

Charcoal, 43 40 bytes

↶≔⁰ηFS≡ι(⊞υ⁺ηLυ)«↖⁻ⅉ⊟υ\¿υ/»«JηηFυ↙↗ι/≦⊕η

Try it online! Link is to verbose version of code. Takes input as a string of atoms and parentheses without spaces. Explanation:

Change the default text output direction to upwards.

≔⁰η

Start with 0 atoms so far.

FS≡ι

Switch on each input character.

(⊞υ⁺ηLυ

For (, save the row where this expression starts.

For )...

↖⁻ⅉ⊟υ

... draw a line of \s back to the last saved row, ...

\

... with an extra \ for the atom at the start of that expression, ...

¿υ/»

... and a joining / if this is not the outer expression.

«JηηFυ↙

Otherwise, jump to the position of this atom on the canvas, which is one square down and right for each atom, plus one square down and left for each level of nesting.

↗ι/

Output the atom and the joining /.

≦⊕η

Increment the atom count.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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