47
\$\begingroup\$

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

Test cases

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

As this is about depalindromizing, your code cannot be a palindrome.

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

\$\endgroup\$
  • 22
    \$\begingroup\$ -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. \$\endgroup\$ – Rɪᴋᴇʀ Nov 2 '16 at 23:56
  • 24
    \$\begingroup\$ +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? \$\endgroup\$ – Titus Nov 3 '16 at 0:04
  • 41
    \$\begingroup\$ The non-palindrome restriction is probably a joke based on the previous challenge. Did anyone really downvote based on that? \$\endgroup\$ – Luis Mendo Nov 3 '16 at 0:27
  • 5
    \$\begingroup\$ It does prevent single-byte solutions. @diynevala +1 for the unnecessary +1. \$\endgroup\$ – Adám Nov 3 '16 at 10:29
  • 5
    \$\begingroup\$ What if the string is not a palindrome to begin with? \$\endgroup\$ – Xavon_Wrentaile Nov 3 '16 at 23:45

63 Answers 63

13
\$\begingroup\$

Julia, 21 15 bytes

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

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

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

05AB1E, 3 bytes

2ä¬

Uses the CP-1252 encoding. Try it online!

\$\endgroup\$
6
\$\begingroup\$

Python 2, 23 bytes

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

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

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.

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

05AB1E, 5 bytes

Dg;î£

Try it online!

Explanation:

D      Duplicate
 g;î   Divide length by two and round up
    £  First b letters of a
\$\endgroup\$
5
\$\begingroup\$

Cheddar, 22 18 bytes

@.head($0.len/2+1)

So simple I don't think needs explanation but I'll add one if wanted.

Try it online

\$\endgroup\$
4
\$\begingroup\$

Pyth - 4 bytes

hc2Q

Test Suite.

\$\endgroup\$
4
\$\begingroup\$

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)
\$\endgroup\$
  • 1
    \$\begingroup\$ You can shorten to just s=>s.slice(0,s.length/2+1) Since length will always be odd \$\endgroup\$ – Downgoat Nov 2 '16 at 23:56
  • \$\begingroup\$ @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. \$\endgroup\$ – Hedi Nov 3 '16 at 0:19
  • 2
    \$\begingroup\$ -s.length/2 works for both odd and even lengths. \$\endgroup\$ – Neil Nov 3 '16 at 10:17
4
\$\begingroup\$

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

Haskell, 27 bytes

take=<<succ.(`div`2).length

Pointfree version of

\x->take(div(length x)2+1)x

which is also 27 bytes.

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

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]
\$\endgroup\$
  • 1
    \$\begingroup\$ Wow, that is a very neat way to handle complex values as arguments for slicing \$\endgroup\$ – miles Nov 3 '16 at 0:55
  • \$\begingroup\$ @miles Thanks! Yes, it's handy. The imaginary unit works as end, and colons between the array elements are implicit \$\endgroup\$ – Luis Mendo Nov 3 '16 at 1:01
3
\$\begingroup\$

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

V, 12 bytes

Two completely different solutions, both 12 bytes.

ò"Bx$xh|ò"bP

Try it online!

Ó./&ò
MjdGÍî

Try it online!

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

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

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

Dyalog APL, 9 bytes

⊢↑⍨2÷⍨1+≢

the argument

↑⍨ truncated at

2÷⍨ half of

1+ one plus

the length

TryAPL online!

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

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.

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

Java 7, 57 bytes

String c(String s){return s.substring(0,s.length()/2+1);}
\$\endgroup\$
  • \$\begingroup\$ You're missing a closing } (so it's 57 bytes). \$\endgroup\$ – Kevin Cruijssen Nov 3 '16 at 9:09
  • 1
    \$\begingroup\$ @KevinCruijssen fixed. \$\endgroup\$ – Numberknot Nov 3 '16 at 9:24
2
\$\begingroup\$

TI-Basic, 14 bytes

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

sub(Ans,1,.5+.5length(Ans
\$\endgroup\$
2
\$\begingroup\$

GameMaker Language, 59 bytes

a=argument0 return string_copy(a,1,ceil(string_length(a)/2)
\$\endgroup\$
2
\$\begingroup\$

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.

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

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.

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

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.

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

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ You beat me by half an hour and 7 bytes :) befunge.tryitonline.net/… \$\endgroup\$ – Brian Gradin Nov 3 '16 at 22:18
  • \$\begingroup\$ @BrianGradin, nice. now I've beat beat you by 9 bytes ;) \$\endgroup\$ – Linus Nov 3 '16 at 22:42
  • \$\begingroup\$ 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. \$\endgroup\$ – Brian Gradin Nov 3 '16 at 23:07
2
\$\begingroup\$

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.

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

Brainfuck, 20 bytes

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

Try it online.

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

,[>,]
<
[
  [<]
  >.,>[>]
  <,<
]
\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Steven H. Nov 7 '16 at 10:08
  • \$\begingroup\$ 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 :) \$\endgroup\$ – ElPedro Nov 7 '16 at 10:16
  • 1
    \$\begingroup\$ No problem! If you ever need any help, try pinging me in the Nineteenth byte. \$\endgroup\$ – Steven H. Nov 7 '16 at 10:19
2
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ 6 bytes: ;l½K@H :D \$\endgroup\$ – Sherlock9 Nov 8 '16 at 18:59
2
\$\begingroup\$

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);
}
\$\endgroup\$
  • \$\begingroup\$ @KevinCruijssen fixed \$\endgroup\$ – Karl Napf Nov 3 '16 at 8:59
  • \$\begingroup\$ 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. :) \$\endgroup\$ – Kevin Cruijssen Nov 3 '16 at 9:00
  • 2
    \$\begingroup\$ Well, it has the same length now, works for even+odd and looks golfier. I am okay with this. \$\endgroup\$ – Karl Napf Nov 3 '16 at 9:02
  • \$\begingroup\$ This is arguably a memory leak :-) \$\endgroup\$ – ShreevatsaR Nov 4 '16 at 1:47
  • 1
    \$\begingroup\$ I think you can remove the space in char* c \$\endgroup\$ – Cyoce Dec 13 '16 at 16:25
1
\$\begingroup\$

Python 2, 23 bytes

lambda x:x[:len(x)/2+1]
\$\endgroup\$
  • \$\begingroup\$ I think this requires Python 2; you should indicate that in your answer \$\endgroup\$ – Luis Mendo Nov 3 '16 at 0:09
  • \$\begingroup\$ @LuisMendo oh, thanks! \$\endgroup\$ – Rɪᴋᴇʀ Nov 3 '16 at 0:10
1
\$\begingroup\$

MATLAB / Octave, 20 19 18 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)

Try it at Ideone.

\$\endgroup\$
  • \$\begingroup\$ @StewieGriffin Thanks! I don't know what I was thinking... \$\endgroup\$ – Luis Mendo Nov 3 '16 at 9:08
  • \$\begingroup\$ 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 :) \$\endgroup\$ – Stewie Griffin Nov 3 '16 at 9:10

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.