45
\$\begingroup\$

Lots of people on this site use esoteric languages, and since these languages are unusual and hard to understand, they will frequently write an explanation in a certain format. For example, if the code was

abcdefghijklmnop

And this languages uses # for comments, they would write an explanation like this:

a                #Explanation of what 'a' does
 bc              #Bc
   d             #d
    e            #Explanation of e
     fgh         #foobar
        ij       #hello world
          k      #etc.
           l     #so on
            mn   #and
              op #so forth

I frequently do this too, but every time I do this, I feel like creating the layout of text is really obnoxious and time-consuming. So I want you to create a "Esolang-Comment-Template-Generator" for me. For example, if we ignore the comments, the previous code has this template:

a                #
 bc              #
   d             #
    e            #
     fgh         #
        ij       #
          k      #
           l     #
            mn   #
              op #

The Challenge:

You must write a program or function that takes two strings as input, and outputs this "Esolang-Comment-Template". The first input will be the code, but with bars (|) inserted where the newlines go. The second input is what we will use for comments. So our last example would have this for input:

"a|bc|d|e|fgh|ij|k|l|mn|op", "#"

Unfortunately this excludes bars from being part of the code input, but that's OK. You can assume that the comment input will be a single character. For simplicity's sake, the comment char will not be a bar. The code input will only contain printable ASCII, and it will not contain any newlines.

Hopefully you can infer what to do from the testcases, but I'll try to clarify some things.

You must split the code input up into "code-sections" on every bar. Then, each section of code is output on its own line and left-padded with the length of all the previous code (not including the bars). Then, each line is right-padded with enough spaces so that the last two characters on every line are "One additional space" + "The comment character".

One trailing newline is allowed.

Here is another example. For the input

"Hello|World", "/"

The first section of code is "Hello" and the second is "World". So it should give the output:

Hello      /
     World /

Here are some more samples:

Input:
"a|b|c|d|e|f|g", ","

Output:
a       ,
 b      ,
  c     ,
   d    ,
    e   ,
     f  ,
      g ,

Input:
"abcdefg", ":"

Output:
abcdefg :

Input:
"4|8|15|16|23|42", "%"

Output:
4          %
 8         %
  15       %
    16     %
      23   %
        42 %

Input:
"E|ac|h s|ecti|on is| one c|haracte|r longer| than the| last!", "!"

Output:
E                                                   !
 ac                                                 !
   h s                                              !
      ecti                                          !
          on is                                     !
                one c                               !
                     haracte                        !
                            r longer                !
                                     than the       !
                                              last! !

Input:
"This|Code|has||empty||sections", "@"

Output:
This                     @
    Code                 @
        has              @
                         @
           empty         @
                         @
                sections @

Rules:

You may take these inputs and outputs in any reasonable format. For example, reading/writing a file, STDIN/STOUT, function arguments/return value, etc. As usual, this is , so try to make your code as short as possible and you win if you can get the shortest solution in your language! I will also select the shortest solution as the overall winner. Standard loopholes are banned.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Related \$\endgroup\$ Sep 9, 2016 at 4:32
  • \$\begingroup\$ Are trailing blanks allowed? \$\endgroup\$
    – Titus
    Sep 9, 2016 at 9:02
  • 30
    \$\begingroup\$ Next step : a 3D representation for 2D languages \$\endgroup\$
    – Aaron
    Sep 9, 2016 at 9:26
  • 3
    \$\begingroup\$ A bonus if you manage to do it without using the | character would be nice, so you can explain yourself \$\endgroup\$ Sep 9, 2016 at 12:11
  • \$\begingroup\$ Can the comment character be a bar (|) ? \$\endgroup\$
    – Ton Hospel
    Sep 12, 2016 at 8:46

35 Answers 35

1
2
0
\$\begingroup\$

GolfScript, 85 bytes

{(;);}:r;", "%(r\(r n+:c;;.,\'|'%.,@\-)):l;0:m;{.,0>}{" "m*\(.,m+:m l\-" "\*+c@}while

Try it online

2017 Update - GolfScript - 71 bytes

', '/~~:c;~1/.,\.{'|'=},,@\-):l;['|']/0:i;{.,i+:i l i-' '*c+\' '*"
"\}/

Explanation

', '/~~:c;~1/        # Parses input
.,\.{'|'=},,@\-):l;  # Computes string length without '|'
['|']/               # Splits the array
0:i;                 # Counter
{.,                  # Length of the substring
i+:i                 # Counter update
l i-' '*c+\          # Adds spaces after the substring 
' '*"\n"\            # Adds spaces before the next substring
}/                   # ...For each substring
\$\endgroup\$
0
\$\begingroup\$

Python 3.8 (pre-release), 99 bytes

Different approach:

def f(c,d,i=0):
 for y in(x:=c.split('|')):z=[' '*len(e)for e in x];z[i]=y;print(''.join(z),d);i+=1

Try it online!

Ungolfed with explanation:

def f(c,d,i=0):
 x=c.split('|')              # break string by '|'
 for y in x:                 # for each element of list
  z=[' '*len(e) for e in x]  # create a list of blank strings
  z[i]=y                     # insert element we care about into otherwise blank list
  print(''.join(z),d)        # print line
  i+=1                       # increment
\$\endgroup\$
0
\$\begingroup\$

Stax, 16 bytes

ù3l╛CrΣ╝▀↕┘╣óétº

Run and debug it

\$\endgroup\$
0
\$\begingroup\$

Thunno 2 N, 25 bytes

'|/DJl⁺X0Yıyṣs+xðĿnlẏ+ƥ¹+

Try it online!

Explanation

(Yes, this is in the exact same format as the outputs for this challenge :P)

'|/DJl⁺X0Yıyṣs+xðĿnlẏ+ƥ¹+  '# Implicit input
'|/                        '# Split the input on "|"
   DJl                      # Duplicate, join, and get length
      ⁺X                    # Increment this and store it in x
        0Y                  # Store 0 in y
          ı                 # Map over the list:
           yṣ               #  Push y spaces
             s+             #  Prepend this to the input
               xðĿ          #  Pad with spaces to length x
                  nlẏ+ƥ     #  Add the length to y
                       ¹+   #  Append the second input
                            # Implicit output, joined on newlines
\$\endgroup\$
0
\$\begingroup\$

Go, 209 bytes

import."strings"
func f(C,c string)string{L,Q,S:=Split(C,"|"),[]string{},0
for _,l:=range L{q:=len(l)
if q<1{continue}
Q=append(Q,Repeat(" ",S)+l+Repeat(" ",len(Join(L,""))-q+1-S)+c)
S+=q}
return Join(Q,"\n")}

Attempt This Online!

Explanation

import."strings"
func f(C,c string)string{
// split C into substrings, and init the output slice and space count
L,Q,S:=Split(C,"|"),[]string{},0
// for each substring...
for _,l:=range L{
// if it's empty, skip it
q:=len(l)
if q<1{continue}
// add to the output slice...
Q=append(Q,
    // initial spaces
    Repeat(" ",S)+
    // the substring
    l+
    // the following spaces 
    Repeat(" ",len(Join(L,""))-q+1-S)
    // and the comment    
    +c)
// increment the space count
S+=q}
// join together with newlines
return Join(Q,"\n")}
\$\endgroup\$
1
2

Your Answer

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

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