13
\$\begingroup\$

Your task is to palindromize a string as follows:

Take the string.

abcde

Reverse it.

edcba

Remove the first letter.

dcba

Glue it onto the original string.

abcdedcba

But here's the catch: since this challenge is about palindromes, your code itself also has to be a palindrome.

Remember, this is , so the code with the smallest number of bytes wins.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ There should be a hefty bonus for any code that does not use comments to fulfill the palindrome requirement. \$\endgroup\$
    – Theo
    Commented Nov 3, 2016 at 2:14
  • \$\begingroup\$ @Theo Then it would go straight to the golf-langs with the palindrome builtin. I don't really see the need for it, and doing it without a comment is more of a personal challenge. \$\endgroup\$
    – miles
    Commented Nov 3, 2016 at 2:25
  • \$\begingroup\$ @miles Fair enough. Although I would love to see some creativity from the non-golfing-langs in general. \$\endgroup\$
    – Theo
    Commented Nov 3, 2016 at 2:28
  • \$\begingroup\$ Related. \$\endgroup\$
    – xnor
    Commented Nov 3, 2016 at 7:53
  • 8
    \$\begingroup\$ Actually, it´s not an exact duplicate. You have to remove one character from the input here. \$\endgroup\$
    – Titus
    Commented Nov 3, 2016 at 14:23

23 Answers 23

17
\$\begingroup\$

Python 3, 41 bytes

lambda t:t+t[-2::-1]#]1-::2-[t+t:t adbmal

Try it here.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ My first Python answer \$\endgroup\$
    – Luis Mendo
    Commented Nov 2, 2016 at 23:42
  • 1
    \$\begingroup\$ Not that it matters, but one of your reversed brackets is the wrong direction. \$\endgroup\$
    – xnor
    Commented Nov 3, 2016 at 0:19
  • 1
    \$\begingroup\$ @xnor Thank you! Corrected \$\endgroup\$
    – Luis Mendo
    Commented Nov 3, 2016 at 0:22
  • 1
    \$\begingroup\$ I think he means the other one \$\endgroup\$ Commented Nov 3, 2016 at 13:29
16
\$\begingroup\$

05AB1E, 1 byte

û

Try it online!

\$\endgroup\$
4
  • 10
    \$\begingroup\$ Yep. One char is a palindrome. \$\endgroup\$ Commented Nov 2, 2016 at 23:11
  • 3
    \$\begingroup\$ how do you have a built in for this \$\endgroup\$ Commented Nov 3, 2016 at 7:59
  • 2
    \$\begingroup\$ @undergroundmonorail: It is quite often useful in ascii art challenges, or really any challenge with horizontal symmetry. \$\endgroup\$
    – Emigna
    Commented Nov 3, 2016 at 8:32
  • 2
    \$\begingroup\$ @Emigna I guess that's fair. I thought "I wonder what function is being horribly abused because it happens to generate this output" but no, I checked the docs and sure enough it's called "palindromize" haha \$\endgroup\$ Commented Nov 3, 2016 at 21:43
15
\$\begingroup\$

Dip, 1 byte

B

Body must be at least 30 characters; you entered 13.

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

Pyth - 13 bytes

It kinda looks like its crying.

+Qt_Q " Q_tQ+

Try it online here.

\$\endgroup\$
4
  • \$\begingroup\$ +1 for almost being Q_Q \$\endgroup\$
    – Downgoat
    Commented Nov 2, 2016 at 23:22
  • 6
    \$\begingroup\$ Hate to see a Qt crying \$\endgroup\$
    – Aaron
    Commented Nov 3, 2016 at 9:58
  • \$\begingroup\$ You can use z instead of Q to not need quotes in the input (Which I think you need to add 2 bytes for the required chars in input (pyth.herokuapp.com/…) \$\endgroup\$
    – Artyer
    Commented Nov 3, 2016 at 18:38
  • \$\begingroup\$ @Artyer actually, I don't need to add 2 bytes for the " because it is such a common and standard input format. I could have used z, but I was expecting to use the implicit input feature of Q, but ended up not using it, and didn't go back and change it. \$\endgroup\$
    – Maltysen
    Commented Nov 3, 2016 at 18:57
11
\$\begingroup\$

아희(Aheui), 149 137 bytes (47 chars + 2 newlines)

붛뱐쎤붇쎡뻐처순의이멓희
빠본땨벌다따토먀의썩속썩의먀토따다벌땨본빠
희멓이의순처뻐쎡붇쎤뱐붛

Due to the nature of the Aheui language, the input must end in a double quotation mark (") (gets ignored).

Try it here! (copy and paste the code)

Bonus (each line is palindromic), 131 bytes (42 chars + 5 newlines)

밯빪반분반빪밯
쏜발뚝볃뚝발쏜
쏙툼닿뗘닿툼쏙
뽓첡순괆순첡뽓
숙쌱멈긕멈쌱숙
몋희익욥익희몋
\$\endgroup\$
4
\$\begingroup\$

APL -- 13 bytes.

⊣,¯1↓⌽⍝⌽↓1¯,⊣

Explanation:

⊣,¯1↓⌽
     ⌽ Reverse the argument.
  ¯1↓  Drop the last character.
⊣,     Concatenate the original to the result.
\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 86 bytes

s=>s+[...s].reverse().slice(1).join("")//)""(nioj.)1(ecils.)(esrever.]s...[+s>=s

I tried a smarter solution but it's still longer :/ even abusing [,...a]=s didn't seem to save bytes

\$\endgroup\$
2
  • \$\begingroup\$ aww you ninja'd me \$\endgroup\$
    – anna328p
    Commented Nov 2, 2016 at 23:29
  • 1
    \$\begingroup\$ You can use join`` instead of join("") \$\endgroup\$
    – Huntro
    Commented Nov 3, 2016 at 0:55
3
\$\begingroup\$

J, 17 bytes

}:,|.NB. .BN.|,:}

Takes the easy way out by using a comment to mirror the code. In J, NB. starts a line comment. I do hope to find a method that doesn't involve a comment but the digrams in J probably do make it harder.

Also forms two smileys, }: and :}.

Usage

   f =: }:,|.NB. .BN.|,:}
   f 'abcde'
abcdedcba

Explanation

Comment part removed since it is just filler.

}:,|.  Input: string S
   |.  Reverse S
}:     Curtail, get S with its last char removed
  ,    Join them and return
\$\endgroup\$
3
\$\begingroup\$

C#, (51 50 + 1)*2 = 104 102 bytes

saved 2 bytes to the use of .Aggregate()

s=>s+s.Aggregate("",(a,b)=>b+a,s=>s.Substring(1));//;))1(gnirtsbuS.s>=s,a+b>=)b,a(,""(etagerggA.s+s>=s

You can capture this lambda with

Func<string,string> f = <lambda here>

and call it like this

f("abcde")
\$\endgroup\$
2
\$\begingroup\$

Ruby, 47 bytes

->s{s+s.reverse[1..-1]}#}[1-..1]esrever.s+s{s>-
\$\endgroup\$
2
\$\begingroup\$

Mathematica, 67 bytes

f=#~Join~Rest@Reverse@#//Function;noitcnuF//#@esreveR@tseR~nioJ~#=f

Defines a named function f that takes a list of characters as input and returns the appropriate palindromized list as output. The bit after the semicolon (is fortunately syntactically valid and) gives a second, way more complicated name to this same function.

It was fun trying to make Mathematica make sense without any brackets!

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

Brachylog, 11 bytes

:Lc.r
r.cL:

Try it online!

Explanation

:Lc.         Input concatenated with a string L results in the Output
   .r(.)     The Output reversed is still the Output

The second line is a new predicate declaration that never gets called in that code.

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

Actually, 14 13 bytes

RiXßRkΣkRßXiR

Try it online!

Explanation

R          # reverse input
 i         # flatten
  X        # discard top of stack
   ßR      # push reversed input
     kΣ    # concatenate each
k          # wrap in list
 R         # reverse
  ß        # push input
   X       # discard it
    i      # flatten list to string
     R     # reverse string
\$\endgroup\$
2
\$\begingroup\$

Pyke, 1 bytes

s

Try it here!

Yes, this is the same builtin as the digital root one. This time it takes a sting and turns it into a palindrome. Added with this commit (actually on the same day as the digital root question)

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

Java 7, 146 bytes

(Enter is added for readibility and not part of the code.)

String c(String s){return s+new StringBuffer(s).reverse().substring(1);}/
/};)1(gnirtsbus.)(esrever.)s(reffuBgnirtS wen+s nruter{)s gnirtS(c gnirtS
\$\endgroup\$
1
\$\begingroup\$

Java 7, 152 bytes

String c(char[]s,int i){return s.length==i+1?s[i]+"":s[i]+c(s,++i)+s[i-1];}//};]1-i[s+)i++,s(c+]i[s:""+]i[s?1+i==htgnel.s nruter{)i tni,s][rahc(c gnirtS
\$\endgroup\$
3
  • \$\begingroup\$ Beat ya to it with a shorter answer few seconds before you. I like the manual recursive approach instead of new StringBuffer(s).reverse() though. Unfortunately I don't see anything to make it shorter this time. ;) \$\endgroup\$ Commented Nov 3, 2016 at 8:37
  • \$\begingroup\$ @KevinCruijssen you won.Actually i am weak with handling strings.I need to open my java book. \$\endgroup\$
    – Numberknot
    Commented Nov 3, 2016 at 8:44
  • \$\begingroup\$ Seems like this makes it even String c(char[]s,int i){return s.length<i+2?s[i]+"":s[i]+c(s,i+1)+s[i];} \$\endgroup\$
    – cliffroot
    Commented Nov 3, 2016 at 10:17
1
\$\begingroup\$

PHP, 38+39=77 bytes

<?=($s=$argv[1]).substr(strrev($s),1);#;)1,)s$(verrts(rtsbus.)]1[vgra$=s$(=?<

stupid restriction ... for non-eso languages :)

\$\endgroup\$
2
  • \$\begingroup\$ your 3rd character is an =, but does not come back in the end \$\endgroup\$
    – nl-x
    Commented Nov 3, 2016 at 9:49
  • \$\begingroup\$ @nl-x Typo ... the bytecount was correct ... apart from the missing $ for argv. Thanks. \$\endgroup\$
    – Titus
    Commented Nov 3, 2016 at 9:51
1
\$\begingroup\$

Haskell, 46 44 bytes

p s=init s++reverse s--s esrever++s tini=s p

Try it on Ideone. Saved 2 bytes thanks to Damien.

Straight forward solution. init takes everything but the last character of a string (or last element of a list). -- enables a single line comment.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ init s++reverse s is shorter \$\endgroup\$
    – Damien
    Commented Nov 3, 2016 at 10:44
1
\$\begingroup\$

R, 107 bytes

This is the boring solution which just uses comments to make it a palindrome.

cat(x<-scan(,""),rev(strsplit(x,"")[[1]])[-1],sep="")#)""=pes,]1-[)]]1[[)"",x(tilpsrts(ver,)"",(nacs-<x(tac

R with few comments, 271 bytes

While this code is longer, only 89 of the bytes (33%) are comments, rather than the 50% in the above code. R relies extensively on parentheses for function application, so this was rather difficult.

`%q%`=function
(x,y)cat(x,y,sep=e)#
`%p%`=function
(x,y)`if`(length(y),rev(y)[-1],x)#
x=strsplit(scan(,e<-""),e)[[1]]#
n=NULL
e%q%x%p%n
n%p%x%q%e
LLUN=n
#]]1[[)e,)""-<e,(nacs(tilpsrts=x
#)x,]1-[)y(ver,)y(htgnel(`fi`)y,x(
noitcnuf=`%p%`
#)e=pes,y,x(tac)y,x(
noitcnuf=`%q%`
\$\endgroup\$
1
\$\begingroup\$

WinDbg, 142 287 bytes

db$t0 L1;.for(r$t1=@$t0;@$p;r$t1=@$t1+1){db$t1 L1};r$t3=@$t1-1;.for(r$t1=@$t1-3;@$t1>=@$t0;r$t1=@$t1-1;r$t3=@$t3+1){m$t1 L1 $t3};eb$t3 0;da$t0;*;0t$ad;0 3t$be;}3t$ 1L 1t$m{)1+3t$@=3t$r;1-1t$@=1t$r;0t$@=>1t$@;3-1t$@=1t$r(rof.;1-1t$@=3t$r;}1L 1t$bd{)1+1t$@=1t$r;p$@;0t$@=1t$r(rof.;1L 0t$bd

+145 bytes to make it a palindrome, nearly missed that requirement...

Input is passed in via an address in psuedo-register $t0. For example:

eza 2000000 "abcde"           * Write string "abcde" into memory at 0x02000000
r $t0 = 33554432              * Set $t0 = 0x02000000
* Edit: Something got messed up in my WinDB session, of course r $t0 = 2000000 should work
* not that crazy 33554432.

This may be more golfable, for example I feel like there should be an easier way to convert a memory address in a register to the value at that address.

It works by concatenating the chars from the second-to-last to the first to the end of the string.

db $t0 L1;                                      * Set $p = memory-at($t0)
.for (r $t1 = @$t0; @$p; r $t1 = @$t1 + 1)      * Set $t1 = $t0 and increment until $p == 0
{
    db $t1 L1                                   * Set $p = memory-at($t1)
};
r $t3 = @$t1 - 1;                               * Point $t3 at end of string

* From the second-to-last char, reverse through the string with $t1 back to the start ($t0)
* and continue to increment $t3 as chars are appended to the string.
.for (r $t1 = @$t1 - 3; @$t1 >= @$t0; r $t1 = @$t1 - 1; r $t3 = @$t3 + 1)
{
    m $t1 L1 $t3                                * Copy char at $t1 to end of string ($t3)
};
eb $t3 0;                                       * Null terminate the new string
da $t0;                                         * Print the palindrome string

* Comment of the previous code in reverse, making the whole thing a palindrome
*;0t$ad;0 3t$be;}3t$ 1L 1t$m{)1+3t$@=3t$r;1-1t$@=1t$r;0t$@=>1t$@;3-1t$@=1t$r(rof.;1-1t$@=3t$r;}1L 1t$bd{)1+1t$@=1t$r;p$@;0t$@=1t$r(rof.;1L 0t$bd

Output:

0:000> eza 2000000 "abcde"
0:000> r $t0 = 33554432
0:000> db$t0 L1;.for(r$t1=@$t0;@$p;r$t1=@$t1+1){db$t1 L1};r$t3=@$t1-1;.for(r$t1=@$t1-3;@$t1>=@$t0;r$t1=@$t1-1;r$t3=@$t3+1){m$t1 L1 $t3};eb$t3 0;da$t0;*;0t$ad;0 3t$be;}3t$ 1L 1t$m{)1+3t$@=3t$r;1-1t$@=1t$r;0t$@=>1t$@;3-1t$@=1t$r(rof.;1-1t$@=3t$r;}1L 1t$bd{)1+1t$@=1t$r;p$@;0t$@=1t$r(rof.;1L 0t$bd
02000000  61                                               a
02000000  61                                               a
02000001  62                                               b
02000002  63                                               c
02000003  64                                               d
02000004  65                                               e
02000005  00                                               .
02000000  "abcdedcba"
\$\endgroup\$
0
\$\begingroup\$

C, 162 bytes

f(char*c,char*d){char*e=c;while(*d++=*c++);c-=2;d-=2;do{*d++=*c--;}while(e<=c);}//};)c=<e(elihw};--c*=++d*{od;2=-d;2=-c;)++c*=++d*(elihw;c=e*rahc{)d*rahc,c*rahc(f

Ungolfed, unpalindromized:

f(char* c,char* d){
 char*e=c;
 while(*d++=*c++);
 c-=2;d-=2;
 do{*d++=*c--;}while(e<=c);
}

c is is input string, assumes output string d has sufficient length.

Usage:

int main() {
 char a[] = "abcde";
 char* b=malloc(strlen(a)*2);
 f(a,b);
 printf("%s\n",b);
}
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 44 bytes

43 bytes, plus 1 for -pe instead of -e

s#(.*).#$&.reverse$1#e#1$esrever.&$#.)*.(#s
\$\endgroup\$
0
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 16 chars / 20 bytes

ï+ïĦ⬮Đ1//1Đ⬮Ħï+ï

Try it here (ES6 browsers only).

Generated from the interpreter console using this:

c.value=`ï+ï${alias(String.prototype,'reverse')}⬮${alias(String.prototype,'slice')}1//1${alias(String.prototype,'slice')}⬮${alias(String.prototype,'reverse')}ï+ï`
\$\endgroup\$

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