15
\$\begingroup\$

Background

As far as we can tell from trying everything (besides cheating with ZWSPs), you can't type `\ in Stack Exchange chat. However, you can type \` by doing `\\``.

So, if you want to tell someone in chat how to type backslash-backtick, you'd need to type out `\`\\`\`` to format `\\`` into a codeblock.

Challenge

Your challenge will be to take a string of backticks and backslashes and output a string of backticks and backslashes that will format into that (as a code block) when you type it into SE chat. You can test this in the chat sandbox.

One important thing to note is that a backslash prevents the next backtick from starting or ending a code block, but a backslash will not actually make the next backslash not escape - basically, three backslashes and a backtick will become two literal backslashes and a literal backtick, the latter of which will not function as a code block delimiter anymore.

Thus, one way to solve this challenge is to take each block of backslashes (including the zero-length ones between consecutive backticks) and add a backslash to each, then surround the string in backticks.

You may assume it is possible; that is, the input will not end with a backslash. You also do not need to handle empty input.

Input / Output

Input and output will be taken and provided as a string of backticks and backslashes. For this challenge, you must use these characters specifically. You can input or output as a flat list of characters instead, and input and output do not have to use the same format.

Test Cases

These test cases follow my example solution. I don't actually know if another solution exists, but if you can find an alternative that works in SE chat and isn't just the aforementioned approach, I may offer a bounty.

\`                => `\\``
`\\`\`\\``        => `\`\\\`\\`\\\`\``
`\`\\\`\\`\\\`\`` => `\`\\`\\\\`\\\`\\\\`\\`\``
``````            => `\`\`\`\`\`\``
\\\\\\\\\`        => `\\\\\\\\\\``
`                 => `\``
`\`\`\`\`\`\`\`   => `\`\\`\\`\\`\\`\\`\\`\\``

Reference Implementation in Python; you can input as many lines as you want and it will print a valid answer to each using my approach.

Standard Loopholes are disallowed as usual.

This is a challenge, so the shortest code in bytes per language wins in its language category. Happy golfing!

\$\endgroup\$
3
  • \$\begingroup\$ There is definitely shorter ways to do at least the first one. I think \\` works. \$\endgroup\$ – StackMeter Jun 22 at 17:13
  • 3
    \$\begingroup\$ @StackMeter That won't format into a code block. I thought that was clear, but I'll add a note. \$\endgroup\$ – hyper-neutrino Jun 22 at 17:13
  • \$\begingroup\$ Oh - I see now. \$\endgroup\$ – StackMeter Jun 22 at 17:14
18
\$\begingroup\$

Pip, 6 bytes

RP``.q

Try it online!

Explanation

Um... well, this is interesting.

It just so happens that Pip's Pattern type (used for regex) is delimited with backticks. And that backticks can be escaped within a Pattern using backslashes.

... And furthermore, that the code for generating the Pip repr of a Pattern is apparently incorrect, because it should escape the backslashes too, but in fact it only escapes the backticks. Which is exactly the behavior this question asks for.

"It's not a bug, it's a feature"? ¯\_(ツ)_/¯

     q  Read a line of stdin
  ``.   Cast it to a Pattern by concatenating an empty Pattern to it
RP      Get the repr

Okay, I've looked at this again and I think it's not actually a bug. The reason why I thought it was a bug was because some of the inputs are not possible to enter as Pattern literals. Since they are "impossible" Patterns, it makes sense that their reprs are not correctly formed representations of Pattern literals.

Input  Repr    Comment
`      `\``    Properly formed: backtick is escaped with a backslash
\\`    `\\\``  Properly formed: backslash is also escaped with a backslash
\`     `\\``   Improperly formed: first backtick is not escaped

You can't enter a regex for \` in Pip because \` is not a properly formed regex. (If you want a regex that matches the string \`, you should use \\`, which is entered as `\\\``. Clear as mud? Great.)

In summary, inputs with only even-length runs of backslashes output well-formed reprs, but if there is an odd run of backslashes, all bets are off.

\$\endgroup\$
2
  • \$\begingroup\$ Well, it's the right tool for the job I guess xD \$\endgroup\$ – hyper-neutrino Jun 22 at 17:43
  • 6
    \$\begingroup\$ @hyper-neutrino The right bug for the job. \$\endgroup\$ – Adám Jun 22 at 18:07
8
\$\begingroup\$

Python 3, 35 bytes

lambda s:'`%s`'%s.replace('`','\`')

Try it online!

-1 thanks to dingledooper.

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

Jelly, 11 bytes

ṣ”`j⁾\`⁾``j

Try it online!

Jelly version of Lynn's approach, found independently

How it works

ṣ”`j⁾\`⁾``j - Main link. Takes a string S on the left
ṣ”`         - Split S on backtick
   j⁾\`     - Join with "\`", essentially replacing backticks with "\`"
       ⁾``j - Surround with backticks
\$\endgroup\$
3
\$\begingroup\$

Vyxal 2.3, 7 bytes

\`‛\`Vq

Up until recently, q (uneval) simply surrounded the string in backticks. Now it escapes backslashes and backticks, which we don't want.

Also up until recently, strings in the input couldn't be delimited by backticks, but now they can so input is misparsed.

The rest is just replacing backticks with a backtick and a backslash.

So no TIO, but feel free to download the interpreter from the above link and try it out.

\$\endgroup\$
2
  • \$\begingroup\$ This doesn't work properly. You need to swap the \ and the ` in the two-char string, and move the q to the end: \`‛\`Vq \$\endgroup\$ – Aaron Miller Jun 23 at 17:50
  • \$\begingroup\$ @AaronMiller Ok, thanks! \$\endgroup\$ – A username Jun 23 at 20:01
2
\$\begingroup\$

JavaScript (Node.js), 32 bytes

n=>'`'+n.replace(/`/g,'\\`')+'`'

Try it online!

Self-explanatory trivial solution.

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

Japt, 12 bytes

Look, Ma, no escape characters!

"`{r'`_i'\}`

Try it

\$\endgroup\$
1
  • \$\begingroup\$ look ma no unicode too lol \$\endgroup\$ – wasif Jun 23 at 6:44
2
\$\begingroup\$

QuadR, 11 bytes

^|$
`
`
\\`

Try it online!

Simply replaces (i.e. inserts at) the beginning/end with a backtick,

and any backtick with a backslash and a backtick.

Equivalent to the Dyalog APL function '^|$' ''⎕R'' '\`'

\$\endgroup\$
1
\$\begingroup\$

PowerShell, 31 bytes

'`'+$args.replace('`','\`')+'`'

Try it online!

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

Red, 42 bytes

func[s][rejoin[t:"`"replace/all s t"\`"t]]

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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