Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

A palindrome is a string that is the same forwards and backwards, such as "racecar".

Write a program in some language L, that takes any program P1 in language L as input, and outputs a palindromic program P2 in language L that does the same thing as P1.

You do not need to worry about handling input programs with syntax errors.

This is code golf, so the solution with the fewest number of bytes wins.

share|improve this question
    
Can we define the language L? –  Greg Hewgill Jun 10 at 3:13
1  
@GregHewgill Yes. L is the language you choose to write your program in. –  Quincunx Jun 10 at 3:14
    
In some languages, this is surprisingly tough. –  Quincunx Jun 10 at 4:33
3  
With a turing complete subset of Python, this is a valid entry: x=input();print(x+'#'+x[::-1]). The subset is the set of all programs that don't include newlines. –  Quincunx Jun 10 at 4:58

16 Answers 16

Perl, 55 54 bytes

undef$/;$a=<>."\n__END__\n";print$a,scalar reverse$a;

Reads program source from stdin and writes to stdout.

Result of running on itself:

undef$/;$a=<>."\n__END__\n";print$a,scalar reverse$a;

__END__

__DNE__

;a$esrever ralacs,a$tnirp;"n\__DNE__n\".><=a$;/$fednu
share|improve this answer
    
+1 for not using comments –  professorfish Jun 10 at 15:33
3  
I like that it marks the apparent gibberish at the bottom with "DNE" - a common shorthand for "Do Not Erase" used to mark things on chalkboards/whiteboards so that people don't mistake them for unimportant scribblings and wipe them off. –  anaximander Jun 10 at 15:55
    
how does it work, I don't know perl, more specifically how does it quine(get the line that it's reversing)? –  Cruncher Jun 10 at 20:05
    
@Cruncher: __END__ indicates the end of the parseable portion of the script. See perldoc.perl.org/functions/__END__.html For input, <> reads the input from stdin. –  Greg Hewgill Jun 10 at 20:07
1  
1+ Works in most cases except when program ends with __DATA__ that is read.. eg. print while(<DATA>);\n__DATA__ will change behaviour. –  Sylwester Jun 11 at 9:51

Java, 225 bytes

class c{public static void main(String[]a){String s="";java.util.Scanner r=new java.util.Scanner(System.in);while(r.hasNext())s+=r.nextLine()+"\n";s=s.replace("\n","//\n");System.out.print(s+new StringBuilder(s).reverse());}}

Output on itself (when prettified beforehand):

class c {//
    public static void main(String[] a) {//
        String s = "";//
        java.util.Scanner r = new java.util.Scanner(System.in);//
        while (r.hasNext()) s += r.nextLine() + "\n";//
        s = s.replace("\n", "//\n");//
        System.out.print(s + new StringBuilder(s).reverse());//
    }//
}//

//}
//}
//;))(esrever.)s(redliuBgnirtS wen + s(tnirp.tuo.metsyS        
//;)"n\//" ,"n\"(ecalper.s = s        
//;"n\" + )(eniLtxen.r =+ s ))(txeNsah.r( elihw        
//;)ni.metsyS(rennacS.litu.avaj wen = r rennacS.litu.avaj        
//;"" = s gnirtS        
//{ )a ][gnirtS(niam diov citats cilbup    
//{ c ssalc
share|improve this answer
1  
Problem if comment ending in *. See comment –  edc65 Jun 17 at 22:08

Python 2, 68 bytes

import sys
x=''.join(l[:-1]+'#\n'for l in sys.stdin)
print x+x[::-1]

Doesn't work if run from IDLE, because you need to generate an EOF character to stop the program from waiting on input.

Output when run on itself:

import sys#
x=''.join(l[:-1]+'#\n'for l in sys.stdin)#
print(x+x[::-1])#

#)]1-::[x+x(tnirp
#)nidts.sys ni l rof'n\#'+]1-:[l(nioj.''=x
#sys tropmi

Thanks to Greg Hewgill for helping to hammer out problems and to golf.

share|improve this answer
    
Nice job, beats my kinda lame Python attempt. –  Greg Hewgill Jun 10 at 4:00
1  
@GregHewgill I prefer a nice upvote to a nice comment ;-) –  Quincunx Jun 10 at 4:06
1  
Ok okay... I don't usually vote against myself. :) –  Greg Hewgill Jun 10 at 4:07
4  
@GregHewgill I vote "against" myself a lot. I upvote answers based on their merits, not based on whether or not I answered. –  Quincunx Jun 10 at 4:08

GolfScript, 10 9 bytes

"
}"+.-1%

Quite similar to minitech's solution, but it works well with newlines. It relies on GolfScript's funny (and undocumented) behavior to ignore an unmatched (and uncommented) }, as well as everything that follows it.

It will fail if the input contains an unmatched {, but that would technically constitute a syntax error.

How it works

"
}"   # Push the string "\n}".
+    # Concatenate it with the input string.
.    # Duplicate the modified string.
-1%  # Reverse the copy.

Example

$ echo -n '1{"race{car"}
> {"foo\"bar"}
> if#' | golfscript make-palindrome.gs
1{"race{car"}
{"foo\"bar"}
if#
}}
#fi
}"rab"\oof"{
}"rac{ecar"{1
$ echo '1{"race{car"}
> {"foo\"bar"}
> if#
> }}
> #fi
> }"rab"\oof"{
> }"rac{ecar"{1' | golfscript
race{car
share|improve this answer
    
Try 1\n2# (\n would be an actual newline character) as your input. –  Quincunx Jun 10 at 5:57
1  
@Quincunx: Pesky comments... A newline before the curly bracket should fix that. –  sudo Jun 10 at 6:00
    
Before and after. Need to remain a palindrome. –  Quincunx Jun 10 at 6:06
    
@Quincunx: Of course. It should work now. –  sudo Jun 10 at 6:09

x86 machine code on DOS (.com file) - 70 bytes

Dealing with .COM files, creating a palyndrome is easy - since the COM "loader" just puts the content of the file at address 100h and jumps there, the program must already hardcode its end somehow and ignore everything after it, so we can just append the reverse of the first N-1 bytes (only caveat: if the program somehow tries to do tricks with the length of file everything breaks).

Here is the hex dump of my .COM-palyndromizing .COM:

00000000  31 db 8a 1e 80 00 c6 87  81 00 00 ba 82 00 b8 00  |1...............|
00000010  3d cd 21 72 30 89 c6 bf  ff ff b9 01 00 ba fe 00  |=.!r0...........|
00000020  89 f3 b4 3f cd 21 3c 01  75 18 b4 40 bb 01 00 cd  |...?.!<.u..@....|
00000030  21 85 ff 75 e5 89 f3 f7  d9 88 ee b8 01 42 cd 21  |!..u.........B.!|
00000040  eb d8 47 74 f0 c3                                 |..Gt..|

It takes the input file on the command line, and writes the output on stdout; the expected usage is something like compalyn source.com > out.com.

Commented assembly:

    org 100h

section .text

start:
    ; NUL-terminate the command line
    xor bx,bx
    mov bl, byte[80h]
    mov byte[81h+bx],0
    ; open the input file
    mov dx,82h
    mov ax,3d00h
    int 21h
    ; in case of error (missing file, etc.) quit
    jc end
    ; si: source file handle
    mov si,ax
    ; di: iteration flag
    ; -1 => straight pass, 0 reverse pass
    mov di,-1
loop:
    ; we read one byte at time at a bizarre memory
    ; location (so that dl is already at -2 later - we shave one byte)
    mov cx,1
    mov dx,0feh
    mov bx,si
    mov ah,3fh
    int 21h
    ; if we didn't read 1 byte it means we either got to EOF
    ; or sought before the start of file
    cmp al,1
    jne out
    ; write the byte on stdout
    mov ah,40h
    mov bx,1
    int 21h
    ; if we are at the first pass we go on normally
    test di,di
    jnz loop
back:
    ; otherwise, we have to seek back
    mov bx,si
    ; one byte shorter than mov cx,-1
    neg cx
    ; dl is already at -2, fix dh so cx:dx = -2
    mov dh,ch
    mov ax,4201h
    int 21h
    jmp loop
out:
    ; next iteration
    inc di
    ; if it's not zero we already did the reverse pass
    jz back
end:
    ret

Tested on itself and the solutions to a previous question seems to work fine in DosBox, some more extensive testing on "canonical" DOS executables will follow.

share|improve this answer

GolfScript, 8

.-1%'#'\

Doesn’t handle newlines, but nobody uses those in GolfScript.

share|improve this answer
6  
Using newline in string literals can be used quite often ;-) –  Howard Jun 10 at 5:10

Bash+coreutils, 39 bytes

f="`cat`
exit"
echo "$f"
tac<<<"$f"|rev

Reads from STDIN and outputs to STDOUT:

$ cat hello.sh 
#!/bin/bash

echo 'Hello, World!'

$ ./palin.sh < hello.sh 
#!/bin/bash

echo 'Hello, World!'
exit
tixe
'!dlroW ,olleH' ohce

hsab/nib/!#
$ 
share|improve this answer
    
Scripts with () characters are likely invalid in this way. –  user23013 Jun 17 at 19:45
    
@user23013 Seems to work fine. At least a simple test like ( echo 'Hello, World!' ). bash pretty much ignores everything after the exit. –  DigitalTrauma Jun 17 at 20:05
    
Oh yes... I think I have tried it with sh but not bash... Sorry. –  user23013 Jun 17 at 20:51

Javascript (ES6) Multi-line - 71

Kinda sorta stole Quincunx's comment method here:

alert((x=prompt().replace(/\n/g,'//\n')+'/')+[...x].reverse().join(''))

Single line - 49

alert((x=prompt()+'/')+[...x].reverse().join(''))
share|improve this answer

C++, 214 209 bytes

#include<cstdio>
#include<stack>
int main(){std::stack<char>s;int c;while((c=getc(stdin))>EOF){if(c=='\n')for(int i=2;i;i--)s.push(putchar('/'));s.push(putchar(c));}while(s.size()){putchar(s.top());s.pop();}}

Result of running on itself:

#include<cstdio>//
#include<stack>//
int main(){std::stack<char>s;int c;while((c=getc(stdin))>EOF){if(c=='\n')for(int i=2;i;i--)s.push(putchar('/'));s.push(putchar(c));}while(s.size()){putchar(s.top());s.pop();}}//

//}};)(pop.s;))(pot.s(rahctup{))(ezis.s(elihw};))c(rahctup(hsup.s;))'/'(rahctup(hsup.s)--i;i;2=i tni(rof)'n\'==c(fi{)FOE>))nidts(cteg=c((elihw;c tni;s>rahc<kcats::dts{)(niam tni
//>kcats<edulcni#
//>oidtsc<edulcni#
share|improve this answer
    
Fail when continuation char '\' is used. Try [ideone.com/TCZHr9] –  edc65 Jun 11 at 7:42
    
@edc65: Yeah, I thought about that later. The only obvious way I can think of to handle that would be to unfold the folded lines first. –  Greg Hewgill Jun 11 at 8:30
    
can be done at small cost - se my C answer –  edc65 Jun 11 at 8:53

Brainfuck, 749 without whitespace (not golfed)

This produces brainfuck programs which mirrored palindromes, i.e. they are are mirror images of themselves.

++++++++++
[->++++>+++++++++<<]>+++.>+..<.>++.
>>>>+[>,]<-[+<-]
>[
  [-<+<<+>>>]
  +<-------------------------------------------[-<+>>[-]<]>[-<<<.>>>]
  +<<-[->+>[-]<<]>>[-<<<.>>>]
  +<-[-<+>>[-]<]>[-<<<.>>>]
  +<<-[->+>[-]<<]>>[-<<<.>>>]
  +<--------------[-<+>>[-]<]>[-<<<.>>>]
  +<<--[->+>[-]<<]>>[-<<<.>>>]
  +<-----------------------------[-<+>>[-]<]>[-<<<.>>>]
  +<<--[->+>[-]<<]>>[-<<<.>>>]
  <[-]>>
]
<<<<[<]
<--.<.>++..--..<.>++.
>>[>]
<[
  [->+>>+<<<]
  +>-------------------------------------------[->+<<[-]>]<[->>>.<<<]
  +>>-[-<+<[-]>>]<<[->>>.<<<]
  +>-[->+<<[-]>]<[->>>.<<<]
  +>>-[-<+<[-]>>]<<[->>>.<<<]
  +>--------------[->+<<[-]>]<[->>>++.--<<<]
  +>>--[-<+<[-]>>]<<[->>>--.++<<<]
  +>-----------------------------[->+<<[-]>]<[->>>++.--<<<]
  +>>--[-<+<[-]>>]<<[->>>--.++<<<]
  >[-]<<
]
<--.<.>++..<.

Given a program it outputs

+[[+]PROGRAM[+]][[+]MIRROR[+]]+

with PROGRAM and MIRROR replaced by the program (without non-brainfuck characters) and its mirror image.

share|improve this answer

TI-BASIC, 82

:Input Str1:Str1:For(I,1,length(Ans)-1:Ans+sub(Ans,2I,1:End:Str1+":Stop:potS:"+Ans

I had to make it a bit longer than I wanted to make it work on itself. Here's the output when fed itself:

:Input Str1:Str1:For(I,1,length(Ans)-1:Ans+sub(Ans,2I,1:End:Str1+":Stop:potS:"+Ans:Stop:potS:snA+":Stop:potS:"+1rtS:dnE:1,I2,snA(bus+snA:1-)snA(htgnel,1,I(roF:1rtS:1rtS tupnI:
share|improve this answer
    
Are you sure tokens can be reversed letter-by-letter? –  hexafraction Jun 11 at 15:32
    
@hexafraction Most TI-83+ will work with it once it is compiled, but cannot compile it at runtime. –  Timtech Jun 11 at 20:26
    
What product are you using to compile it? I can't reproduce this behavior. –  hexafraction Jun 11 at 20:38
    
You have to compile it with the TI-tools first. It may keep just the tokens in opposite order (since they are all 1 byte). –  Timtech Jun 12 at 10:43

C# - 174

using System;using System.Linq;class c{public static void Main(){var a="";var b="";while((a=Console.ReadLine())!="")b+=a+"//\n";Console.Write(b+string.Concat(b.Reverse()));}}

Test Input:

using System; 
using System.Linq; 
class c 
{ 
    public static void Main() 
    { 
        var a = ""; 
        var b = ""; 
        while ((a = Console.ReadLine()) != "") 
            b += a + "//\n"; 
        Console.Write(b+string.Concat(b.Reverse())); 
    } 
} 

Test Output:

using System; 
using System.Linq; 
class c 
{ 
    public static void Main() 
    { 
        var a = ""; 
        var b = ""; 
        while ((a = Console.ReadLine()) != "") 
            b += a + "//\n"; 
        Console.Write(b+string.Concat(b.Reverse())); 
    } 
} 

// }
// }
// ;)))(esreveR.b(tacnoC.gnirts+b(etirW.elosnoC
// ;"n\//" + a =+ b
// )"" =! ))(eniLdaeR.elosnoC = a(( elihw
// ;"" = b rav
// ;"" = a rav
// {
// )(niaM diov citats cilbup
// {
// c ssalc
// ;qniL.metsyS gnisu
// ;metsyS gnisu
share|improve this answer
    
I think you may have misunderstood one of the instructions. Your program should be able to take any program as input, and write out a palindromic program that does the same thing as the original program. –  Greg Hewgill Jun 11 at 2:20
    
It can.. If I input the C++ code from your answer, it returns exactly what you have. –  malik Jun 11 at 2:24
    
All your program does is reverse its input. The output of your program is not a complete palindromic program. –  Greg Hewgill Jun 11 at 2:30
    
Oh yeah, i get ya. Updated - better now? –  malik Jun 11 at 2:36
2  
Yup, that's it. Your test output should have the // at the end of each line now though. –  Greg Hewgill Jun 11 at 2:42

C 168 175

Correctly handles escaped newline inside source code

Edit 1 fixed bug when last newline missing
Edit 2 fixed bug when line inside comment ends with *: add a tab char before the // comment
(and golfed more)

b[999999];main(c,z){char*p,for(p=b;(*p=c=getchar())>=0;z=c,p++)c-10||(z-92?*p++=9,*p++=47,*p++=47,*p=c:(p-=2));*p=47;for(p=b;*p;)putchar(*p++);for(;p>b;)putchar(*--p);}

C99 Standard, valid code, many warnings

Ungolfed

b[999999]; // working buffer ~ 4M on 32 bit machine, max source size
// c is current char, z is previous char,
main(c,z) // z  start as argv pointer, will be out of char range
{
  char *p;
  for(p = b; 
      (*p=c=getchar()) >= 0; // while testing EOF copy char to buffer set c variable
      z=c, p++) // at end loop increment p and set previous = current
  {
      c-'\n' || // if newline 
       (z - '\\' // check if escaped
          ? *p++='\t',*p++='/',*p++='/', *p=c // if not escaped, add tab,/,/ and newline
          : (p-=2) // if escaped, drop both escape and newline
       ); 
  }
  *p='/'; // if last newline missing, will add a comment anyway
  for(p=b;*p;) putchar(*p++); // ouput buffer 
  for(;--p>=b;) putchar(*p); // outbut buffer reversed
}
share|improve this answer
1  
there is a little bug in it. try /* *<NL> */int main(){} –  user23013 Jun 17 at 19:38
    
@user23013 fixed, thanks –  edc65 Jun 17 at 21:53

PHP, 96 bytes

function a($b){
    echo $c = "a('$b')" . strrev("a)'" . $b . "'(");
    $d = substr($c, 0, strlen($b) + 5);
    eval("$d;");
}

Sample Usage:

a('apple'); // echoes a('apple')('elppa')a until your bytes get exhausted

This is nothing clever. It's just a simple piece of code that does the job... I was in a mood for playing. I do know that this code is rife with bad programming practices!

Finally, I will gladly accept any criticism and edits to this code!

share|improve this answer
    
Welcome to Code Golf. This is a function, not a program. See the other answers, they provide good examples. –  A.L Jun 10 at 9:21
    
Noted. Thank you! –  Thauwa Jun 10 at 10:49

Cobra - 134

class P
    def main
        i=List<of String?>(Console.readLine.split('\n'))
        print '/#\n[i.reversed.join("\n")]\n#/#\n[i.join("\n")]\n#/'
share|improve this answer

Racket 133

(require srfi/13)(let((r read-line)(w display))(let l((i(r)))(when
(not(eq? eof i))(w i)(w";\n")(l(r))(w"\n;")(w(string-reverse i)))))

Ungolfed (but still very imperative):

(require srfi/13)
(let recurse ((instr (read-line)))
  (when (not (eof-object? instr))
    (display instr)
    (display ";\n")
    (recurse (read-line))
    (display "\n;")
    (display (string-reverse instr))))

Output when given the ungolfed version as input:

(require srfi/13);
(let recurse ((instr (read-line)));
  (when (not(eof-object? instr));
    (display instr);
    (display ";\n");
    (recurse (read-line));
    (display "\n;");
    (display (string-reverse instr))));

;))))rtsni esrever-gnirts( yalpsid(    
;)";n\" yalpsid(    
;))enil-daer( esrucer(    
;)"n\;" yalpsid(    
;)rtsni yalpsid(    
;))rtsni ?tcejbo-foe(ton( nehw(  
;)))enil-daer( rtsni(( esrucer tel(
;)31/ifrs eriuqer(
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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