17
\$\begingroup\$

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.

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

24 Answers 24

14
\$\begingroup\$

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.

Haskell, Function, 20 bytes

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.

Haskell, Function, No Comments, 95 bytes

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ currently accepted answer. Reason: a non-golf lang with no comments is astonishing. Will remain accepted if there is no better answer. \$\endgroup\$ – okie Mar 24 at 23:41
  • 1
    \$\begingroup\$ @okie Please don't accept any answer that does not win according to the winning criterion (code golf in this case). \$\endgroup\$ – Bubbler Mar 24 at 23:44
  • \$\begingroup\$ oh, sorry. Looks like I still failed to understand the site rule. \$\endgroup\$ – okie Mar 24 at 23:46
9
\$\begingroup\$

Python, 44 43 42 41 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!

\$\endgroup\$
8
  • 5
    \$\begingroup\$ 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. \$\endgroup\$ – Wheat Wizard Mar 24 at 9:35
  • 1
    \$\begingroup\$ Save the trailing newline by reversing the lines Try It Online \$\endgroup\$ – Jonathan Allan Mar 25 at 1:08
  • 1
    \$\begingroup\$ ...save two more by using the walrus operator in Python 3.8+ TIO \$\endgroup\$ – Jonathan Allan Mar 25 at 1:11
  • 1
    \$\begingroup\$ @okie Sure! :) On TIO, there is a place you can copy and paste the link, already formatted for Stach Exchange. \$\endgroup\$ – qwatry Mar 31 at 1:29
  • 1
    \$\begingroup\$ 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! \$\endgroup\$ – Jakque May 7 at 14:06
8
\$\begingroup\$

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
\$\endgroup\$
8
\$\begingroup\$

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!

\$\endgroup\$
5
\$\begingroup\$

JavaScript (SpiderMonkey), 31 bytes

f=([a,...b])=>!a?'':a+f(b)//a+

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Very clever to make use of recursion like this! \$\endgroup\$ – user81655 Mar 24 at 9:48
  • \$\begingroup\$ 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. \$\endgroup\$ – user81655 Mar 24 at 9:48
  • \$\begingroup\$ @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. :) \$\endgroup\$ – tsh Mar 24 at 9:54
  • \$\begingroup\$ Good point about the f=. Makes sense 👍 \$\endgroup\$ – user81655 Mar 24 at 10:06
4
\$\begingroup\$

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.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ I believe the answer is m. \$\endgroup\$ – xigoi Mar 24 at 8:19
  • 1
    \$\begingroup\$ @xigoi that sounds like a very reasonable answer. How would you like your imaginary brownie points delivered? \$\endgroup\$ – lyxal Mar 24 at 8:22
  • 3
    \$\begingroup\$ Preferably with a mail pigeon, thank you. \$\endgroup\$ – xigoi Mar 24 at 8:34
  • 1
    \$\begingroup\$ m! also works as an answer, and also m? itself; when m equals "How do you do". \$\endgroup\$ – Neil Mar 24 at 10:58
  • \$\begingroup\$ "Hey look, I have a new WeChat message! Let's check it out!" \$\endgroup\$ – 2x-1 Mar 24 at 12:46
4
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

05AB1E, 2 3 2 bytes

-1 bytes thanks to @Razetime

ºI

Try it online!

Try it online!!enilno ti yrT

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 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! \$\endgroup\$ – okie Mar 24 at 7:21
  • \$\begingroup\$ Oh oops, I'll fixed that \$\endgroup\$ – Command Master Mar 24 at 7:23
  • 2
    \$\begingroup\$ The question actually asks to mirror the output, not exactly palindromize, so just use º. \$\endgroup\$ – Razetime Mar 24 at 7:24
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

Japt, 5 bytes

'1ê U

Try it

  • palindromized

    '1ê UU ê1'

Try it

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ Not the type of palindrome I expect for, but a answer respect for. \$\endgroup\$ – okie Mar 24 at 23:44
3
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ Your palindrome is not a palindrome. The right one would be !{m:}xx}:m{!. \$\endgroup\$ – Eike Schulte Mar 25 at 10:27
  • \$\begingroup\$ @EikeSchulte thanks for the help. \$\endgroup\$ – Razetime May 6 at 8:34
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

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)
\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ No way! Java being actually competitive! \$\endgroup\$ – A username Mar 27 at 20:52
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

><>, 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.

\$\endgroup\$
1
\$\begingroup\$

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(
\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

GolfScript, 6 bytes

#%1-.

Try it online!

Try it online!(palendromized)

\$\endgroup\$
0
\$\begingroup\$

Haskell (GHC 8.4.1), 32 bytes, no comments

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.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Wheat Wizard Mar 24 at 21:25
  • \$\begingroup\$ @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". \$\endgroup\$ – Delfad0r Mar 24 at 21:33
  • \$\begingroup\$ 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. \$\endgroup\$ – Wheat Wizard Mar 24 at 21:39
  • \$\begingroup\$ @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. \$\endgroup\$ – Delfad0r Mar 24 at 21:53
  • \$\begingroup\$ 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. \$\endgroup\$ – Wheat Wizard Mar 25 at 6:56
0
\$\begingroup\$

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.

With comments

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!

\$\endgroup\$
0
0
\$\begingroup\$

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!

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.