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 code-golf, 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.
48 Answers 48
Java 11, (削除) 189 (削除ここまで) (削除) 159 (削除ここまで) 136 bytes
s->c->{int p=0;for(var a:s.split("\\|"))System.out.println(" ".repeat(p)+a+" ".repeat(s.replace("|","").length()-(p+=a.length())+1)+c);}
-30 bytes converting Java 7 to Java 10 and optimizing the loops.
-23 bytes converting Java 10 to Java 11, so String#repeat
builtin can be used.
Explanation:
s->c->{ // Method with String & char parameters and no return-type
int p=0; // Position-integer, starting at 0
for(var a:s.split("\\|")) // Loop over the parts split by "|":
System.out.println( // Print with trailing newline:
" ".repeat(p) // `p` amount of spaces
+a // plus the current part
" ".repeat(s.replace("|","").length()-(p+=a.length())+1)
// plus the correct trailing amount of spaces
// (and add the length of the current part to `p` at the
// same time)
+c);} // plus the current comment character
Retina, (削除) 35 (削除ここまで) 34 bytes
Byte count assumes ISO 8859-1 encoding.
\|
·$'¶$`±
T0-2`·±|p`___ `.+±.|·.+
The two input strings are separated by a space (which is unambiguous since we know that the comment delimiter is always a single character).
-
1\$\begingroup\$ Why do you need a space to delimit the strings? Since it is a single character, it could just be the last one. \$\endgroup\$Adám– Adám2016年09月09日 12:56:35 +00:00Commented Sep 9, 2016 at 12:56
-
1\$\begingroup\$ @Adám I'm reusing it as the space separator in the final output. \$\endgroup\$Martin Ender– Martin Ender2016年09月09日 13:01:05 +00:00Commented Sep 9, 2016 at 13:01
Pyth - (削除) 28 (削除ここまで) (削除) 27 (削除ここまで) (削除) 24 (削除ここまで) 23 bytes
(削除) Might be able to golf a little off. (削除ここまで) A lot off, apparently!
jt+R+;zC.t.u+*lNdYcQ\|k
JavaScript (ES6), 92 bytes
f=
(s,c)=>s.split`|`.map((_,i,a)=>a.map((e,j)=>i-j?e.replace(/./g,` `):e).join``+` `+c).join`
`
;
<div oninput=o.textContent=f(s.value,c.value)><input id=s placeholder=Code><input id=c size=1 maxlength=1 value=#><pre id=o>
GNU sed (85 + 1 for -r) 86
:s;h;:;s,\|( *)[^ \|](.),|1円 2,円;t;s,\|,,g
p;g;:l;s,^( *)[^ \|],1円 ,;tl;s,\|,,;/\S/bs
The inputs are strings separated by a space.
Tests:
input.txt:
a|b|c|d|e|f|g ,
abcdefg :
4|8|15|16|23|42 %
E|ac|h s|ecti|on is| one c|haracte|r longer| than the| last! !
This|Code|has||empty||sections @
Output:
$ cat input.txt | sed -rf template
a ,
b ,
c ,
d ,
e ,
f ,
g ,
abcdefg :
4 %
8 %
15 %
16 %
23 %
42 %
E !
ac !
h s !
ecti !
on is !
one c !
haracte !
r longer !
than the !
last! !
This @
Code @
has @
@
empty @
@
sections @
-
\$\begingroup\$ The nameless label
:
is a GNU sed feature/bug and\S
is I think an extension, so maybe the title should beGNU sed
. Other than that, great code. \$\endgroup\$seshoumara– seshoumara2016年09月10日 05:57:09 +00:00Commented Sep 10, 2016 at 5:57 -
\$\begingroup\$ @seshoumara Thanks! \$\endgroup\$Riley– Riley2016年09月10日 06:10:17 +00:00Commented Sep 10, 2016 at 6:10
Python 2, (削除) 107 (削除ここまで) (削除) 105 (削除ここまで) (削除) 102 (削除ここまで) 99 bytes
Tested with all test cases above
EDIT Golfed off 2 bytes by changing d=a.split("|");i=0 to d,i=a.split("|"),0 Not sure how I missed that one. Thanks @Oliver Ni
Another 3 bytes gone. Thanks again.
Suggestion from @Jonathan actually saves 3 bytes and takes it down to the magic 99. Thanks.
def c(a,b):
d,i=a.split("|"),0
for e in d:j=i+len(e);print" "*i+e+" "*(len("".join(d))-j+1)+b;i=j
-
1\$\begingroup\$ Golfed it down another byte \$\endgroup\$Oliver Ni– Oliver Ni2016年09月10日 04:04:09 +00:00Commented Sep 10, 2016 at 4:04
-
3\$\begingroup\$ Hey @OliverNi , providing tips for golfing stuff down is appreciated, but editing code isn't really appropriate on this site (source), so I've rolled back your edit. Feel free to post those tips as a comment though! I'm sure the OP would appreciate it, but it should be up to them to test it and choose if they want to use it. \$\endgroup\$DJMcMayhem– DJMcMayhem2016年09月10日 14:04:50 +00:00Commented Sep 10, 2016 at 14:04
-
1\$\begingroup\$ Thanks to both of you. Firstly to @Oliver for taking the interest and the time to improve my humble effort and secondly to DJMcMayhem for clarifying what I believed to be the case but had not had a chance to comment on. Oliver - thanks again and please feel free to post changes as comments so that I can learn from your golfing experience. \$\endgroup\$ElPedro– ElPedro2016年09月10日 18:25:33 +00:00Commented Sep 10, 2016 at 18:25
-
1\$\begingroup\$ You can remove the parenthesis around
" "*i
to ave 2 bytes \$\endgroup\$Oliver Ni– Oliver Ni2016年09月10日 20:55:48 +00:00Commented Sep 10, 2016 at 20:55 -
1\$\begingroup\$ You can also set a variable to
len(e)
likefor e in d:z=len(e)....
to save a byte because it is used twice \$\endgroup\$Oliver Ni– Oliver Ni2016年09月10日 20:59:00 +00:00Commented Sep 10, 2016 at 20:59
Haskell, (削除) 139 (削除ここまで) 135 bytes
s#p=j$foldl g("",0)s where g(a,n)c|c=='|'=(j(a,n)++"\n"++q n,n)|1>0=(a++[c],n+1);q m=' '<$[1..m];j(a,n)=a++q(sum[1|c<-s,c/='|']-n+1)++p
Saved 4 bytes by inlining a definition.
Ungolfed:
template :: String -> String -> String
template code comment = format $ foldl g ("", 0) code
where g (acc, n) c
| c == '|' = (format (acc, n) ++ "\n" ++ spaces n, n)
| otherwise = (acc ++ [c], n+1)
l = length $ filter (/= '|') code
spaces n = replicate n ' '
format (acc, n) = acc ++ spaces (l-n+1) ++ comment
Groovy, (削除) 120 (削除ここまで) (削除) 113 (削除ここまで) 111 Bytes
def m(s,c){s.split(/\|/).inject(0,{e,t->println((' '*e+t).padRight(s.replace('|','').size()+1)+c);e+t.size()})}
ungolfed*
def m(s,c){
s.split(/\|/).inject(0, { e, t ->
println((' '*e+t).padRight(s.replace('|','').size())+' '+c)
e+t.size()
})
}
(First Draft with 120 Bytes)
def m(s,c){def l=0;s.split(/\|/).collect{l+=it.size();it.padLeft(l).padRight(s.replace('|','').size())+' '+c}.join('\n')}
ungolfed*
def m(s,c){
def l=0 // minimized version needs a semicolon here
s.split(/\|/).collect{
l+=it.size() // minimized version needs a semicolon here
it.padLeft(l).padRight(s.replace('|','').size())+' '+c
}.join('\n')
}
Tests
%> m('a|bc|d|e|fgh|ij|k|l|mn|op', '#')
a #
bc #
d #
e #
fgh #
ij #
k #
l #
mn #
op #
%> m('Hello|World', '/')
Hello /
World /
%> m('a|b|c|d|e|f|g', ',')
a ,
b ,
c ,
d ,
e ,
f ,
g ,
%> m('abcdefg', ':')
abcdefg :
%> m('4|8|15|16|23|42', '%')
4 %
8 %
15 %
16 %
23 %
42 %
%> m('E|ac|h s|ecti|on is| one c|haracte|r longer| than the| last!', '!')
E !
ac !
h s !
ecti !
on is !
one c !
haracte !
r longer !
than the !
last! !
%> m('This|Code|has||empty||sections', '@')
This @
Code @
has @
@
empty @
@
sections @
-
\$\begingroup\$ How about
.padRight(s.replace('|','').size()+1)+c)
\$\endgroup\$AmazingDreams– AmazingDreams2016年09月09日 11:57:40 +00:00Commented Sep 9, 2016 at 11:57
Python 2, (削除) 125 124 (削除ここまで) 132 bytes
-1 byte thanks to @TuukkaX (missed golfing the space from i, v
)
def g(s,c):x=s.split('|');print((' '+c+'\n').join(' '*len(''.join(x[:i]))+v+' '*len(''.join(x[i+1:]))for i,v in enumerate(x))+' '+c)
All test cases on ideone
-
1\$\begingroup\$ Your supposed to use
c
as the comment char, not#
. \$\endgroup\$Oliver Ni– Oliver Ni2016年09月11日 01:01:13 +00:00Commented Sep 11, 2016 at 1:01 -
\$\begingroup\$ @OliverNi - uh that was a hit for the code in its current form. \$\endgroup\$Jonathan Allan– Jonathan Allan2016年09月11日 01:10:17 +00:00Commented Sep 11, 2016 at 1:10
05AB1E, (削除) 29 (削除ここまで) (削除) 38 (削除ここまで) (削除) 31 (削除ここまで) 29 bytes
'|„ǝʒ:'ǝ¡'ʒмεD®>úsg®+©s}.Bεð2J,
Can definitely be golfed, but at least its working now..
+9 bytes because ¡
(split) removes empty items automatically, so I had to add '|„ǝʒ:'ǝ¡'ʒм
..
-2 bytes thanks to @MagicOctopusUrn by changing '|„ǝʒ:'ǝ¡'ʒм
to '|¶:.BεðÜ}
(current solution doesn't work on items with trailing spaces, but I've assumed that's allowed according to the test cases).
Explanation:
'|¶: # Take the first input, and replace every "|" with "¶"
# i.e. "abc|d|e||fg" → "abc¶d¶e¶¶fg" (¶ are new-lines in 05AB1E)
.B # Box all the items (appending trailing whitespace to make it a rectangle)
# i.e. "abc¶d¶e¶¶fg" → ['abc','d ','e ',' ','fg ']
εðÜ} # Remove all trailing spaces from each item
# i.e. ['abc','d ','e ',' ','fg '] → ['abc','d,'e','','fg']
# NOTE: `'|¡` would have resulted in ['abc','d','e','fd'], hence the use of
# Box which implicitly splits on new-lines to keep empty items
ε # For-each:
D # Duplicate the current item
®>ú # Prepend global_variable + 1 amount of spaces
# (+1 because the global_variable is -1 by default)
# i.e. "e" and 3+1 → " e"
sg # Swap so the duplicated item is at the top, and take its length
®+ # Sum it with the global_variable
# i.e. "e" (→ 1) and 4 → 5
© # And store it as new global_variable
s # Then swap so the space appended item is at the end again
} # And end the for-each loop
.B # Box all the items (appending the appropriate amount of spaces)
# i.e. ['abc',' d',' e',' ',' fg']
# → ['abc ',' d ',' e ',' ',' fg']
ε # For-each again:
ð # A space character
I # The second input-character
J # Join both together with the current item
, # And print the current row with trailing new-line
-
\$\begingroup\$ This is invalid if the code were to contain
ǝʒ
.'|¶:.B
could work tho. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2018年06月05日 20:32:11 +00:00Commented Jun 5, 2018 at 20:32 -
\$\begingroup\$ @MagicOctopusUrn The challenge description states "The code input will only contain printable ASCII, and it will not contain any newlines." Also, what part of the code should be replaced with
'|¶:.B
? \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年06月05日 20:42:47 +00:00Commented Jun 5, 2018 at 20:42 -
\$\begingroup\$ I was thinking that'd be a shorter split, but it wouldn't work with your current code just slamming it in, you'd have to trim the excess. OR just ignore the excess and
.B
a second time after adding in the preceeding spaces. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2018年06月05日 21:08:08 +00:00Commented Jun 5, 2018 at 21:08 -
\$\begingroup\$ @MagicOctopusUrn It indeed might save some bytes, since my current work-around is pretty long, but it'll be harder to calculate the amount of preceeding spaces with the spaces after the
.B
already present. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年06月05日 21:12:24 +00:00Commented Jun 5, 2018 at 21:12 -
1\$\begingroup\$
'|¶:.BεðÜ}εD®>úsg®+©s}.BεðIJ,
? 29 bytes. Back to iteration 1 :)..B
splits on newlines, which is a feature not many people know. It's the only way I know of to keep empty elements. I'd request this as a feature..¡
should mean split, but keep empty elements.. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2018年06月05日 21:15:56 +00:00Commented Jun 5, 2018 at 21:15
Japt -R
, 17 bytes
There's a byte to be saved here, at least, I just can't quite see it.
q| ËùT±DÊÃú Ë+ViS
q| ËùT±DÊÃú Ë+ViS :Implicit input of strings U=programme & V=character
q| :Split U on |
Ë :Map each D
ù : Left pad with spaces to length
T± : Increment T (initially 0) by
DÊ : Length of D
à :End map
ú :Right pad each with spaces to length of longest
Ë :Map
+ : Append
Vi : V prepended with
S : Space
:Implicit output joined with newlines
-
\$\begingroup\$ Wow, this is almost exactly the code I came up, the only difference being that I had
®+S+V
instead ofË+ViS
\$\endgroup\$noodle person– noodle person2023年10月07日 11:51:14 +00:00Commented Oct 7, 2023 at 11:51 -
\$\begingroup\$ I found that byte you were looking for :) \$\endgroup\$noodle person– noodle person2023年10月08日 12:40:02 +00:00Commented Oct 8, 2023 at 12:40
PowerShell v2+, (削除) 103 (削除ここまで) 99 bytes
param($a,$b)$a-split'\|'|%{" "*$l+$_+" "*(($a-replace'\|').length+1-$_.length-$l)+$b;$l+=$_.Length}
Takes input as two strings, -split
s the first on literal pipe (since split uses regex syntax), and feeds the elements into a loop |%{...}
.
Each iteration, we construct a string as being a number of spaces defined by $l
concatenated with the current element. For the first loop, $l
initializes to $null
, which gets evaluate here as 0
.
That string is further concatenated with another number of spaces (defined by how long $a
would be if we -replace
d every pipe with nothing, plus 1
for the additional padding between code and comments, minus the .length
of the current element, minus $l
which is how many spaces we padded left on this iteration), concatenated with our comment character $b
. That's left on the pipeline.
We then update $l
for the next iteration.
The resultant strings are all left on the pipeline, and output via implicit Write-Output
happens at program execution, with a newline between them by default.
Examples
PS C:\Tools\Scripts\golfing> .\esolang-comment-template-generator.ps1 "This|Code|has||empty||sections" "@"
This @
Code @
has @
@
empty @
@
sections @
PS C:\Tools\Scripts\golfing> .\esolang-comment-template-generator.ps1 "a|bc|def|ghi|h" "|"
a |
bc |
def |
ghi |
h |
Vim, (削除) 39 (削除ここまで) 38 keystrokes
-1 byte thanks to DJMcMayhem
Expects as input a buffer (e.g. a file) whose first character is the comment delimiter, followed by the code, e.g. #foo|bar|baz
.
"cxqaf|m`Yp<Ctrl+o>v$r jv0r x@aq@a$p<Ctrl+v>gg$C <Ctrl+r>c<Esc>
Explanation
("_
" denotes a literal space.)
"cx " Delete the first character (the comment delimiter) and store in register 'c'
qa " Start recording macro 'a'
f|m` " Advance to the first '|' on the line and set mark
Yp<Ctrl+o> " Duplicate this line and return to mark
v$r_ " Replace everything after the cursor on this line (inclusive) with spaces
jv0r_x " Go down a line and replace everything before the cursor on this line (inclusive) with
" spaces, then delete one space
@a " Call macro recursively
q@a " Stop recording and immediately call the macro
$p " Paste the deleted space at the end of the last line
<Ctrl+v>gg$ " Highlight the column where the comment delimiters will go and all trailing spaces
C_<Ctrl+r>c<Esc> " Replace the highlighted text on each line with a space and the contents of
" register 'c' (the comment delimiter)
-
1\$\begingroup\$ :D I always upvote vim! I think you could take one byte off if you change
mm
tom`
and then change`m
to<C-o>
\$\endgroup\$DJMcMayhem– DJMcMayhem2016年09月09日 22:33:30 +00:00Commented Sep 9, 2016 at 22:33 -
\$\begingroup\$ @DJMcMayhem Thanks! I love golfing in Vim because I always learn something about a tool I use every day. \$\endgroup\$Jordan– Jordan2016年09月10日 15:14:45 +00:00Commented Sep 10, 2016 at 15:14
Floroid - 94 bytes
Ah(a,b):c=a.fn("|");z(" "+b+"\n".y(' '*Z("".y(c[:j]))+l+" "*Z("".y(c[j+1:]))Kj,lIai(c))+' '+b)
Uses an approach similar to @JonathanAllans' Python solution.
Testcases
Call: h("a|bc|d|e|fgh|ij|k|l|mn|op", "#")
Output:
a #
bc #
d #
e #
fgh #
ij #
k #
l #
mn #
op #
C# (削除) 176 (削除ここまで) (削除) 167 (削除ここまで) 154 bytes
string f(string s,char x){var c=s.Split('|');var d="";int i=0;foreach(var b in c)d+=b.PadLeft(i+=b.Length).PadRight(s.Length+2-c.Length)+x+"\n";return d;}
UnGolfed
string f(string s, char x)
{
var c = s.Split('|');
var d = "";
int i = 0;
foreach (var b in c)
d += b.PadLeft(i += b.Length).PadRight(s.Length + 2 - c.Length) + x + "\n";
return d;
}
A LINQ solution would have been 146 but needed using System.Linq;
bringing it back up to 164:
string f(string s,char x){var c=s.Split('|');int i=0;return c.Aggregate("",(g,b)=>g+b.PadLeft(i+=b.Length).PadRight(s.Length+2-c.Length)+x+"\n");}
Old solutions:
167 bytes:
string f(string s,char x){var c=s.Split('|');var d="";int i=0;foreach(var b in c){d+=b.PadLeft(i+b.Length).PadRight(s.Length+2-c.Length)+x+"\n";i+=b.Length;}return d;}
176 bytes using string interpolation
string f(string s,char x){var c=s.Split('|');var d="";int i=0;foreach(var b in c){d+=string.Format($"{{1,{i}}}{{0,-{s.Length+2-c.Length-i}}}{x}\n",b,"");i+=b.Length;}return d;}
-
2\$\begingroup\$ Explanation by hyper-neutrino \$\endgroup\$2021年06月09日 16:42:55 +00:00Commented Jun 9, 2021 at 16:42
AWK, (削除) 107 (削除ここまで) (削除) 104 (削除ここまで) 103 bytes
w=2-(n=split(1,ドルp,"|"))+gsub(t=".",FS,1ドル){for(;s++<n;gsub(t,FS,b))y=y substr((b=b p[s])1,1,ドルw)2ドルRS}0ドル=y
Thanks to Pedro Maimere for helping shave off more 3 chars
One interesting thing about this challenge... I found a quirk about the TIO AWK setup. I can't figure out way to specify input where a positional argument can contain a space. Meaning setup a TIO test that behaves like this commandline,
echo "this is the first parameter" second | gawk '{ print 1ドル }'
In TIO, the first parameter will be "this
not the whole string. That means while it works in a terminal window, I can't show that it handle the second test for this challenge in TIO.
Here's how the code works... The test associated with the codeblock is always truthy, so the code always runs. It's only a test (rather then code) to avoid needing a ;
character. It does a couple of things by using AWK's willingness to combine multiple expressions into one "line" of code.
w=2-(n=split(1,ドルp,"|"))+gsub(t=".",FS,1ドル)
First is splits the first parameter into in array of strings using the pipe character as the delimeter with n=split(1,ドルp,"|")
. It also saves the number of substrings generated in n
. Then it changes all the characters in the first parameter to spaces with gsub(t=".",FS,1ドル)
setting convenience constants t
and n
in the process.
Finally the value of w
is set to the number of non-pipe characters in the first parameter by subtracting the number of sub-strings from the total number of characters in the original.
The associated code block is one statement, a for
loop, that processes each substring.
for(;s++<n;gsub(t,FS,b))y=y substr((b=b p[s])1,1,ドルw)2ドルRS
The iteration check is simple s++<n
. The end of loop statement converts all the characters in the previous output line to blanks gsub(t,FS,b)
, which turns it into the prefix for the next line. The body of the loop constructs and appends the next line of output to an accumulator y
. That string is a concatenation of the previous string, plus the current string (code fragment) with a bunch of blanks appended (meaning 1ドル
) (b=b p[s])1ドル
. That mess is truncated to the length we want w
and the comment delimiter and LF are appended with y=y substr(...,w)2ドルRS
.
Once all the code substrings have been processes, assigning 0ドル
to the accumulator takes care if printing out the results.
0ドル=y
-
1\$\begingroup\$ 104 bytes. It is having trouble when the code has spaces. I haven't figured out a way to fix that, though. \$\endgroup\$Pedro Maimere– Pedro Maimere2021年06月08日 00:44:53 +00:00Commented Jun 8, 2021 at 0:44
-
1\$\begingroup\$ I keep forgetting to use FS and RS. :) Thanks! The spaces work when I run it from the command line. Meaning when I run it in a terminal window. I think it's something about passing tokenized input to AWK in TIO. I haven't found a way to pass something like
"this is one parameter"
as1ドル
in TIO. \$\endgroup\$cnamejj– cnamejj2021年06月08日 08:33:05 +00:00Commented Jun 8, 2021 at 8:33 -
\$\begingroup\$ I tried in gawk 5.0.1 and mawk 1.3.3, without success. What version of awk are you using? I also tried another algorithm, but only reached 105 bytes. (sigh) \$\endgroup\$Pedro Maimere– Pedro Maimere2021年06月08日 20:26:19 +00:00Commented Jun 8, 2021 at 20:26
-
1\$\begingroup\$ This works on my system
echo "E|ac|h s|ecti|on is| one c|haracte|r longer| than the| last!" '!' | gawk 'w=2-(n=split(1,ドルp,"|"))+gsub(t=".",c=FS,1ドル){for(;s++<n;gsub(t,c,b))y=y substr((b=b p[s])1,1,ドルw)2ドルRS}0ドル=y'
and it's on Ubuntu 18.04 runningGNU Awk 4.1.4, API: 1.1 (GNU MPFR 4.0.1, GNU MP 6.1.2)
\$\endgroup\$cnamejj– cnamejj2021年06月08日 20:42:25 +00:00Commented Jun 8, 2021 at 20:42 -
1\$\begingroup\$ I'd post that one as a separate answer since it's a completely different approach. \$\endgroup\$cnamejj– cnamejj2021年06月08日 23:41:33 +00:00Commented Jun 8, 2021 at 23:41
Vyxal j
, 74 bitsv2 , 9.25 bytes
\|/:¦↳:t↲Ḋ
Bitstring:
01101010100010101110000101000000110001011111111110001111111100110011110001
Explained
\|/:¦↳:t↲Ḋ
\|/ # Split on "|"s
:¦ # Push the cumulative sums without popping. This acts as the space length before each section
↳ # Pad each item right with spaces to length in ^
:t↲ # And pad each item left with spaces to length of tail of ^
Ḋ # Append " {comment char}" to each
Implicitly join on newlines
💎
Created with the help of Luminespire and not this challenge.
-
\$\begingroup\$ You should probably make the Try it Online link have a test case that actually shows the task \$\endgroup\$noodle person– noodle person2023年10月08日 22:10:38 +00:00Commented Oct 8, 2023 at 22:10
JavaScript (Node.js), 79 bytes
c=>s=>s.split`|`.map((_,i,a)=>a.map(y=>i--?y.replace(/./g,' '):y).join``+' '+c)
+4 to add an extra space before #
Haskell, 96 bytes
f"".lines.map r
r '|'='\n'
r c=c
w=(' '<$)
f l(a:s)c=(w l++a++(w=<<c:s)++c):f(l++a)s c
f l s c=s
Eplanation
The function r
is defined to map pipe characters to newlines (r '|'='\n'
) and every other char to itself (r c=c
). lines.map r
thus first replaces all pipes in the input string with new lines (map r
), and then splits the string at the newlines with the build-in lines
function.
The main function f
iterates over this list of strings and constructs the output line by line with the expression w l++a++(w=<<c:s)++c
where
a
is the part of the code that appears on the current linec
is the comment character (as a string) that gets put at the end of the linew
is a function that replaces each character of a string by spaces (definde byw=(' '<$)
)l
is the string of code fragments to the left ofa
, which is put at the beginnig of the line as spaces withw l
s
is the list of remaining code strings which are also converted to spaces, concatenated, and put aftera
(c
is added tos
to get the extra space required before the comment character)
☾, (削除) 30 (削除ここまで) 26 chars
␛|␛|Ϝ🃌⋅s+→⍉⍉ᴍ⨁sn⨝
30 → 26 with help from Ganer
Uiua, (削除) 42 (削除ここまで) (削除) 36 (削除ここまで) (削除) 33 (削除ここまで) (削除) 26 (削除ここまで) 23 bytes
≡⊂≡↻1⬚@ \◇(⌝↘⧻)°/$"_|_"
Try it: Uiua pad
Revisiting one of my first Uiua solutions over a year later, I was able to shave off quite a few more bytes, in part thanks to the addition of ⌝↘
(anti-drop) to prepend a given number of spaces to a string, as well as changes made to filled \
(scan).
-
-
\$\begingroup\$ @chunes Cool! So many neat features I haven’t seen yet \$\endgroup\$noodle person– noodle person2024年03月07日 20:52:56 +00:00Commented Mar 7, 2024 at 20:52
PHP, (削除) 120 (削除ここまで) (削除) 117 (削除ここまで) (削除) 116 (削除ここまで) (削除) 110 (削除ここまで) 109 bytes
foreach($a=split('\|',$argv[1])as$i=>$t){$c=preg_replace('#.#',' ',$a);$c[$i]=$t;echo join($c)," $argv[2]
";}
or
foreach($a=split('\|',$argv[1])as$t){$c=preg_replace('#.#',' ',$a);$c[$i++|0]=$t;echo join($c)," $argv[2]
";}
MATL, (削除) 33 (削除ここまで) 31 bytes
'\|'0'|'hYXo8M&YbY:&YdtaZ)0ihYc
Explanation
The builtin function Yd
(blkdiag
), which builds a block-diagonal matrix from its inputs, does most of the work. The fill values in the matrix are 0, and char 0 is treated as a space for displaying purposes. The code would simply split on |
, build a matrix from the resulting blocks, convert to char, and append two columns with space and comment symbol.
However, the possibility of empty sections in the input string (削除) complicates (削除ここまで) makes the problem more interesting: the resulting block would be empty and thus wouldn't show in the resulting matrix.
To solve this, we introduce a char 0 before each |
, so no block will be empty; and then in the resulting char matrix we remove columns that are formed by char 0 only. A non-empty code section will have some printable ASCII char, and thus the columns it spans will survive. An empty section will contribute a row, but won't introduce an extra column.
'\|' % Push this string: source for regexp matching. It's just | escaped
0'|'h % Push a string formed by char 0 followed by | (no escaping needed)
YX % Input string implicitly. Replace first of the above string by the second
o % Convert from chars to code points. Gives a numeric vector
8M % Push '|' again
&Yb % Split numeric vector at occurences of | (the latter is automatically
% converted to its code point). This gives a cell array of numeric vectors
Y: % Unbox cell array: pushes the numeric vectors it contains
&Yd % Form a block-diagonal matrix from those vectors
ta % Duplicate. Compute vector that equals true for columns that have some
% nonzero value
Z) % Used that as a logical index (mask) for the columns of the matrix.
% This removes columns that contain only zeros
0ih % Input comment symbol and prepend char 0 (which will be displayed as space)
Yc % Append that to each row of the matrix. The matrix is automatically
% converted from code points to chars
% Display implicitly
-
1\$\begingroup\$ I'm vaguely disappointed that you didn't put your explanation in the format the OP mentioned \$\endgroup\$Random832– Random8322016年09月09日 18:30:29 +00:00Commented Sep 9, 2016 at 18:30
-
1\$\begingroup\$ @Random832 I don't use that format often. It uses up a lot of space, leaving little room for the explanations \$\endgroup\$Luis Mendo– Luis Mendo2016年09月09日 18:37:30 +00:00Commented Sep 9, 2016 at 18:37
-
\$\begingroup\$ Why is the escape needed in the first string? \$\endgroup\$Conor O'Brien– Conor O'Brien2016年09月11日 04:04:58 +00:00Commented Sep 11, 2016 at 4:04
-
\$\begingroup\$ @ConorO'Brien Good question. I never know which/when special symbols need escaping and which/when not. In this case,
|
(match subexpression before or after the|
) does need it, at least in the Matlab/Octave regexp engine \$\endgroup\$Luis Mendo– Luis Mendo2016年09月11日 11:01:03 +00:00Commented Sep 11, 2016 at 11:01
Ruby, (削除) 96 (削除ここまで) 80 bytes
->s,c{s.gsub(/(^|\|)([^|]*)/){" "*$`.count(t="^|")+2ドル+" "*(1+$'.count(t))+c+$/}}
See it on eval.in: https://eval.in/639012
I really ought to just learn Retina.
Pyth, 30 bytes
VJcE\|s[*ZdN*h--lsJZlNdQ)=+ZlN
or
jm+dQ.t.t+MC,.u*l+NYdJc+Ed\|kJ
Both are full programs that take input on STDIN of the comment string, and then the program string, newline-separated.
How they work
VJcE\|s[*ZdN*h--lsJZlNdQ)=+ZlN Program. Inputs: E, Q
cE\| Split E on "|"
J Assign to J
Implicit Z=0
V For N in that:
[ ) Create a list with elements:
*Zd Z spaces
N N
-lsJZ len(concatenate(J))-Z
- lN -len(N)
h +1
* d spaces
Q Q
s Concatenate the list
Implicitly print
=+ZlN Z=Z+len(N)
jm+dQ.t.t+MC,.u*l+NYdJc+Ed\|kJ Program. Inputs: E, Q
+Ed Add a trailing space to E
c \| Split that on "|"
J Assign to J
.u Cumulatively reduce J with:
k starting value empty string and
function N, Y ->
l+NY len(N+Y)
* d spaces
, J Two-element list of that and J
C Transpose
+M Map concatenation over that
.t Transpose, padding with spaces
.t Transpose again
m+dQ Map concatenation with Q over that
j Join on newlines
Implicitly print
Perl, 63 bytes
Includes +5 for -Xpi
Run with input on STDIN and comment character after -i:
perl -Xpi% esolang.pl <<< "Ab|Cd||ef"
esolang.pl
:
s/
/|/;s%(.*?)\|%$"x$`=~y/|//c.1ドル.$"x$'=~y/|//c." $^I
"%eg
Totally boring straightforward solution
Turtlèd, 35 bytes (noncompeting)
Takes one input, the last character is the comment character. Does not work with comment character as space, but I assume that isn't necessary.
!' [*.+(|' dl)r_]' r[*+.(|u)_][ .d]
Explanation:
! take input into string variable
' write space over current cell
[* ] while cell is not *
.+ write pointed char of string, stringpointer+1 (starts 0)
(| ) if current cell is |
' dl write space (over |), move down, left
r_ move right, write * if pointed char is
last char, else space
' r write space, move right
[* ] while cell is not *
+. increment pointer and write pointed char
(|u) if cell is |, move up
_ write * if the pointed char is the last char
[ ] while cell is not space
.d write the pointed char from string, move down
|
character would be nice, so you can explain yourself \$\endgroup\$|
) ? \$\endgroup\$