17
\$\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\$
6
  • \$\begingroup\$ There is definitely shorter ways to do at least the first one. I think \\` works. \$\endgroup\$ 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\$ Jun 22 at 17:14
  • 1
    \$\begingroup\$ Can we assume the input will always contain a backtick? So it won't be empty, nor only contain \? \$\endgroup\$ Aug 17 at 7:14
  • 1
    \$\begingroup\$ @KevinCruijssen Yes; I could've picked better wording but "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." means that the input will always be non-empty and end with a backtick. \$\endgroup\$
    – hyper-neutrino
    Aug 17 at 13:54

11 Answers 11

21
\$\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
  • 1
    \$\begingroup\$ Well, it's the right tool for the job I guess xD \$\endgroup\$
    – hyper-neutrino
    Jun 22 at 17:43
  • 8
    \$\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\$

JavaScript (Node.js), 32 bytes

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

Try it online!

Self-explanatory trivial solution.

\$\endgroup\$
0
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\$ Jun 23 at 17:50
  • \$\begingroup\$ @AaronMiller Ok, thanks! \$\endgroup\$
    – emanresu A
    Jun 23 at 20:01
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\$
2
\$\begingroup\$

05AB1E (legacy), 9 bytes

Z„\`.:Z.ø

Assumes the input will always contain a backtick.

Try it online or verify all test cases.

Explanation:

Z          # Get the maximum character of the (implicit) input-string (the backtick `)
    .:     # Replace all backticks in the input
 „\`       # with "\`"
      Z    # Then get the backtick again with the maximum (without popping the string)
       .ø  # Surround the string with this backtick as leading/trailing character
           # (after which the result is output implicitly)

Uses the legacy version of 05AB1E, because the newer 05AB1E version's min/max won't work on characters (it will return the first character, instead of the lowest/highest unicode character).

\$\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\$
0
\$\begingroup\$

Perl 5 + -pl140, 16 bytes

$_=$\.s/`/\\`/gr

Try it online!

Explanation

A relatively standard solution, this uses the -l140 flag to set $\ to ` which is automatically output after every call to print. Since we're also using the -p flag, $_ will be automatically printed so we set it to $\ concatenated with the /result of s/ubsitituing ` with \` in the (implicit) input stored in $_.


Perl 5 + -p0140l, 19 bytes

$\.="$_\\`"}{$\.=$/

Try it online!

Explanation

Slightly more interesting but longer, this answer uses the -0140 flag to split input on ` instead of newlines which means the first part of the code (before the }{) will be called for each part of the string (up to, but excluding `), appending the string itself concatenated with \\` to $\ which is pre-initialised to $/ (`) via -l. Once all the input has been parsed (}{), $/ is appended to $\ which is implicitly output due to -p.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ This seems like flag abuse \$\endgroup\$ Aug 18 at 15:36
  • \$\begingroup\$ @BrowncatPrograms The meta consensus is that flags class an answer as a different language so it wouldn't be competing with regular Perl 5 answers with just -p, for example. I can see why it looks like that though. I find it quite fascinating how much you can push into a Perl program with its abundant flag abuse though. Technically it's possible to add a -F argument that can perform the whole script, but that's less interesting than this in my opinion. Thank you for clarifying the downvote though! Have a great day! \$\endgroup\$ Aug 18 at 17:47
  • \$\begingroup\$ I'm aware of that rule, yeah (my usual respone). Usually my stance on this sort of thing is that it's absuing the rule for a free lower byte count, but this is actually a pretty interesting use of a flag. Let me know if you edit this any time soon so I can undo my downvote :p \$\endgroup\$ Aug 18 at 18:41
  • \$\begingroup\$ Yeah, I think it's good to question these things too, and the line is pretty fine. I do enjoy using -F to squeeze as much out of flags as possible, but I think there's something fun in abusing tools and predefined variables to get the most out of an interpreter. I guess it's not for everyone, but for me, realising that you can do so much with something not intended for that purpose is quite satisfying! As you said, it's all about having fun! \$\endgroup\$ Aug 18 at 20:30

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.