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

asked Apr 1, 2020 at 20:13
\$\endgroup\$
5
  • 2
    \$\begingroup\$ Related \$\endgroup\$ Commented Apr 1, 2020 at 20:28
  • 1
    \$\begingroup\$ Related: "Convenient palindrome" checker \$\endgroup\$ Commented Apr 2, 2020 at 2:39
  • 1
    \$\begingroup\$ Also related: I'm symmetric, not palindromic! \$\endgroup\$ Commented Apr 2, 2020 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\$ Commented Apr 3, 2020 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\$ Commented Apr 3, 2020 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.

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

answered Apr 2, 2020 at 0:12
\$\endgroup\$
2
  • 2
    \$\begingroup\$ A Stax answer that you don't even have to pack to make it shorter, haha. xD \$\endgroup\$ Commented Apr 2, 2020 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\$ Commented Apr 3, 2020 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.

answered Apr 1, 2020 at 23:13
\$\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!

answered Apr 1, 2020 at 20:57
\$\endgroup\$
0
3
\$\begingroup\$

05AB1E, 4 (or 3?) bytes

osK

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.

o2äθ

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

Try it online or verify all test cases.

Explanation:

o # 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)
o # 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)
answered Apr 2, 2020 at 10:54
\$\endgroup\$
2
  • \$\begingroup\$ 3-byter ºsK passes all test cases. It fails if the input is a single letter, though. \$\endgroup\$ Commented Apr 6, 2020 at 8:50
  • \$\begingroup\$ @Grimmy Thanks, added :) \$\endgroup\$ Commented Apr 6, 2020 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.
answered Apr 1, 2020 at 20:46
\$\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.

answered Apr 1, 2020 at 20:56
\$\endgroup\$
2
\$\begingroup\$

Charcoal, 13 bytes

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

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

Print the input string without moving the cursor.

‖T

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

Loop over the original string.

¿Noβι

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

answered Apr 1, 2020 at 23:07
\$\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

answered Apr 3, 2020 at 4:02
\$\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!

answered Apr 2, 2020 at 0:17
\$\endgroup\$
5
  • \$\begingroup\$ 85 bytes \$\endgroup\$ Commented Apr 2, 2020 at 0:41
  • \$\begingroup\$ @Bubbler Amazing recusive refactoring - thanks! :-) \$\endgroup\$ Commented Apr 2, 2020 at 0:47
  • \$\begingroup\$ You can save 1 byte by replacing strchr with index instead. \$\endgroup\$ Commented Apr 2, 2020 at 3:09
  • \$\begingroup\$ @dingledooper Nice one - thanks! :-) \$\endgroup\$ Commented Apr 2, 2020 at 6:37
  • \$\begingroup\$ @ceilingcat Clever - thanks! :-) \$\endgroup\$ Commented Apr 13, 2020 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!

answered Apr 1, 2020 at 20:34
\$\endgroup\$
2
  • \$\begingroup\$ I really like your test rig, especially those Unicode characters. \$\endgroup\$ Commented Apr 2, 2020 at 2:22
  • \$\begingroup\$ @xnor Thanks, have them memorised they're so useful. :D \$\endgroup\$ Commented Apr 2, 2020 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

answered Apr 2, 2020 at 10:11
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Woops.. 127 bytes in that case. My bad. \$\endgroup\$ Commented Apr 2, 2020 at 11:04
1
\$\begingroup\$

Canvas, 1 byte

Exactly. A 1 byte built-in.

Try it here!

answered Apr 3, 2020 at 10:32
\$\endgroup\$
1
\$\begingroup\$

J, (削除) 32 (削除ここまで) (削除) 28 (削除ここまで) 26 bytes

-4 bytes thanks to Bubbler!

-2 bytes thanks to FrownyFrog

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

Try it online!

answered Apr 2, 2020 at 12:36
\$\endgroup\$
4
  • 1
    \$\begingroup\$ 28 bytes. You don't need to append anything to the replacement array. \$\endgroup\$ Commented Apr 3, 2020 at 4:37
  • \$\begingroup\$ @Bubbler Of course I don't! I feel stupid, like so many times before... Thanks! \$\endgroup\$ Commented Apr 3, 2020 at 6:11
  • 1
    \$\begingroup\$ 26 bytes minor adjustment \$\endgroup\$ Commented Apr 3, 2020 at 11:41
  • \$\begingroup\$ @FrownyFrog Thank you! \$\endgroup\$ Commented Apr 3, 2020 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!

answered Apr 4, 2020 at 20:57
\$\endgroup\$
2
  • 1
    \$\begingroup\$ ? [--$i] instead [-++$i] \$\endgroup\$ Commented Apr 4, 2020 at 23:14
  • 1
    \$\begingroup\$ ? Try it online! \$\endgroup\$ Commented Apr 4, 2020 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!

answered Apr 2, 2020 at 6:27
\$\endgroup\$
0
\$\begingroup\$

Perl 5 -pl, 37 bytes

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

Try it online!

answered Apr 2, 2020 at 20:51
\$\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 ^^

answered Apr 3, 2020 at 8:47
\$\endgroup\$
0
\$\begingroup\$

Icon, (削除) 76 (削除ここまで) 68 bytes

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

Try it online!

answered Apr 2, 2020 at 11:43
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.