14
\$\begingroup\$

Inspired by I reverse the source code, ...

Your task, if you wish to accept it, is to reverse text and mirror select characters. Yes, yes, I know. Very surprising.

Input

A string, stdin, an array of characters, or any other source of text. All characters of the input are guaranteed to be in the printable ASCII range (32-126).

Output

The reversed text with some characters mirrored. To do this:

  • You replace any occurrences of the characters (, ), /, \, <, >, [, ], {, or } in the text with the corresponding "mirrored" character: ), (, \, /, >, <, ], [, }, or {.
  • and then reverse the text.

You may assume a maximum line length of 255.

Rules

  • Standard loopholes apply.
  • This is , which means that the shortest answer in each programming languages wins. Consequently, I will not be accepting an answer.

Testcases

(s)t/r\i<n>g[i]e{s} -> {s}e[i]g<n>i/r\t(s)
stringies -> seignirts
()/\<>[]{} -> {}[]<>/\()
{s}e[i]g<n>i/r\t(s) -> (s)t/r\i<n>g[i]e{s}
seignirts -> stringies
{}[]<>/\() -> ()/\<>[]{}
qwertyuiop database -> esabatad poiuytrewq

As seen above, the output should go back to the input if run through the program again.

Sandbox link

\$\endgroup\$
  • 2
    \$\begingroup\$ Related \$\endgroup\$ – Redwolf Programs Apr 1 at 20:28
  • 1
    \$\begingroup\$ Related: “Convenient palindrome” checker \$\endgroup\$ – xnor Apr 2 at 2:39
  • 1
    \$\begingroup\$ Also related: I'm symmetric, not palindromic! \$\endgroup\$ – xnor Apr 2 at 2:41
  • \$\begingroup\$ am I missing something, or the examples don't show the first rule in action? ie. shouldn't (s)t/r convert to r\t)s( ? \$\endgroup\$ – Gnudiff Apr 3 at 20:33
  • \$\begingroup\$ @Gnudiff The point is that you replace (){}/\[] with )(}{\/][ and then reverse it, so the text looks "mirrored". The title was planned to be "mirror text" but that was too short, so I had to add some filler. \$\endgroup\$ – S.S. Anne Apr 3 at 23:20

19 Answers 19

9
\$\begingroup\$

Python 3, 61 60 59 bytes

lambda s,t="(/<[{}]>\)":[(c+t)[~t.find(c)]for c in s][::-1]

Try it online!

-1 byte thanks to @xnor.

Changed the lookup string, so that the c in t test is eliminated.


Python 2, 63 bytes

lambda s,b='(/<[{}]>\)':map(dict(zip(b,b[::-1])).get,s,s)[::-1]

Try it online!

-1 byte thanks to @xnor.

Python 3, 65 64 bytes

lambda s,b=b'(/<[{}]>\)':s.translate(dict(zip(b,b[::-1])))[::-1]

Try it online!

Just for fun using idiomatic Python. (More idiomatic would be using str.maketrans, but it's way too long.)

-1 byte for both because the \ in '\)' doesn't need to be escaped.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ In TIO,all the cases pass without using the 'r' prefix, so 60 bytes? \$\endgroup\$ – dingledooper Apr 1 at 23:59
  • \$\begingroup\$ @dingledooper I was already editing my answer exactly for that :) \$\endgroup\$ – Bubbler Apr 2 at 0:02
  • \$\begingroup\$ I really like your test rig, especially those unicode characters. \$\endgroup\$ – xnor Apr 2 at 2:21
  • \$\begingroup\$ @xnor It's from Noodle9's answer. \$\endgroup\$ – Bubbler Apr 2 at 2:22
  • \$\begingroup\$ I found a byte-save with (c+t)[~t.find(c)] TIO. FWIW, Python 2 can do the second one for 63 as lambda s,b='(/<[{}]>\)':map(dict(zip(b,b[::-1])).get,s,s)[::-1] \$\endgroup\$ – xnor Apr 2 at 2:51
6
\$\begingroup\$

Stax, 2 bytes

:R

Try it online!

Well, um, yeah, Stax has a two-byte built-in that exactly does the job.

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ A Stax answer that you don't even have to pack to make it shorter, haha. xD \$\endgroup\$ – Kevin Cruijssen Apr 2 at 10:55
  • \$\begingroup\$ Why are you so hesitant to say that? I'm pretty sure that Canvas has a 1-byte built-in. \$\endgroup\$ – user92069 Apr 3 at 10:43
5
\$\begingroup\$

Bash + GNU utilities, 33 bytes

tr '(<[{/)>]}\' ')>]}\\(<[{/'|rev

Try it online!

A straightforward solution seems best here. Really the only golfing is making sure that the backslash appears last in one of the arguments to tr, because then that backslash doesn't have to be escaped, saving 1 byte.

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6),  65  59 bytes

Saved 6 bytes thanks to @Bubbler

I/O format: array of characters

a=>a.map(c=>(S='()/\\<>[]{}')[S.indexOf(c)^1]||c).reverse()

Try it online!

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 4 (or 3?) bytes

ºsK

Doesn't work for single-character inputs that are not in the mirror character-set (i.e. "a").
Thanks to @Grimmy for this version.

Try it online or verify all test cases.

º2äθ

Also works for single-character inputs that aren't in the mirror character-set.

Try it online or verify all test cases.

Explanation:

º     # Mirror the (implicit) input-string
      #  i.e. "(s)t/r\i<n>g[i]e{s}" → "(s)t/r\i<n>g[i]e{s}{s}e[i]g<n>i/r\t(s)"
 sK   # And remove the input-string from it
      #  → "{s}e[i]g<n>i/r\t(s)"
      # (after which it is output implicitly)

º     # Mirror the (implicit) input-string
      #  i.e. "(s)t/r\i<n>g[i]e{s}" → "(s)t/r\i<n>g[i]e{s}{s}e[i]g<n>i/r\t(s)"
 2ä   # Split it into two equal-sized parts
      #  → ["(s)t/r\i<n>g[i]e{s}","{s}e[i]g<n>i/r\t(s)"]
   θ  # Pop and only leave the mirrored second part
      #  → "{s}e[i]g<n>i/r\t(s)"
      # (after which it is output implicitly)
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 3-byter ºsK passes all test cases. It fails if the input is a single letter, though. \$\endgroup\$ – Grimmy Apr 6 at 8:50
  • \$\begingroup\$ @Grimmy Thanks, added :) \$\endgroup\$ – Kevin Cruijssen Apr 6 at 9:05
2
\$\begingroup\$

Python 3, 69 bytes

lambda s,t=r"()/\[]{}<>":[[c,t[t.find(c)^1]][c in t]for c in s][::-1]

Try it online!

Input: A sequence of character.
Output: The reversed string, as list of characters.

How:
For each character c:

  • c in t checks if c is a bracket.
  • t.find(c) finds the index of c in the bracket string. t.find(c)^1 finds the index of the mirrored bracket, which is 1 more or 1 less than the index of c.
  • [c,t[t.find(c)^1]][c in t] evaluates to the same character if c is not a bracket, otherwise evaluates to the mirrored bracket.
  • [::-1] reverses the result.
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Retina 0.8.2, 22 bytes

T`(/<[{}]>\\)`Ro
O^$`.

Try it online! Link includes test cases. Explanation:

T`(/<[{}]>\\)`Ro

Transliterate the string (/<[{}]>\) to its reverse.

O^$`.

Reverse the whole string.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Charcoal, 13 bytes

Pθ‖TFθ¿№βι←ι←

Try it online! Link is to verbose version of code. Explanation:

Pθ

Print the input string without moving the cursor.

‖T

Reflect the canvas. This mirrors the characters ()/\<>[]{}bdpq.

Fθ

Loop over the original string.

¿№βι

Is the current character a b, d, p, q (or any other lowercase letter that wouldn't have been transformed)?

←ι

If so then replace it with the original lowercase letter and move left.

Otherwise leave the current character, which might be a transformed ()/\<>[]{}.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

batch, 442 408 bytes

@Echo off&Setlocal EnableDelayedExpansion
for %%A in (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z)do Set %%A=%%A
for %%B in ("(=)",")=(","<=>",">=<","[=]","]=[","{=}","}={","/=\","\=/")do Set "%%~B"
Set "_=%~1"
Call :M
Echo(!$!
Endlocal
Exit /B
:M
For /L %%C in (0,1,256)do (
If "!_:~%%C,1!"=="" Exit /B
Set ".=!_:~%%C,1!"
For %%D in ("!.!")do IF "!%%~D!"=="" (Set "$= !$!")Else (Set "$=!%%~D!!$!")
)

Output:

enter image description here

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

C (gcc), 112 \$\cdots\$ 84 83 bytes

Saved a whopping 27 bytes thanks to Bubbler!!!
Saved a byte thanks to dingledooper!!!
Saved a byte thanks to ceilingcat!!!

char*t,*b="()/\\<>[]{}";f(char*s){*s&&putchar((t=index(b,*s))?b[t-b^1]:*s,f(s+1));}

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 85 bytes \$\endgroup\$ – Bubbler Apr 2 at 0:41
  • \$\begingroup\$ @Bubbler Amazing recusive refactoring - thanks! :-) \$\endgroup\$ – Noodle9 Apr 2 at 0:47
  • \$\begingroup\$ You can save 1 byte by replacing strchr with index instead. \$\endgroup\$ – dingledooper Apr 2 at 3:09
  • \$\begingroup\$ @dingledooper Nice one - thanks! :-) \$\endgroup\$ – Noodle9 Apr 2 at 6:37
  • \$\begingroup\$ @ceilingcat Clever - thanks! :-) \$\endgroup\$ – Noodle9 Apr 13 at 4:43
1
\$\begingroup\$

Python 3, 111 \$\cdots\$ 75 69 bytes

lambda s,b=r'()/\<>[]{}':[[c,b[b.find(c)^1]][c in b]for c in s][::-1]

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I really like your test rig, especially those Unicode characters. \$\endgroup\$ – xnor Apr 2 at 2:22
  • \$\begingroup\$ @xnor Thanks, have them memorised they're so useful. :D \$\endgroup\$ – Noodle9 Apr 2 at 6:40
1
\$\begingroup\$

Java (JDK), 111 bytes

s->{var a="(\\<[{}]>/)";for(int i=s.length,j;i-->0;System.out.print(j<0?s[i]:a.charAt(9-j)))j=a.indexOf(s[i]);}

Try it online!

Credits

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

J, 32 28 26 bytes

-4 bytes thanks to Bubbler!

-2 bytes thanks to FrownyFrog

|.rplc(;"0|.)@'([{/<>\}])'

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 28 bytes. You don't need to append anything to the replacement array. \$\endgroup\$ – Bubbler Apr 3 at 4:37
  • \$\begingroup\$ @Bubbler Of course I don't! I feel stupid, like so many times before... Thanks! \$\endgroup\$ – Galen Ivanov Apr 3 at 6:11
  • 1
    \$\begingroup\$ 26 bytes minor adjustment \$\endgroup\$ – FrownyFrog Apr 3 at 11:41
  • \$\begingroup\$ @FrownyFrog Thank you! \$\endgroup\$ – Galen Ivanov Apr 3 at 11:48
1
\$\begingroup\$

PowerShell, 69 58 bytes

+ to @Bubbler

-11 bytes thanks to @mazzy

$t='(/<[{}]>\)'
$args|%{$a="$_$t"[-1-($t|% i*f $_)]+$a}
$a

Expects input via splatting.

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ ? [--$i] instead [-++$i] \$\endgroup\$ – mazzy Apr 4 at 23:14
  • 1
    \$\begingroup\$ ? Try it online! \$\endgroup\$ – mazzy Apr 4 at 23:39
0
\$\begingroup\$

Red, 69 bytes

func[s][reverse s forall s[s/1: any[select"()([][{}{<></\/"s/1 s/1]]]

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Perl 5 -pl, 37 bytes

$_=reverse;y|(){}[]<>/\\|)(}{][><\\/|

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

PHP, 52 bytes

<?=strrev(strtr($argn,$a='(/<[{}]>\\)',strrev($a)));

Try it online!

Glad that this time PHP has an elegant way of doing it ^^

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Canvas, 1 byte

Exactly. A 1 byte built-in.

Try it here!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Icon, 76 68 bytes

procedure f(s)
r:=reverse
return map(r(s),t:="([{/<>\\}])",r(t))
end

Try it online!

| improve this answer | |
\$\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.