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. Nov 2, 2016 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? Nov 3, 2016 at 0:04
• The non-palindrome restriction is probably a joke based on the previous challenge. Did anyone really downvote based on that? Nov 3, 2016 at 0:27
• It does prevent single-byte solutions. @diynevala +1 for the unnecessary +1.
Nov 3, 2016 at 10:29
• What if the string is not a palindrome to begin with? Nov 3, 2016 at 23:45

Pyke, 4 bytes

le>_


Try it here!

l    -    len(input)
e   -   ^//2
>  -  input[^:]
_ - reversed(^)


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

Retina, 24 bytes

I basically took this from another answer of mine.

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


Try it online

CJam, 7 bytes

r_,-2/<


Try it online!

C# 51 50 bytes

s=>string.Join("",s.Where((_,i)=>i<=s.Count()/2));


It's a lambda, woo!
You can catch it with:

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


it requires the string to be a palindrome.

GO, 43 bytes

func(s string)string{return s[:len(s)/2+1]}

Try it online!

Python 2, 22 bytes

lambda _:_[len(_)/2:]


Depalindromizes a given string. Still fails on all the test cases. Weird... ;-)

• Welcome to PPCG! However, I get edcba instead of abcde when I give the string abcdedcba. Please fix this. Nov 5, 2016 at 20:00
• You can actually fix it by changing len(_)/2: to :-~len(_)/2. Nov 5, 2016 at 20:03
• @Oliver It is depalindromized ;p I'll fix it ASAP. Nov 5, 2016 at 20:14

Racket 48 bytes

(substring s 0(+ 1(floor(/(string-length s)2))))


Ungolfed:

(define (f s)
(substring s 0 (+ 1
(floor
(/ (string-length s)
2)))))


Testing:

(f "abcdedcba")
(f "johncenanecnhoj")
(f "ppapapp")
(f "codegolflogedoc")


Output:

"abcde"
"johncena"
"ppap"
"codegolf"


Hexagony, 56 bytes

This is awful, but I just wanted to do it in Hexagony :)

Golfed:

,<_..){/"./..\&..$@....>;{.....'..>"+"+"&"='&'&=}\}=.:(<  In a hexagon:  , < _ . . ) { / " . / . . \ & . .$
@ . . . . > ; {
. . . . . ' . . >
" + " + " & " =
' & ' & = } \
} = . : ( <
. . . . .


Way to many no-ops...

Python 3.5, 87 Bytes

x=list(input())
for q in range(0,int(round((len(x)/2)-0.1,0))):x.pop()
print(''.join(x))


i'm new to this codegolfing, but it's to be fun. I probably won't win but oh well.

• Welcome to PPCG! Nov 14, 2016 at 3:05

><>, 7 15 bytes

l1+2,[v
;!?lo<


Try it here!

This is a full program. To use it simply place the palindrome on the stack, and the result will be outputted to stdout!

Explanation

l       get the stack length
2,   divide by two
[  pop the value, create a new stack of that length


That's the meat of it, ol?!; just outputs until the stack is empty.

*><>, 11 bytes (non-competing)

l5+2,]r{[$.  Try it here! (first line is a program that calls the function, outputs the stack, then exits) This is a function. To use it simply call it (instruction C) with the palindrome on the stack, and the result will be on the stack! This part is non-competing because *><> is younger than the challenge. Explanation l get the stack length 5+ add five 2, divide by two ] close the stack r reverse the stack { shift the stack left [ pop the value on the end, create a new stack of that length$    swap the two values on the end of the stack
.   jump back, with the result on the stack


For ><> coders who might be confused by this, a new stack is created when a function is called, so you can actually call ] right from the start if you're inside a function. The data inside this stack is the x, y coordinate of the ><> when C was called.

Keg, 7 bytes

^(!2/|_


That is easy but really hard to golf.

Explanation

^       # Input in the right order
(!2/   # Repeat half of length times:
|_ # Pop the stack


TIO

R, 35 bytes

substr(x<-scan(,''),1,nchar(x)/2+1)


Try it online!

Vyxal, 2 bytes

½h


Try it Online!

Makes a list of each half of the string and takes the first item. Vyxal rounds up when splitting odd numbers in half. Don't think this can be golfed to 1 byte because the code can't be a palindrome :p

f[x]=[x]
f(a:b)=a:f(init b)


No indexing. Recursively takes from the start and end, keeping letters from the end.

Perl 6, 21 bytes

*.substr(0,1+* div 2)


Expanded:

*\            # Whatever lambda
.substr(
0,
1 + * div 2 # Whatever lambda
)


Brain-Flak, 45 bytes

([][()]<>){({}[()()])<>{}<>}<>{({}<>)<>}<>


Try it online!

42 bytes of code, and +3 for the -c flag which enables ASCII input and output.

Python 3, 26 bytes

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


This squeezes 2 chars out of the other Python3 answer by replacing the int() with division with //.

• You could replace len(c)+1 with -~len(c) and remove the parentheses Nov 3, 2016 at 11:02
• or len(c)//2+1 as I did in my solution Nov 3, 2016 at 16:33

Java 7, 57 bytes

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


At first I misinterpreted the question and also made code that supported even palindromes (which isn't necessary due to this being the reverse of that other challenge, and therefore there aren't any even palindrome test-cases). So I deleted my answer.
After seeing @KarlNapf's great answer however, I undeleted and edited this. It's the same byte count as the other Java 7 answer, but works for both odd and even length palindromes (although only odd were asked for this challenge).

Ungolfed & test code:

Try it here.

class M{
static String c(String s){
return s.substring(0, -~s.length()/2);
}

public static void main(String[] a){
System.out.println(c("abcdedcba"));
System.out.println(c("abcddcba"));
}
}


Output:

abcde
abcd

• i post it before you and this time i beat you Nov 3, 2016 at 8:54
• @Numberknot It fails on palindromes of even length though. :) "abcddcba" outputs abcdd instead of abcd. Nov 3, 2016 at 8:56
• @Numberknot Nevermind, I'm an idiot. Although I'm correct in saying it doesn't work for even palindromes, since this is the reverse of that other challenge, there won't be any test cases for even palindromes. I'll delete my answer.. Nov 3, 2016 at 8:58

R, 59 bytes

Not pretty but it gets the job done.

cat(strsplit(scan(,""),"")[[1]][0:(nchar(x)%/%2)+1],sep="")


GolfSharp, 20 bytes

v=>v.R(0,v.L()/2+1);


PowerShell, 46 36 bytes

Unfortunately there's no PowerShell on Try it Online. Alas, here's my attempt.

$args[0][0..(($args[0].Length-1)/2)]

• This seems to print the characters on different lines. tio.run/nexus/… Or am I doing something wrong? Dec 10, 2016 at 17:08
• Yeah, it does. As far as the rules go this should be fine though. Dec 12, 2016 at 7:24

Python 3, 2826 24 bytes

Python 3 strings don't like non-int indexes...

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


Alternatively, with the same byte count,

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

• I think you can use one / for the second one Nov 4, 2016 at 16:36
• @Farhan.K no - in py3, / creates a float which is not a valid index, so floordiv (//) has to be used. The single slash, however, works in python 2 Nov 4, 2016 at 16:42
• Oh right, didn't see that it was python 3 :) Nov 7, 2016 at 9:32

QBIC, 18 bytes

;?left|(A,_lA|/2)


; gets a command line parameter, then we print the left half of it using _l. Works fine for even and odd-length strings.

Ruby, 21 19 bytes

->s{s[0..s.size/2]}

• you can use size Nov 3, 2016 at 15:30

Lua, 28 bytes

x=...print(x:sub(1,#x//2+1))


Common Lisp, 43

(lambda(s)(subseq s 0(ceiling(length s)2)))


Truly hard to golf without accidentally writing a palindrome, will all those ( matching opposite ).

I know, this is not what "palindrome" means.

Element, 11 bytes

_2:$'[()#]  Try it online! _2:$'[()#]
_             take input
2:           make a second copy
\$          length of string
'[    ]   FOR loop
[(  ]   chop off then print the first character
[  )#]   chop off then discard the last character


The FOR loop is actually executed about twice as many times as it needs to be, but those extra iterations do not affect the output since it runs out of data to print.

Scala, 21 bytes

a=>a.take(a.size/2+1)


requires an assignment with a type annotation: val f:(Seq[_]=>Seq[_])...

Clojure, 30 bytes

Clojure port of basically every answer here:

#(subs % 0(inc(/(count %)2)))
`

Should be self explanatory.