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 code-golf, 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.
19 Answers 19
Python 3, (削除) 61 (削除ここまで) (削除) 60 (削除ここまで) 59 bytes
lambda s,t="(/<[{}]>\)":[(c+t)[~t.find(c)]for c in s][::-1]
-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]
-1 byte thanks to @xnor.
Python 3, (削除) 65 (削除ここまで) 64 bytes
lambda s,b=b'(/<[{}]>\)':s.translate(dict(zip(b,b[::-1])))[::-1]
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.
-
\$\begingroup\$ In TIO,all the cases pass without using the 'r' prefix, so 60 bytes? \$\endgroup\$dingledooper– dingledooper2020年04月01日 23:59:27 +00:00Commented Apr 1, 2020 at 23:59
-
\$\begingroup\$ @dingledooper I was already editing my answer exactly for that :) \$\endgroup\$Bubbler– Bubbler2020年04月02日 00:02:15 +00:00Commented Apr 2, 2020 at 0:02
-
\$\begingroup\$ I really like your test rig, especially those unicode characters. \$\endgroup\$xnor– xnor2020年04月02日 02:21:05 +00:00Commented Apr 2, 2020 at 2:21
-
\$\begingroup\$ @xnor It's from Noodle9's answer. \$\endgroup\$Bubbler– Bubbler2020年04月02日 02:22:01 +00:00Commented Apr 2, 2020 at 2:22
-
-
2\$\begingroup\$ A Stax answer that you don't even have to pack to make it shorter, haha. xD \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年04月02日 10:55:39 +00:00Commented 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\$user92069– user920692020年04月03日 10:43:37 +00:00Commented Apr 3, 2020 at 10:43
Bash + GNU utilities, 33 bytes
tr '(<[{/)>]}\' ')>]}\\(<[{/'|rev
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()
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)
-
\$\begingroup\$ 3-byter
ºsKpasses all test cases. It fails if the input is a single letter, though. \$\endgroup\$Grimmy– Grimmy2020年04月06日 08:50:16 +00:00Commented Apr 6, 2020 at 8:50 -
\$\begingroup\$ @Grimmy Thanks, added :) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年04月06日 09:05:14 +00:00Commented Apr 6, 2020 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]
Input: A sequence of character.
Output: The reversed string, as list of characters.
How:
For each character c:
c in tchecks ifcis a bracket.t.find(c)finds the index ofcin the bracket string.t.find(c)^1finds the index of the mirrored bracket, which is 1 more or 1 less than the index ofc.[c,t[t.find(c)^1]][c in t]evaluates to the same character ifcis 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
Pθ‖TFθ¿Noβι←ι←
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.
¿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 ()/\<>[]{}.
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));}
-
-
\$\begingroup\$ @Bubbler Amazing recusive refactoring - thanks! :-) \$\endgroup\$Noodle9– Noodle92020年04月02日 00:47:05 +00:00Commented Apr 2, 2020 at 0:47
-
\$\begingroup\$ You can save 1 byte by replacing
strchrwithindexinstead. \$\endgroup\$dingledooper– dingledooper2020年04月02日 03:09:45 +00:00Commented Apr 2, 2020 at 3:09 -
\$\begingroup\$ @dingledooper Nice one - thanks! :-) \$\endgroup\$Noodle9– Noodle92020年04月02日 06:37:35 +00:00Commented Apr 2, 2020 at 6:37
-
\$\begingroup\$ @ceilingcat Clever - thanks! :-) \$\endgroup\$Noodle9– Noodle92020年04月13日 04:43:39 +00:00Commented Apr 13, 2020 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]
-
\$\begingroup\$ I really like your test rig, especially those Unicode characters. \$\endgroup\$xnor– xnor2020年04月02日 02:22:50 +00:00Commented Apr 2, 2020 at 2:22
-
\$\begingroup\$ @xnor Thanks, have them memorised they're so useful. :D \$\endgroup\$Noodle9– Noodle92020年04月02日 06:40:13 +00:00Commented Apr 2, 2020 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]);}
Credits
- -1 byte thanks to Kevin Cruijssen
-
1\$\begingroup\$ Woops.. 127 bytes in that case. My bad. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年04月02日 11:04:32 +00:00Commented Apr 2, 2020 at 11:04
J, (削除) 32 (削除ここまで) (削除) 28 (削除ここまで) 26 bytes
-4 bytes thanks to Bubbler!
-2 bytes thanks to FrownyFrog
|.rplc(;"0|.)@'([{/<>\}])'
-
1
-
\$\begingroup\$ @Bubbler Of course I don't! I feel stupid, like so many times before... Thanks! \$\endgroup\$Galen Ivanov– Galen Ivanov2020年04月03日 06:11:16 +00:00Commented Apr 3, 2020 at 6:11
-
1\$\begingroup\$ 26 bytes minor adjustment \$\endgroup\$FrownyFrog– FrownyFrog2020年04月03日 11:41:09 +00:00Commented Apr 3, 2020 at 11:41
-
\$\begingroup\$ @FrownyFrog Thank you! \$\endgroup\$Galen Ivanov– Galen Ivanov2020年04月03日 11:48:07 +00:00Commented Apr 3, 2020 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.
-
1\$\begingroup\$ ?
[--$i]instead[-++$i]\$\endgroup\$mazzy– mazzy2020年04月04日 23:14:38 +00:00Commented Apr 4, 2020 at 23:14 -
1\$\begingroup\$ ? Try it online! \$\endgroup\$mazzy– mazzy2020年04月04日 23:39:34 +00:00Commented Apr 4, 2020 at 23:39
PHP, 52 bytes
<?=strrev(strtr($argn,$a='(/<[{}]>\\)',strrev($a)));
Glad that this time PHP has an elegant way of doing it ^^
Icon, (削除) 76 (削除ここまで) 68 bytes
procedure f(s)
r:=reverse
return map(r(s),t:="([{/<>\\}])",r(t))
end
(){}/\[]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\$