# Mirror and reverse text

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 • Related – Redwolf Programs Apr 1 at 20:28 • Related: “Convenient palindrome” checker – xnor Apr 2 at 2:39 • Also related: I'm symmetric, not palindromic! – xnor Apr 2 at 2:41 • 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( ? – Gnudiff Apr 3 at 20:33 • @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. – S.S. Anne Apr 3 at 23:20 ## 19 Answers # Python 3, 6160 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.

• In TIO,all the cases pass without using the 'r' prefix, so 60 bytes? – dingledooper Apr 1 at 23:59
• @dingledooper I was already editing my answer exactly for that :) – Bubbler Apr 2 at 0:02
• I really like your test rig, especially those unicode characters. – xnor Apr 2 at 2:21
• @xnor It's from Noodle9's answer. – Bubbler Apr 2 at 2:22
• 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] – xnor Apr 2 at 2:51

# Stax, 2 bytes

:R


Try it online!

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

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

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. # 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! # 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. º2äθ  Also works for single-character inputs that aren't in the mirror character-set. 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)  • 3-byter ºsK passes all test cases. It fails if the input is a single letter, though. – Grimmy Apr 6 at 8:50 • @Grimmy Thanks, added :) – Kevin Cruijssen Apr 6 at 9:05 # 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. # 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.

# Charcoal, 13 bytes

Ｐθ‖ＴＦθ¿№βι←ι←


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

Ｐθ


Print the input string without moving the cursor.

‖Ｔ


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

Ｆθ


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 ()/\<>[]{}.

# 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: # 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! • 85 bytes – Bubbler Apr 2 at 0:41 • @Bubbler Amazing recusive refactoring - thanks! :-) – Noodle9 Apr 2 at 0:47 • You can save 1 byte by replacing strchr with index instead. – dingledooper Apr 2 at 3:09 • @dingledooper Nice one - thanks! :-) – Noodle9 Apr 2 at 6:37 • @ceilingcat Clever - thanks! :-) – Noodle9 Apr 13 at 4:43 # 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! • I really like your test rig, especially those Unicode characters. – xnor Apr 2 at 2:22 • @xnor Thanks, have them memorised they're so useful. :D – Noodle9 Apr 2 at 6:40 # 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 # J, 3228 26 bytes -4 bytes thanks to Bubbler! -2 bytes thanks to FrownyFrog |.rplc(;"0|.)@'([{/<>\}])'  Try it online! • 28 bytes. You don't need to append anything to the replacement array. – Bubbler Apr 3 at 4:37 • @Bubbler Of course I don't! I feel stupid, like so many times before... Thanks! – Galen Ivanov Apr 3 at 6:11 • 26 bytes minor adjustment – FrownyFrog Apr 3 at 11:41 • @FrownyFrog Thank you! – Galen Ivanov Apr 3 at 11:48 # 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!

• ? [--$i] instead [-++$i] – mazzy Apr 4 at 23:14
• – mazzy Apr 4 at 23:39

# Red, 69 bytes

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


Try it online!

$_=reverse;y|(){}[]<>/\\|)(}{][><\\/|  Try it online! # PHP, 52 bytes <?=strrev(strtr($argn,$a='(/<[{}]>\\)',strrev($a)));


Try it online!

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

# Canvas, 1 byte

Exactly. A 1 byte built-in.

↔


Try it here!

# Icon, 76 68 bytes

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


Try it online!