# I palindrome the source code, you palindrome the input!

This is a rip-off of a rip-off of a rip-off of a rip-off of a rip-off. Go upvote those!

Your task, if you accept it, is to write a program/function that outputs/returns its input/args. The tricky part is that if I make your source code palindrome via duplicating reversed source code after the original code, the output must be returned "palindromize".

The source code will be only duplicated once.

## Examples

Let's say your source code is ABC and the input is xyz. If i run ABC the output must be xyz. But if i run ABCCBA instead, the output must be xyzzyx.

Put it in a table:

Input: "xyz"
code ABC => "xyz"
code ABCCBA => "xyzzyx" (aware that it isn't "xyzyx")

## Details

Your code before processing should return the input.

Your code after processing should return the input with reversed input following no matter what input is.

The process:

Your code, no matter what, will be followed with a reversed code after it. Even if it's already palindrome by itself.

Multiline code:

Multiline code will be treated as single line code but with \n(newline)

## Rules

• Input can be taken in any convenient format.
• Output can be in any convenient format as well. As long as it follow the input-reversed input order.
• Standard Loopholes are forbidden.

## Scoring

This is code-golf so the answer with the fewest amount of bytes wins.

• Can the output have a trailing newline? – Razetime Mar 24 at 8:10
• @Razetime yes, as long as the palindrome string is clear. – okie Mar 24 at 8:11
• Must the end character be duplicated, or may we choose that ABCBA outputs xyzyx instead? – Shaggy Mar 25 at 0:59
• the end character must be duplicated, precisely "mirrored", so the code have to be ABCCBA and outputs follow the rule too. – okie Mar 25 at 1:08
• Is it OK to output extra spaces at the beginning and end, e.g. to output " xyz " instead of "xyz"? – Robin Ryder Mar 25 at 8:35

# Haskell, Full program, 39 bytes

iddi=(++)<*>reverse;main=interact--
id

Try it online!

## Mirrored

iddi=(++)<*>reverse;main=interact--
iddi
--tcaretni=niam;esrever>*<)++(=iddi

Try it online!

Very simple approach for a full program. We have two "lines" (separated by ; rather than an actual line break) of boilerplate ending in -- and then the function id. When the program is reversed id gets changed to iddi, which we have defined to do the intended operation.

f x=x--x esrever++

Try it online!

## Mirrored

f x=x--x esrever++

++reverse x--x=x f

Try it online!

This one is also a pretty simple approach. We have a function f x=x and a payload hidden in a comment. When the code is revealed the payload extends our function with ++reverse x.

i=id
ko 'a' b=i id b
ko u m=i reverse m
u=flip ko
k s="ab">>=s
yf i=k<$>u erehw l=l fy=i erehw Try it online! ## Mirrored i=id ko 'a' b=i id b ko u m=i reverse m u=flip ko k s="ab">>=s yf i=k<$>u
erehw l=l
fy=i erehw  where i=yf
l=l where
u>$<k=i fy s=>>"ba"=s k ok pilf=u m esrever i=m u ok b di i=b 'a' ok di=i Try it online! The relevant function is fy. This one is likely not optimal. I will explain it when I shorten it down a bit. • currently accepted answer. Reason: a non-golf lang with no comments is astonishing. Will remain accepted if there is no better answer. – okie Mar 24 at 23:41 • @okie Please don't accept any answer that does not win according to the winning criterion (code golf in this case). – Bubbler Mar 24 at 23:44 • oh, sorry. Looks like I still failed to understand the site rule. – okie Mar 24 at 23:46 ## Python, 44434241 40 bytes a little trick with comment solves this problem -1 byte thanks to @Wheat Wizard with the ";" -1 byte thanks to @Jonathan Allan with the reversed line -1 byte thanks to @Jonathan Allan again with Walrus Operator -1 byte thanks to @qwatry for combining two line and saved an innocent byte from being used. print(n:=input(),end='')#)]1-::[n(tnirp mirrored print(n:=input(),end='')#)]1-::[n(tnirp print(n[::-1])#)''=dne,)(tupni=:n(tnirp Try it online! • You can use the same trick I did in my haskell answer to save a byte: put the input and the output on the same "line" using ; so you can comment them out together. – Wheat Wizard Mar 24 at 9:35 • Save the trailing newline by reversing the lines Try It Online – Jonathan Allan Mar 25 at 1:08 • ...save two more by using the walrus operator in Python 3.8+ TIO – Jonathan Allan Mar 25 at 1:11 • @okie Sure! :) On TIO, there is a place you can copy and paste the link, already formatted for Stach Exchange. – qwatry Mar 31 at 1:29 • you can save 2 extra bytes by defining n before the print then use the "end" to display it : n=input();print(end=n)#)]1-::[n(tnirp Try it online! – Jakque May 7 at 14:06 # APL (Dyalog Unicode), 3 bytes ⌽⊢, Try it online! A function that returns the string unmodified. ⌽⊢,,⊢⌽ Try it online!!enilno ti yrT A function that returns the string with its mirror appended. ### How it works ⌽⊢, ⍝ Input: a string S (char vector) , ⍝ Ravel of S (no-op on a char vector) ⌽ ⍝ Reverse of S ⊢ ⍝ Select the right one (therefore no-op as a whole) ⌽⊢,,⊢⌽ ,⊢⌽ ⍝ Same as above, but select the reverse of S ⊢, ⍝ Append the above to S (S + rev(S)) ⌽ ⍝ Reverse the entire string, ⍝ but no-op since the string is already palindrome # R, 65 bytes, no comments xx=paste0(x<-scan(,""),intToUtf8(rev(utf8ToInt(x))));+=cat;""+x Try it online! Palindromized: xx=paste0(x<-scan(,""),intToUtf8(rev(utf8ToInt(x))));+=cat;""+xx+"";tac=+;))))x(tnIoT8ftu(ver(8ftUoTtni,)"",(nacs-<x(0etsap=xx Try it online! The vanilla version defines + to be an alias of cat, so that the call ""+x outputs x. In the palindromized version, we call ""+xx+"" instead, outputting xx which is defined as the concatenation of x and its mirror. The code errors after producing the desired output. There is an extra space at the beginning and end of the output, which was allowed by OP. Previous version with same byte count, with comments but with no errors or extra spaces: ## R, 65 bytes function(x,xx=paste0(x,intToUtf8(rev(utf8ToInt(x)))),?=cat)?# x Try it online! Try it online palindromized! # JavaScript (SpiderMonkey), 31 bytes f=([a,...b])=>!a?'':a+f(b)//a+ Try it online! • Very clever to make use of recursion like this! – user81655 Mar 24 at 9:48 • On a philosophical note: I'm wondering if including the newline at the end is allowed. I'm not sure what the site rules are around this but in my understanding if someone submits a function as a solution (as opposed to a full program) then only a function and no other code outside of the function can be part of the submission. – user81655 Mar 24 at 9:48 • @user81655 but f= is not a part of this function too. And seems every function submission requires recursion would include it as a part of submission. Anyway, Firefox 86- did include the comment and new line as a part of function. So I believe it still valid since I can find out an implementation make it works. :) – tsh Mar 24 at 9:54 • Good point about the f=. Makes sense 👍 – user81655 Mar 24 at 10:06 # Vyxal, 2 bytes m? Try it Online! A blatant port of the 05ab1e answer. Imaginary brownie points for anyone who can give me an answer to the question posed by my answer. • I believe the answer is m. – xigoi Mar 24 at 8:19 • @xigoi that sounds like a very reasonable answer. How would you like your imaginary brownie points delivered? – lyxal Mar 24 at 8:22 • Preferably with a mail pigeon, thank you. – xigoi Mar 24 at 8:34 • m! also works as an answer, and also m? itself; when m equals "How do you do". – Neil Mar 24 at 10:58 • "Hey look, I have a new WeChat message! Let's check it out!" – 2x-1 Mar 24 at 12:46 # Charcoal, 3 bytes θ⮌ω Try it online! Palindromised: θ⮌ωω⮌θ Try it online! Explanation: θ Print the input. ⮌ω Print the reverse of the empty string. ω Print the empty string. ⮌θ Print the reverse of the input. # 05AB1E, 23 2 bytes -1 bytes thanks to @Razetime ºI Try it online! Try it online!!enilno ti yrT • That's Mostly correct - but with palindromed code, it will return "xyzyx" instead of "xyzzyx", that's maybe my fault that i didn't clarify the question. But I prefer "xyzzyx" as answer! – okie Mar 24 at 7:21 • Oh oops, I'll fixed that – Command Master Mar 24 at 7:23 • The question actually asks to mirror the output, not exactly palindromize, so just use º. – Razetime Mar 24 at 7:24 # Jelly, 3 bytes Ṛ;ṛ Try it online! ## Palindromized Ṛ;ṛṛ;Ṛ Try it online!!enilno ti yrT I believe a 2-byte solution is not possible. However, it's nice that this answer is a case-insensitive palindrome. # Japt, 5 bytes '1ê U Try it • palindromized '1ê UU ê1' Try it # Brainfuck, 7 bytes ,[.>,]+ ## Mirrored ,[.>,]++[,<.], This solution is probably a bit cheating as it is optical palindrome. So not just order of characters is reversed, but characters are too. > becomes < and [ becomes ]. +-, are kept the same as they are (almost) optically same when reversed. This was the only way as it is impossible to construct loops in brainfuck when code is reversed, because code like [...] would become ]...[ which is invalid code in brainfuck. ## How it works ,[ load first character and start loop . print last loaded character >, load next character into next cell ] if last character is null byte end loop + sets value of cell to 1 mirrored part: + sets value of cell to 2 [ starts loop , no more characters are there so it just sets cell to 0 <. print previous cell ] end loop when there are no more characters , does nothing; just reads another null byte • Not the type of palindrome I expect for, but a answer respect for. – okie Mar 24 at 23:44 # Stax, 5 bytes {m:xx Run and debug it Run and debug itti gubed dna nuR Fixed, and now 1 byte shorter. ## Explanation {m:xx {m map each char to itself :x escape regex special characters x push the input again xx:m{ x push the input once more x push the input again :m mirror it { error on unterminated block, exiting and displaying top of stack • Your palindrome is not a palindrome. The right one would be !{m:}xx}:m{!. – Eike Schulte Mar 25 at 10:27 • @EikeSchulte thanks for the help. – Razetime May 6 at 8:34 # JavaScript, 79 bytes m=process.argv[2]// console.log(m)// //)nioj.)(esrever.tilps.m(gol.elosnoc Palindromed: m=process.argv[2]// console.log(m)// //)nioj.)(esrever.tilps.m(gol.elosnoc console.log(m.split.reverse().join)// //)m(gol.elosnoc //]2[vgra.ssecorp=m Try it online! Try it online!!enilno ti yrT # Bash, 12 bytes tee a#a ver Try it online! Try it online (palindromized)! ## Explanation tee a#a ver # Copy STDIN to file a (with implicit output) rev a#a eet # Using file a as STDIN, reverse STDIN (with implicit output) # Java, 37 bytes s->s//)(esrever.)s(reffuBgnirtS wen+ Try it online! Mirrored: s->s//)(esrever.)s(reffuBgnirtS wen+ +new StringBuffer(s).reverse()//s>-s Try it online! • No way! Java being actually competitive! – A username Mar 27 at 20:52 # PHP -F, 32 bytes echo$argn;//;)ngra$(verrts ohce Try it online! palindromed: echo$argn;//;)ngra$(verrts ohce echo strrev($argn);//;ngra$ohce Try it online! Notice the new line at the end of the code # ><>, 17 bytes <o:\?(0~!:i <o>; Try it online! Palindromified: # ><>, 34 bytes <o:\?(0~!:i <o>; ;>o< i:!~0(?\:o< Try it online!!enilno ti yrT Explanation, if not palindromized: < | send the IP leftward (golfier due to the second line) i | push one character of input; stack = [i] : | duplicate and push the top value of the stack; stack = [i,i] ! | skip the next instruction ~ | pop (drop) the top element of the stack (skipped) 0 | push literal 0; stack = [i,i,0] ( | pop x then pop y and push 1 if x < y else push 0; stack = [i,bool] ? | pop the stack and skip the next instruction if the value was 0 (checks for -1 terminator); stack = [i] \ | send ip upwards, wrapping around vertically (only if input value was terminating -1) :o | non-destructively output the top value, thereby accumulating the input onto the stack as we go | or ; | halt When palindrominated, instead of wrapping back around to ;, we instead go through ~, dropping the top value of the stack (known to be -1), then hit >o<, outputting the top value of the stack until the stack is empty (i.e. the input in reverse) and then terminate via error. # JavaScript (V8), 39 bytes (s,ss)=>//nioj.esrever.]s...[+s|| s Try it online! When palindromised it becomes: (s,ss)=>//nioj.esrever.]s...[+s|| ss ||s+[...s].reverse.join//>=)ss,s( # JavaScript, 31 bytes s=>s//nioj.)(esrever.]s...[+ Try it online! Mirrored: s=>s//nioj.)(esrever.]s...[+ +[...s].reverse().join//s>=s Try it online! # Perl 5-p, 14 bytes #;esrever=._$

Try it online!

Note the trailing newline.

Perl -p prints the input after the code runs. The normal code does nothing to manipulate the input, being commented out. The palindromed code:

#;esrever=.__.=reverse;#

takes the input and appends the reverse of it to the input before printing.

Try it online!!enilno ti yrT

# GolfScript, 6 bytes

#%1-.

Try it online!

Try it online!(palendromized)

c x y=y<>x
iddi=reverse>>=c
id

The function for this part of the problem is id.

Unfortunately, TIO has an older version of GHC, which doesn't export the (<>) operator in Prelude. I can't use the standard (++) operator because it's a palindrome (more on this later).

## Mirrored

c x y=y<>x
iddi=reverse>>=c
iddi
c=>>esrever=iddi
x><y=y x c

The function for this part of the problem is iddi.

## How?

c x y=y<>x

This line defines a new function c, that is basically the concatenation operator (++) with its arguments swapped. I was forced to use (<>) instead of (++) because the latter is a palindrome and would mess things up in the mirrored part.

iddi=reverse>>=c

This line defines a new function iddi, which basically implements the "mirror" functionality. Expanding the (>>=) operator for clarity, the definition can be rewritten as

iddi s=c(reverse s)s -- = s<>reverse s

c=>>esrever=iddi
x><y=y x c

This part of the mirrored code is just garbage. Fortunately, it's syntactically correct garbage. These two line define two new operators, (=>>) and (><), in terms of the previously defined functions iddi and c, whose names are (very conveniently) palindromic strings.

The reason why I can't use (++) instead of (<>) is that in this case the mirrored part of the code would try to redefine (++), leading to an unpleasant result.

• I have some reservations as to whether this is a valid answer. Requiring differently named functions for different parts is at least a little bit cheaty. – Wheat Wizard Mar 24 at 21:25
• @WheatWizard I totally understand, and I had some reservations as well, but I believe that it is at least possible to interpret the consensus on anonymous functions in a way that's compatible with my answer. The point is that, separately, the unmirrored and the mirrored part would be considered as valid submissions, so I don't see why the submission as a whole should be considered invalid. I will, however, wait for the OP's opinion and, if necessary, mark this answer as "not competitive". – Delfad0r Mar 24 at 21:33
• I see what you are saying now. I think that in general anonymous function submissions like this are a little fishy for source layout challenges since it only meets the requirements in this very specific layout. Where I view the point of an anonymous function as being that it can be called from anywhere. This is a matter of my opinion mostly. I'm not downvoting, or suggesting it be deleted, and I'm certainly not speaking as a moderator here. – Wheat Wizard Mar 24 at 21:39
• @WheatWizard By the way I think this trick could be used to improve the score on your "no comments" submission: Don't try it online!. But at this point, I don't know what to do: it would feel like stealing to post this as my own, since the most ingenious part is, in my opinion, the where trick. However, I would be fine with you adding this to your answer (if you wanted to, of course). In this case, I might consider deleting mine. – Delfad0r Mar 24 at 21:53
• Feel free to use as much of my answer as you wish. I won't consider it "stealing". I'm probably not going to edit another solution in since it is a bit long, and I'm currently trying to edit down my explanation. – Wheat Wizard Mar 25 at 6:56

# Pxem (esolang-box notation), 43 bytes.

Some unprintables are escaped.

.e
e.z\001.rXX.a.w.s.p.d.a.w.c.o.i.cED.-.+.ae.

## Parindromed

.e
e.z\001.rXX.a.w.s.p.d.a.w.c.o.i.cED.-.+.ae..ea.+.-.DEc.i.o.c.w.a.d.p.s.w.a.XXr.\001z.e
e.

For parindromed.

XX.z
# MAIN ROUTINE
# call with empty stack
.a.eXX.z

# SUBROUTINE
# should be called with either
.aXX.z
# if top is not e, then push zero
# else e is popped
.ae.z\001.rXX.aXX.z
# if top is not zero, then print and pop each item
# (except top .; discarded) and return
.a.w.s.p.d.aXX.z
# cat program BUT preserve each item
# XXX: where does EOF: -1 go?
.a.w.c.o.i.cED.-.+.aXX.z
# NOTE if not parindromed, the next section and so far is e.,
# which .e and so far would not be there
# if parindromed, call with stack
.ae..eXX.z
# garbage, then reaching to .D returns to main section
.aa.+.-.DXX.z
# and garbage
.aEc.i.o.c.w.a.d.p.s.w.a.XXr.\001z.e\ne.

Try it online!

# CJam, 8 bytes

qe#e%W_

Try it online!

It has a newline at the end.

Reversed:

qe#e%W_

_W%e#eq

Try it online!

• your palindromed code outputs nothing, which should be output palindrome string instead – okie May 7 at 3:38
• @okie Sorry! I mistyped the palindromed code, it was missing a "W". It works OK now. – Iaka Noe May 7 at 3:59
• also, you seems to palindrome the code wrong way, it still work tho try it – okie May 7 at 4:07
• new line is palindromed also. – okie May 7 at 4:07
• Yep, true. Thanks! – Iaka Noe May 7 at 4:25