Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
20  
-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. – Easterly Irk Nov 2 at 23:56
10  
+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 at 0:04
29  
The non-palindrome restriction is probably a joke based on the previous challenge. Did anyone really downvote based on that? – Luis Mendo Nov 3 at 0:27
1  
@MamaFunRoll I feel that it adds nothing to the challenge, and might as well not be there, and thus probably shouldn't be there. – Easterly Irk Nov 3 at 1:24
4  
It does prevent single-byte solutions. @diynevala +1 for the unnecessary +1. – Adám 2 days ago

54 Answers 54

Julia, 21 15 bytes

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

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

share|improve this answer
2  
end/2 is cool feature – Downgoat Nov 3 at 3:07
    
@Downgoat yes, dennis showed it to me. – Easterly Irk Nov 3 at 3:16

05AB1E, 3 bytes

2ä¬

Uses the CP-1252 encoding. Try it online!

share|improve this answer

Python 2, 23 bytes

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

lambda s:s[:-~len(s)/2]
share|improve this answer
2  
If you are running on android, you can use QPython from the google play store. It's the best I've found :) – TuukkaX 2 days ago
    
termux apt-get install python2 – Matt 2 days ago
    
@Matt That's overkill if all you want is Python. – mbomb007 2 days ago
    
@Matt as well as that if you can find apt-get on your phone, it's probably not a normal phone. – MathManiac 7 hours ago

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.

share|improve this answer

05AB1E, 5 bytes

Dg;î£

Try it online!

Explanation:

D      Duplicate
 g;î   Divide length by two and round up
    £  First b letters of a
share|improve this answer

Pyth - 4 bytes

hc2Q

Test Suite.

share|improve this answer

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

share|improve this answer

WinDbg, 87 bytes

db$t0 L1;.for(r$t1=@$t0;@$p;r$t1=@$t1+1){db$t1 L1};r$t5=@$t0+(@$t1-@$t0)/2;eb$t5 0;da$t0

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)
};
r $t5 = @$t0 + (@$t1 - @$t0) / 2;            * Point $t5 to the right-middle of the string
eb $t5 0;                                    * Insert a null at $t5
da $t0                                       * Print 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};r$t5=@$t0+(@$t1-@$t0)/2;eb$t5 0;da$t0
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"
share|improve this answer

Haskell, 27 bytes

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

Pointfree version of

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

which is also 27 bytes.

share|improve this answer

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]
share|improve this answer
1  
Wow, that is a very neat way to handle complex values as arguments for slicing – miles Nov 3 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 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
share|improve this answer

V, 12 bytes

Two completely different solutions, both 12 bytes.

ò"Bx$xh|ò"bP

Try it online!

Ó./&ò
MjdGÍî

Try it online!

share|improve this answer

Java 7,57 bytes

String c(String s){return s.substring(0,s.length()/2+1);}
share|improve this answer
    
You're missing a closing } (so it's 57 bytes). – Kevin Cruijssen 2 days ago
1  
@KevinCruijssen fixed. – Numberknot 2 days ago

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)
share|improve this answer
1  
You can shorten to just s=>s.slice(0,s.length/2+1) Since length will always be odd – Downgoat Nov 2 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 at 0:19
1  
-s.length/2 works for both odd and even lengths. – Neil 2 days ago

TI-Basic, 14 bytes

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

sub(Ans,1,.5+.5length(Ans
share|improve this answer

GameMaker Language, 59 bytes

a=argument0 return string_copy(a,1,ceil(string_length(a)/2)
share|improve this answer

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

share|improve this answer

C, 31 bytes

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

Usage:

main(){
 char a[]="hellolleh";
 f(a);
 printf("%s\n",a);
}
share|improve this answer
    
@KevinCruijssen fixed – Karl Napf 2 days ago
    
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 2 days ago
2  
Well, it has the same length now, works for even+odd and looks golfier. I am okay with this. – Karl Napf 2 days ago
    
This is arguably a memory leak :-) – ShreevatsaR 2 days ago
    
@ShreevatsaR In case it was a malloc'ed string, the memory manager does not know if it is a string or anything else, so the free would free everything – Karl Napf yesterday

Dyalog APL, 9 bytes

⊢↑⍨2÷⍨1+≢

the argument

↑⍨ truncated at

2÷⍨ half of

1+ one plus

the length

TryAPL online!

share|improve this answer

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.

share|improve this answer
    
As the input is defined as coming from this (codegolf.stackexchange.com/questions/98325/…) challenge it's length will always be odd, so you can just go with your shorter one. – user59178 2 days ago

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.

share|improve this answer
2  
You can save two bytes, you don't need to set -n and -a because -F does so implicitly. – simbabque yesterday
    
@simbabque Yes. But for 5.20.0+ only. – Denis Ibaev yesterday

Python 2, 23 bytes

lambda x:x[:len(x)/2+1]
share|improve this answer
    
I think this requires Python 2; you should indicate that in your answer – Luis Mendo Nov 3 at 0:09
    
@LuisMendo oh, thanks! – Easterly Irk Nov 3 at 0:10

Actually, 7 bytes

2@l\ußH

Try it online!

Explanation:

2@l\ußH
2@l\u    len(input)//2 + 1
     ßH  first (len(input)//2 + 1) characters of input
share|improve this answer

Pyke, 4 bytes

le>_

Try it here!

l    -    len(input)
 e   -   ^//2
  >  -  input[^:]
   _ - reversed(^)
share|improve this answer

IBM/Lotus Notes Formula, 21 bytes

@Left(a;@Length(a)/2)

Computed field that takes input from an editable field a. Works because Notes rounds up when an odd number is divided by 2.

Test Cases

test case 1

test case 2

test case 3

test case 4

share|improve this answer
    
Thanks @Loovjo. Sorry, forgot to indent code block. – ElPedro 2 days ago

Retina, 24 bytes

I basically took this from another answer of mine.

^((.)*?.??)(?<-2>.)*$
$1

Try it online

share|improve this answer

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.

share|improve this answer

CJam, 7 bytes

r_,-2/<

Try it online!

share|improve this answer

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.

share|improve this answer
1  
you can save 3 chars by replacing length() with y|||c – ardnew 2 days ago

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.

share|improve this answer
1  
You beat me by half an hour and 7 bytes :) befunge.tryitonline.net/… – Brian Gradin 2 days ago
    
@BrianGradin, nice. now I've beat beat you by 9 bytes ;) – Linus 2 days ago
    
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 2 days ago

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.