Normal and visual string reversion

17

1

Write some code that acts as a cat program. That is, to input a string and output it as is. But the normal reversion of your code must output the normal reversion of the input string. And the visual reversion of your code must output the visual reversion of the input string.

The normal reversion is the reversed character sequence of a string. The visual reversion is the normal reversion with the characters ()[]{}<> replaced by )(][}{>< respectively.

You could use any codepage that has the characters ()[]{}<> and is published before this challenge to define characters. You must use the same codepage for all your code. Your original code must be valid in this codepage, and applying either of your reversed code to that should yield itself.

This is , shortest code in bytes wins.

Example

For the string AB(XY), its normal and visual reversions are )YX(BA and (YX)BA respectively.

If your code (in a hypothetical language) is AB(XY), then the code )YX(BA and (YX)BA should output the normal and visual reversions of the input string respectively. And AB(XY) should act as a cat program.

jimmy23013

Posted 2016-05-19T04:20:30.097

Reputation: 34 042

Is it expected that input strings might contain newlines? – Digital Trauma – 2016-05-19T05:54:40.207

@DigitalTrauma Didn't think about that... Your program should support whatever the input routine you use could return (as long as it returns a string). And it should work on the original code itself. – jimmy23013 – 2016-05-19T06:23:22.217

1

Related http://codegolf.stackexchange.com/q/28190/15599

– Level River St – 2016-05-19T17:45:16.870

Answers

9

05AB1E, 16 bytes

Uses the fact that 05AB1E has a constant predefined to "()<>[]{}" and isn't affected the visually reversion.

Code:

,q‡"}{][><)("užR

Explanation:

,                 # Pop and print the input.
 q                # Quit.
  ‡"}{][><)("užR  # This part is ignored.

Try it online!


Reversed:

Ržu"()<>[]{}"‡q,

Explanation:

R                 # Reverse the input.
 žu               # Short for "()<>[]{}".
   "()<>[]{}"     # Push this string.
             ‡    # Transliterate (no-op, since everything is transliterated to itself).
              q   # Quit and implicitly print.
               ,  # This part is ignored.

Try it online!


Visually reversed:

Ržu")(><][}{"‡q,

Explanation:

R                 # Reverse the input.
 žu               # Short for "()<>[]{}".
   ")(><][}{"     # Push this string.   
             ‡    # Transliterate (giving the visually reversed string).
              q   # Quit and implicitly print.
               ,  # This part is ignored.       

Try it online!

Uses CP-1252 encoding.

Adnan

Posted 2016-05-19T04:20:30.097

Reputation: 41 965

6

CJam, 21 bytes

qe#ere$_"}{][><)("%Wq

Test it here.

Normal reversion:

qW%"()<>[]{}"_$ere#eq

Test it here.

Visual reversion:

qW%")(><][}{"_$ere#eq

Test it here.

Explanation

First, the normal code:

qe#ere$_"}{][><)("%Wq

This is simple: q reads all input, e# comments out the remainder of the program, and the input is printed implicitly at the end.

Now the normal reversion:

q            e# Read all input.
W%           e# Reverse it.
"()<>[]{}"   e# Push this string.
_$           e# Duplicate and sort it. However, the string is already sorted
             e# so we just get two copies of it.
er           e# Transliteration (i.e. character-wise substitution). But since the
             e# source and target string are identical, the reversed input
             e# is left unchanged.
e#eq            Just a comment...

And finally, the visual reversion:

q            e# Read all input.
W%           e# Reverse it.
")(><][}{"   e# Push this string.
_$           e# Duplicate and sort it. This gives us "()<>[]{}", i.e. the
             e# same string with each bracket pair swapped.
er           e# Transliteration (i.e. character-wise substitution). This
             e# time, this toggles all the brackets in the reversed input
             e# completing the visual reversion.
e#eq            Just a comment...

Martin Ender

Posted 2016-05-19T04:20:30.097

Reputation: 184 808

I had exactly this code myself. – jimmy23013 – 2016-05-19T08:28:36.910

6

Haskell, 124 bytes

Forward:

f=id
--esrever.q pam=2>1|esrever=2<1|f;x=x q;')'='(' q;'('=')' q;']'='[' q;'['=']' q;'>'='<' q;'<'='>' q;'}'='{' q;'{'='}' q

Normal reverse:

q '}'='{';q '{'='}';q '>'='<';q '<'='>';q ']'='[';q '['=']';q ')'='(';q '('=')';q x=x;f|1<2=reverse|1>2=map q.reverse--
di=f

Visual reverse:

q '{'='}';q '}'='{';q '<'='>';q '>'='<';q '['=']';q ']'='[';q '('=')';q ')'='(';q x=x;f|1>2=reverse|1<2=map q.reverse--
di=f

Each version defines a function f which takes and returns a string. In forward mode f is the identity function id, the rest of the code is a comment. In normal reverse mode the guard 1<2 in f is True, so reverse is applied. In visual reverse mode, the < is switched to > and the guard is False. The second guard is just the other way around and Truein visual mode, so additionally q is applied which switches "()<>{}[]".

f|1<2=reverse|1>2=map q.reverse      -- normal reverse mode
f|1>2=reverse|1<2=map q.reverse      -- visual reverse mode

Besides <and > in the guards, my code doesn't use any of the brackets, so they can't be messed up.

nimi

Posted 2016-05-19T04:20:30.097

Reputation: 34 639

6

Bash + common linux utilities, 51

  • 2 bytes saved thanks to @jimmy23013
  • 2 bytes saved thanks to @AdamKatz
#'><}{][)(' `P5BD706D5AC79E196iFe- cd` rt|ver|
\cat

Normal reversion:

tac\
|rev|tr `dc -eFi691E97CA5D607DB5P` '()[]{}<>'#

Visual reversion:

tac\
|rev|tr `dc -eFi691E97CA5D607DB5P` ')(][}{><'#

The main trick here is that the string ()[]{}<> is encoded as 691E97CA5D607DB5 (base 15). The resulting dc command will yield this same result after either kind of reversion. However the '()[]{}<>' string literal is sensitive to the reversal type.

tac is required to reverse order of input lines and rev is required to reverse the characters of each line. Any ASCII input should be acceptable.

Digital Trauma

Posted 2016-05-19T04:20:30.097

Reputation: 64 644

5

MATL, 26 24 22 16 bytes

Forward

DPEXSt'><}{][)('

Try it Online!

Explanation:

                % Implicitly grab the input as a string
D               % Pop the top of the stack and display it
P               % Tries to flip the top element on the stack but errors out
                % because the stack is empty. Program terminates.
EXSt'><}{][)('  % Not executed

Normal reversion:

'()[]{}<>'tSXEPD

Try it Online!

Explanation:

            % Implicitly grab input as a string
'()[]{}<>'  % String literal (search string)
tS          % Duplicate and sort to create the replacement string: '()[]{}<>'
XE          % Replace all entries in the input using the search and replacement strings. 
            % Corresponding characters in the strings are used for the replacement.
            % Effectively a no-op
P           % Flip the string
D           % Explicitly display result

Visual reversion:

')(][}{><'tSXEPD

Try it Online!

Explanation:

            % Implicitly grab the input as a string
')(][}{><'  % String literal (search string)
tS          % Duplicate and sort to create the replacement string: '()[]{}<>'
XE          % Replace all entries in the input using the search and replacement strings. 
            % Corresponding characters in the strings are used for the replacement. 
P           % Flip the result
D           % Explicitly display the result

Suever

Posted 2016-05-19T04:20:30.097

Reputation: 10 257

*The visual reversion is the normal reversion with the characters ()[]{}<> replaced by )(][}{>< respectively.* – Erik the Outgolfer – 2016-05-19T15:27:00.263

@ΈρικΚωνσταντόπουλος Updated. – Suever – 2016-05-19T15:39:11.540

3

GolfScript, 32 28 bytes

#%{=1-[=-\7?@.`{[(<>)]}.}%1-

Try it online!

Normal reversion:

-1%}.}])><([{`.@?7\-=[-1={%#

Try it online!

Visual reversion:

-1%{.{[(<>)]}`.@?7\-=]-1=}%#

Try it online!

The fact that an unmatched } terminates the program in GolfScript, made this fairly simple. However, I'm pretty sure that my code for swapping the brackets isn't optimal yet.

Martin Ender

Posted 2016-05-19T04:20:30.097

Reputation: 184 808

0

Python 2.7, 208 bytes

Forward

import sys#
print''.join(sys.stdin)#
#0:tpecxe
#"]1-::[)nidts.sys(nioj.'' tnirp"cexe:yrt
#0:tpecxe
#"(('<>{}[]()','><}{][)(')snartekam.s)etalsnart.[1-::](nidts.sys)nioj.'' tnirp"cexe:yrt
#s sa gnirts,sys tropmi

Normal reversion

import sys,string as s#
try:exec"print''.join)sys.stdin(]::-1[.translate)s.maketrans)'()[]{}<>',')(][}{><'(("#
except:0#
try:exec"print''.join(sys.stdin)[::-1]"#
except:0#
#)nidts.sys(nioj.''tnirp
#sys tropmi

https://eval.in/574639

Visual reversion

import sys,string as s#
try:exec"print''.join(sys.stdin)[::-1].translate(s.maketrans(')(][}{><','()[]{}<>'))"#
except:0#
try:exec"print''.join)sys.stdin(]::-1["#
except:0#
#(nidts.sys)nioj.''tnirp
#sys tropmi

https://eval.in/574638

All directions read from stdin until EOF.

Nothing super clever here. Trailing comments to only execute the forward vs the backward code, then an exec statement in a try block to catch syntax errors for the two different reversions.

Orez

Posted 2016-05-19T04:20:30.097

Reputation: 471