Palindromize this string!

13

3

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.

Oliver Ni

Posted 2016-11-02T23:06:22.200

Reputation: 9 650

Question was closed 2016-11-03T13:46:40.157

1There should be a hefty bonus for any code that does not use comments to fulfill the palindrome requirement. – Theo – 2016-11-03T02:14:45.217

@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. – miles – 2016-11-03T02:25:40.943

@miles Fair enough. Although I would love to see some creativity from the non-golfing-langs in general. – Theo – 2016-11-03T02:28:14.680

Related. – xnor – 2016-11-03T07:53:21.673

@miles It´s virtually impossible for non-eso-langs to do this without a comment. You can´t just palindromize predefined function names. Maybe it can be done, but it would ridiculously blow up the size. – Titus – 2016-11-03T09:56:35.150

Can there be an exception for the program to substitute exact characters for mirrored characters in its palindrome? I.e. Allowing "(1+1)" as a palindrome. I think it would increase the competitiveness of non-golf languages. – None – 2016-11-03T13:22:58.230

8Actually, it´s not an exact duplicate. You have to remove one character from the input here. – Titus – 2016-11-03T14:23:58.790

Answers

16

Luis Mendo

Posted 2016-11-02T23:06:22.200

Reputation: 87 464

10Yep. One char is a palindrome. – JungHwan Min – 2016-11-02T23:11:08.630

3how do you have a built in for this – undergroundmonorail – 2016-11-03T07:59:06.360

2@undergroundmonorail: It is quite often useful in ascii art challenges, or really any challenge with horizontal symmetry. – Emigna – 2016-11-03T08:32:15.883

2@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 – undergroundmonorail – 2016-11-03T21:43:53.363

16

Python 3, 41 bytes

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

Try it here.

Luis Mendo

Posted 2016-11-02T23:06:22.200

Reputation: 87 464

1My first Python answer – Luis Mendo – 2016-11-02T23:42:08.187

1Not that it matters, but one of your reversed brackets is the wrong direction. – xnor – 2016-11-03T00:19:16.587

1@xnor Thank you! Corrected – Luis Mendo – 2016-11-03T00:22:37.330

1I think he means the other one – WorldSEnder – 2016-11-03T13:29:28.210

15

Dip, 1 byte

B

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

acrolith

Posted 2016-11-02T23:06:22.200

Reputation: 3 728

14

Pyth - 13 bytes

It kinda looks like its crying.

+Qt_Q " Q_tQ+

Try it online here.

Maltysen

Posted 2016-11-02T23:06:22.200

Reputation: 25 023

+1 for almost being Q_Q – Downgoat – 2016-11-02T23:22:55.473

6Hate to see a Qt crying – Aaron – 2016-11-03T09:58:28.307

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 (http://pyth.herokuapp.com/?code=%2Bzt_z+%22+z_tz%2B&input=abcde&debug=0)

– Artyer – 2016-11-03T18:38:19.413

@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. – Maltysen – 2016-11-03T18:57:05.443

11

아희(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)

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

JungHwan Min

Posted 2016-11-02T23:06:22.200

Reputation: 13 290

4

APL -- 13 bytes.

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

Explanation:

⊣,¯1↓⌽
     ⌽ Reverse the argument.
  ¯1↓  Drop the last character.
⊣,     Concatenate the original to the result.

Zacharý

Posted 2016-11-02T23:06:22.200

Reputation: 5 710

3

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

Downgoat

Posted 2016-11-02T23:06:22.200

Reputation: 27 116

aww you ninja'd me – dkudriavtsev – 2016-11-02T23:29:43.137

1You can use join\`` instead of join("") – Huntro – 2016-11-03T00:55:50.237

3

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

miles

Posted 2016-11-02T23:06:22.200

Reputation: 15 654

3

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")

hstde

Posted 2016-11-02T23:06:22.200

Reputation: 159

2

Ruby, 47 bytes

->s{s+s.reverse[1..-1]}#}[1-..1]esrever.s+s{s>-

dkudriavtsev

Posted 2016-11-02T23:06:22.200

Reputation: 5 781

2

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!

Greg Martin

Posted 2016-11-02T23:06:22.200

Reputation: 13 940

2

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.

Fatalize

Posted 2016-11-02T23:06:22.200

Reputation: 32 976

2

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

Emigna

Posted 2016-11-02T23:06:22.200

Reputation: 50 798

2

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)

Blue

Posted 2016-11-02T23:06:22.200

Reputation: 26 661

1

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 :)

Titus

Posted 2016-11-02T23:06:22.200

Reputation: 13 814

your 3rd character is an =, but does not come back in the end – nl-x – 2016-11-03T09:49:23.070

@nl-x Typo ... the bytecount was correct ... apart from the missing $ for argv. Thanks. – Titus – 2016-11-03T09:51:15.060

1

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"

milk

Posted 2016-11-02T23:06:22.200

Reputation: 3 043

1

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

Kevin Cruijssen

Posted 2016-11-02T23:06:22.200

Reputation: 67 575

1

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

Numberknot

Posted 2016-11-02T23:06:22.200

Reputation: 885

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. ;) – Kevin Cruijssen – 2016-11-03T08:37:32.250

@KevinCruijssen you won.Actually i am weak with handling strings.I need to open my java book. – Numberknot – 2016-11-03T08:44:11.030

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];} – cliffroot – 2016-11-03T10:17:12.800

1

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.

Laikoni

Posted 2016-11-02T23:06:22.200

Reputation: 23 676

1init s++reverse s is shorter – Damien – 2016-11-03T10:44:29.177

1

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%`

rturnbull

Posted 2016-11-02T23:06:22.200

Reputation: 3 689

0

Perl 5, 44 bytes

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

s#(.*).#$&.reverse$1#e#1$esrever.&$#.)*.(#s

msh210

Posted 2016-11-02T23:06:22.200

Reputation: 3 094

0

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);
}

Karl Napf

Posted 2016-11-02T23:06:22.200

Reputation: 4 131

0

, 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')}ï+ï`

Mama Fun Roll

Posted 2016-11-02T23:06:22.200

Reputation: 7 234