Depalindromize this string!

Given a palindrome generated according to this challenge, depalindromize it.

Test cases

abcdedcba -> abcde
johncenanecnhoj -> johncena
ppapapp -> ppap
codegolflogedoc -> codegolf


Remember, this is , so the code with the fewest bytes wins.

• -1 for the pointless restriction on your code not being a palindrome. It adds nothing to the challenge IMO, in very few languages would it matter. – Rɪᴋᴇʀ Nov 2 '16 at 23:56
• +1 for the restriction. It´s so mirroring the paliondrome challenge ... and it´s adding challenge to esolangs. I like it. Am I correct in the assumption that input will always have an uneven length? – Titus Nov 3 '16 at 0:04
• The non-palindrome restriction is probably a joke based on the previous challenge. Did anyone really downvote based on that? – Luis Mendo Nov 3 '16 at 0:27
• It does prevent single-byte solutions. @diynevala +1 for the unnecessary +1. – Adám Nov 3 '16 at 10:29
• What if the string is not a palindrome to begin with? – Xavon_Wrentaile Nov 3 '16 at 23:45

Julia, 21 15 bytes

x->x[1:end/2+1]


Try it online! (extra code is for printing output)

• end/2 is cool feature – Downgoat Nov 3 '16 at 3:07
• @Downgoat yes, dennis showed it to me. – Rɪᴋᴇʀ Nov 3 '16 at 3:16

05AB1E, 3 bytes

2ä¬


Uses the CP-1252 encoding. Try it online!

Python 2, 23 bytes

I am not able to test on my phone, but this should work:

lambda s:s[:-~len(s)/2]

• If you are running on android, you can use QPython from the google play store. It's the best I've found :) – Yytsi Nov 3 '16 at 8:29
• termux apt-get install python2 – Matt Nov 3 '16 at 12:43
• @Matt That's overkill if all you want is Python. – mbomb007 Nov 3 '16 at 13:56
• @Matt as well as that if you can find apt-get on your phone, it's probably not a normal phone. – Lawful Lazy Nov 6 '16 at 0:04
• @MathManiac termux is installed from Google Play onto any non rooted Android phone. Can't get much more normal than that. – Matt Nov 9 '16 at 13:45

Fuzzy Octo Guacamole, 4 bytes

2.^/


I spent a while searching for a language in which this challenge is short, and realized I was dumb and my own language did that.

05AB1E, 5 bytes

Dg;î£


Try it online!

Explanation:

D      Duplicate
g;î   Divide length by two and round up
£  First b letters of a


@.head($0.len/2+1)  So simple I don't think needs explanation but I'll add one if wanted. Try it online Pyth - 4 bytes hc2Q  JavaScript (ES6), 32 26 25 bytes 1 byte saved thanks to Neil: s=>s.slice(0,-s.length/2)  f= s=>s.slice(0,-s.length/2) ; console.log(f('abcdedcba')) console.log(f('johncenanecnhoj')) console.log(f('ppapapp')) console.log(f('codegolflogedoc')) Previous solutions 26 bytes thanks to Downgoat: s=>s.slice(0,s.length/2+1)  32 bytes: s=>s.slice(0,(l=s.length/2)+l%2)  • You can shorten to just s=>s.slice(0,s.length/2+1) Since length will always be odd – Downgoat Nov 2 '16 at 23:56 • @Downgoat thanks to you I found that for one more byte s=>s.slice(0,s.length/2+.5) would work for even length too. – Hedi Nov 3 '16 at 0:19 • -s.length/2 works for both odd and even lengths. – Neil Nov 3 '16 at 10:17 WinDbg, 87 71 bytes db$t0 L1;.for(r$t1=@$t0;@$p;r$t1=@$t1+1){db$t1 L1};da$t0 L(@$t1-@$t0)/2  -16 bytes by not inserting NULL, instead passing length to da Input is passed in via an address in psuedo-register $t0. For example:

eza 2000000 "abcdedcba"       * Write string "abcdedcba" 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.  It works by replacing the right of middle char (or right-middle if the string has even length) with a null and then prints the string from the original starting memory address. 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) }; da$t0 L(@$t1-@$t0)/2                        * Print half the string


Output:

0:000> eza 2000000 "abcdeedcba"
0:000> r $t0 = 33554432 0:000> db$t0 L1;.for(r$t1=@$t0;@$p;r$t1=@$t1+1){db$t1 L1};da$t0 L(@$t1-@$t0)/2 02000000 61 a 02000000 61 a 02000001 62 b 02000002 63 c 02000003 64 d 02000004 65 e 02000005 65 e 02000006 64 d 02000007 63 c 02000008 62 b 02000009 61 a 0200000a 00 . 02000000 "abcde"  Haskell, 27 bytes take=<<succ.(div2).length  Pointfree version of \x->take(div(length x)2+1)x  which is also 27 bytes. MATL, 7 6 bytes 9LQ2/)  Try it online! Explanation 9L % Push array [1, 1j] Q % Add 1: transforms into [2, 1+1j] 2/ % Divide by 2: transforms into [1, 0.5+0.5j] ) % Apply as index into implicit input. The array [1, 0.5+0.5j] used as an index % is interpreted as [1:0.5+end*0.5]  • Wow, that is a very neat way to handle complex values as arguments for slicing – miles Nov 3 '16 at 0:55 • @miles Thanks! Yes, it's handy. The imaginary unit works as end, and colons between the array elements are implicit – Luis Mendo Nov 3 '16 at 1:01 Jelly, 4 bytes œs2Ḣ  Try it online! Explanation œs2 Split input into 2 chunks of similar lengths. For odd-length input, the first chunk is the longest Ḣ Keep the first chunk  V, 12 bytes Two completely different solutions, both 12 bytes. ò"Bx$xh|ò"bP


Try it online!

Ó./&ò
MjdGÍî


Try it online!

Brachylog, 4 bytes

@2tr


Try it online!

Explanation

@2        Split in half
t       Take the second half
r      Reverse it


If the input has odd length, the second half generated by @2 is the one that is the longest, that is the one we should return (after reversing it).

Dyalog APL, 9 bytes

⊢↑⍨2÷⍨1+≢


⊢ the argument

↑⍨ truncated at

2÷⍨ half of

1+ one plus

≢ the length

TryAPL online!

Perl, 15 bytes

Includes +2 for -lp

Give input string on STDIN:

depal.pl <<< "HelleH"


depal.pl:

#!/usr/bin/perl -lp
s/../chop/reg


The -l is not really needed if you input the palindrome without final newline, but I included it to be fair to the other perl solutions that use it.

Java 7, 57 bytes

String c(String s){return s.substring(0,s.length()/2+1);}

• You're missing a closing } (so it's 57 bytes). – Kevin Cruijssen Nov 3 '16 at 9:09
• @KevinCruijssen fixed. – Numberknot Nov 3 '16 at 9:24

TI-Basic, 14 bytes

Standard function. Returns string from index 1 to index (length/2 + 1/2).

sub(Ans,1,.5+.5length(Ans


GameMaker Language, 59 bytes

a=argument0 return string_copy(a,1,ceil(string_length(a)/2)


PHP, 40 bytes

<?=substr($a=$argv[1],0,1+strlen($a)/2);  strlen($a)/2 gets cast to int, with the input always having odd length, +1 suffices to round up.

42 bytes for any length:

<?=substr($a=$argv[1],0,(1+strlen($a))/2);  for unknown length, (1+strlen)/2 gets cast to int, rounding up strlen/2. Dip, 8 bytes H{C'0ÏEI  Explanation:  # Implicit input H # Push length of input { # Add 1 C # Divide by 2 ' # Convert to int 0Ï # Get string back E # Push prefixes of string I # Push prefixes[a] # Implicit print  This could probably be much improved. Perl, 23 + 2 (-pl flag) = 28 25 bytes perl -ple '$_=substr$_,0,1+y///c/2'  Ungolfed: while (<>) { # -p flag chomp($_)            # -l flag
$_ = substr($_, 0, 1 + length($_) / 2); print($_, "\n")      # -pl flag
}


Thanx to @ardnew.

• you can save 3 chars by replacing length() with y|||c – ardnew Nov 3 '16 at 20:33

Befunge, 24 22 bytes

~:0!#v_\1+
0:-2,\_@#


Try it online!

Befunge has no string or array type so the everything is done on the stack one character at a time. The first loop (on the top line) counts the number of characters read (swapping with less than 2 elements in the stack produces an initial 0). The second (on the middle line) prints characters while counting down twice as fast. As a result only the last half of the input is printed, but LIFO so it's in the correct order.

Thanks to Brian Gradin for a better version of the first loop.

• You beat me by half an hour and 7 bytes :) befunge.tryitonline.net/… – Brian Gradin Nov 3 '16 at 22:18
• @BrianGradin, nice. now I've beat beat you by 9 bytes ;) – Linus Nov 3 '16 at 22:42
• Ah, ok. I see what you did. Didn't occur to me to count down by two rather than calculate the actual number of characters to print. Nicely done. – Brian Gradin Nov 3 '16 at 23:07

Perl, 14 + 3 (-lF flag) = 19 17 bytes

For 5.20.0+:

perl -lF -E 'say@F[0..@F/2]'


For 5.10.0+ (19 bytes):

perl -nlaF -E 'say@F[0..@F/2]'


Ungolfed:

while (<>) {             # -n flag (implicitly sets by -F in 5.20.0+)
chomp($_) # -l flag @F = split('',$_);  # -aF flag (implicitly sets by -F in 5.20.0+)
say(@F[0 .. (scalar(@F) / 2)]);
}


Thanx to @simbabque.

• You can save two bytes, you don't need to set -n and -a because -F does so implicitly. – simbabque Nov 4 '16 at 12:05
• @simbabque Yes. But for 5.20.0+ only. – Denis Ibaev Nov 4 '16 at 18:24

Brainfuck, 20 bytes

,
[
[>,]
<[<]
>.,>[>]
<<
]


Try it online.

This saves a byte over the more straightforward approach of consuming the input before starting the main loop:

,[>,]
<
[
[<]
>.,>[>]
<,<
]


Pyth, 8 7 bytes

<zh/lz2


Saved 1 with thanks to @Steven H

Not the shortest Pyth answer (by half) but I'm making an effort to learn the language and this is my first post using it. Posted as much for comments and feedback as anything. It's also the first Pyth program that I have actually got to work :)

Now I just need to work out how the 4 byte answer from @Maltysen works :-)

• If you still want to know how Maltysen's answer works, it chops the input Q into 2 pieces and takes the first piece using h (which, thanks to the implementation of chop, will grab the center letter as well). As for your code, you could replace +1 with h, the built-in for incrementing numbers. – Steven H. Nov 7 '16 at 10:08
• Thanks for the explanation and for the h hint @Steven H. There are so many built-ins I guess it just takes some time to find them all :) – ElPedro Nov 7 '16 at 10:16
• No problem! If you ever need any help, try pinging me in the Nineteenth byte. – Steven H. Nov 7 '16 at 10:19

Actually, 5 bytes

l½KßH


Try it online!

-1 byte thanks to Sherlock9

Explanation:

l½K@H
l½K    ceil(len(input)/2)
ßH  first (len(input)//2 + 1) characters of input

• 6 bytes: ;l½K@H :D – Sherlock9 Nov 8 '16 at 18:59

C, 31 30 bytes

Saving 1 byte thanks to Cyoce.

f(char*c){c[-~strlen(c)/2]=0;}


Usage:

main(){
char a[]="hellolleh";
f(a);
printf("%s\n",a);
}

• @KevinCruijssen fixed – Karl Napf Nov 3 '16 at 8:59
• Hi, sorry I deleted my comment. I was correct in saying it won't work for even palindromes. But, since this is the reverse of that other challenge, there won't be any test cases for even palindromes.. Sorry about that, you can undo your change. +1 from me. :) – Kevin Cruijssen Nov 3 '16 at 9:00
• Well, it has the same length now, works for even+odd and looks golfier. I am okay with this. – Karl Napf Nov 3 '16 at 9:02
• This is arguably a memory leak :-) – ShreevatsaR Nov 4 '16 at 1:47
• I think you can remove the space in char* c – Cyoce Dec 13 '16 at 16:25

Python 2, 23 bytes

lambda x:x[:len(x)/2+1]

• I think this requires Python 2; you should indicate that in your answer – Luis Mendo Nov 3 '16 at 0:09
• @LuisMendo oh, thanks! – Rɪᴋᴇʀ Nov 3 '16 at 0:10

MATLAB / Octave, 201918 16 bytes

1 byte off borrowing an idea from Easterly Irk's answer (add 1 instead of .5)
2 bytes off thanks to @StewieGriffin (unnecessary parentheses)

@(x)x(1:end/2+1)

• @StewieGriffin Thanks! I don't know what I was thinking... – Luis Mendo Nov 3 '16 at 9:08
• Neither do I :P It's not like it's a "trick" you didn't know about... I've had a few of those too :) – Stewie Griffin Nov 3 '16 at 9:10