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

0
\$\begingroup\$

R, 35 bytes

Always late to the party.

substr(x<-scan(,""),1,nchar(x)/2+1)
\$\endgroup\$
0
\$\begingroup\$

Ruby, 20 bytes

->n{n[0,-~n.size/2]}

anonymous lambda that returns the first half (rounded up) of a string. The -~n is a 1+n with a higher priority than the division by two, so I don't need any parentheses. Unlike the other Ruby solution, this works on strings with even lengths.

Usage:

->n{n[0,-~n.size/2]}["abba"]
->n{n[0,-~n.size/2]}["abcba"]
->n{n[0,-~n.size/2]}["a"]

Returns:

"ab"
"abc"
"a"
\$\endgroup\$
0
\$\begingroup\$

Java 8, 33 bytes

Same logic as @Numberknot, but with 24 less bytes due to the use of lambdas. I tried to add this as an update to @Numberknot but with no response therefore I posted this as a separate answer since it's a big difference in bytes between his and my answer.

s->s.substring(0,s.length()/2+1);

Ungolfed version:

interface Func { String foo(String s); }


public class Main
{
    public static void main(String[] args)
    {
        Func s = s->s.substring(0,s.length()/2+1);           
    }
}
\$\endgroup\$
  • \$\begingroup\$ why -1? The other Java answers are pretty much the same only written in Java 7. Java 8 with the use of lambdas makes the answer shorter. \$\endgroup\$ – BananyaDev Nov 5 '16 at 20:44
  • \$\begingroup\$ Probably because it looks like you just copied @Numberknot's answer, without giving him any credit... \$\endgroup\$ – Dada Nov 6 '16 at 9:57
0
\$\begingroup\$

Dyalog APL, 9 bytes

⊢↑⍨2÷⍨1+⍴

Thanks to Adám for saving 4 bytes.

\$\endgroup\$
  • \$\begingroup\$ I just saw your answer now. You should know that constants are allowed as the left tine of a fork, so 2÷⍨ works. Since constants are only allowed as left tine, you can swap the arguments of commutative functions to make them permitted, so 1+⍴ works. For non-commutative functions, use . \$\endgroup\$ – Adám Nov 3 '16 at 10:53
0
\$\begingroup\$

OIL, 61 bytes

5
0
12
0
26
1
26
24
14
div
0
24
1
0
19
8
19
13
27

0
4
0
3

2

This uses the div from the standard library.

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

Japt, 5 bytes

¯ÒUÊz

Try it here

\$\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.