48
\$\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\$
  • 23
    \$\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
  • 25
    \$\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
  • 42
    \$\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

66 Answers 66

1
\$\begingroup\$

Pyke, 4 bytes

le>_

Try it here!

l    -    len(input)
 e   -   ^//2
  >  -  input[^:]
   _ - reversed(^)
\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

Retina, 24 bytes

I basically took this from another answer of mine.

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

Try it online

\$\endgroup\$
1
\$\begingroup\$

CJam, 7 bytes

r_,-2/<

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

GO, 43 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python 2, 22 bytes

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

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

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

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

Hexagony, 56 bytes

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

Golfed:

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

In a hexagon:

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

Way to many no-ops...

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Oliver Ni Nov 14 '16 at 3:05
1
\$\begingroup\$

><>, 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
 1+     add one
   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.

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

R, 35 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Haskell, 27 bytes

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

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

\$\endgroup\$
0
\$\begingroup\$

Perl 6, 21 bytes

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

Expanded:

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

Brain-Flak, 45 bytes

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

Try it online!

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

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ You could replace len(c)+1 with -~len(c) and remove the parentheses \$\endgroup\$ – Loovjo Nov 3 '16 at 11:02
  • \$\begingroup\$ or len(c)//2+1 as I did in my solution \$\endgroup\$ – FlipTack Nov 3 '16 at 16:33
0
\$\begingroup\$

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
\$\endgroup\$
  • 2
    \$\begingroup\$ i post it before you and this time i beat you \$\endgroup\$ – Numberknot Nov 3 '16 at 8:54
  • \$\begingroup\$ @Numberknot It fails on palindromes of even length though. :) "abcddcba" outputs abcdd instead of abcd. \$\endgroup\$ – Kevin Cruijssen Nov 3 '16 at 8:56
  • \$\begingroup\$ @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.. \$\endgroup\$ – Kevin Cruijssen Nov 3 '16 at 8:58
0
\$\begingroup\$

R, 59 bytes

Not pretty but it gets the job done.

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

GolfSharp, 20 bytes

v=>v.R(0,v.L()/2+1);
\$\endgroup\$
0
\$\begingroup\$

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

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

Python 3, 28 26 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]
\$\endgroup\$
  • \$\begingroup\$ I think you can use one / for the second one \$\endgroup\$ – Farhan.K Nov 4 '16 at 16:36
  • \$\begingroup\$ @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 \$\endgroup\$ – FlipTack Nov 4 '16 at 16:42
  • \$\begingroup\$ Oh right, didn't see that it was python 3 :) \$\endgroup\$ – Farhan.K Nov 7 '16 at 9:32
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Ruby, 21 19 bytes

->s{s[0..s.size/2]}
\$\endgroup\$
  • \$\begingroup\$ you can use size \$\endgroup\$ – Cristian Lupascu Nov 3 '16 at 15:30
0
\$\begingroup\$

Lua, 28 bytes

x=...print(x:sub(1,#x//2+1))
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Scala, 21 bytes

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

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

\$\endgroup\$
0
\$\begingroup\$

Clojure, 30 bytes

Clojure port of basically every answer here:

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

Should be self explanatory.

\$\endgroup\$
0
\$\begingroup\$

C#, 26 bytes

s=>s.Remove(s.Length/2+1);

This removes the end of the string.

\$\endgroup\$

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.