Depalindromize this string!

48

3

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.

Oliver Ni

Posted 2016-11-02T23:37:49.327

Reputation: 9 650

23-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. – Rɪᴋᴇʀ – 2016-11-02T23:56:21.633

25+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 – 2016-11-03T00:04:28.967

42The non-palindrome restriction is probably a joke based on the previous challenge. Did anyone really downvote based on that? – Luis Mendo – 2016-11-03T00:27:00.450

1@Titus for that challenge, it almost always required extra work to make the code a palindrome. Here, it almost always requires no extra work to make the code not a palindrome. – Rɪᴋᴇʀ – 2016-11-03T00:27:24.437

2@EasterlyIrk Don't you see the symmetry? It's quite beautiful, if I do say so myself. But to be serious, I really don't see anything wrong with the so-called "pointless restriction" that would warrant a downvote. It might be trivial, but it's not the main focus of the challenge. The main focus of the challenge is to depalindromize the string, not to create a non-palindromic code. – Mama Fun Roll – 2016-11-03T01:23:28.123

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. – Rɪᴋᴇʀ – 2016-11-03T01:24:34.087

1@EasterlyIrk You could also argue that because it adds nothing to the challenge, it might as well be there as nothing will happen either way. Really, in this case the final say goes to the OP. After all, it's sort of nice to have complementary challenges like this. – Mama Fun Roll – 2016-11-03T01:29:21.880

5It does prevent single-byte solutions. @diynevala +1 for the unnecessary +1. – Adám – 2016-11-03T10:29:35.753

@Adám So it is essentially "do X without Y" challenge as well. My +1 is not unnecessary, as it supports the choice to restrict palindromous code – diynevala – 2016-11-03T10:57:09.667

@LuisMendo No, it's not a joke, the [tag:restricted-source] tag exists. I have -1'd. – Erik the Outgolfer – 2016-11-03T15:14:58.327

2@EriktheGolfer But the restriction is virtually ineffective. It's very unlikely that the unrestricted code turns out to be palindromic – Luis Mendo – 2016-11-03T15:21:35.500

@LuisMendo Hypothesis: What if I made a language which happened to have an depalindromize operator (let's say q)? I couldn't use it if I didn't do <space>q instead :( – Erik the Outgolfer – 2016-11-03T15:31:02.330

Palindrome tag is not appropriate here, is there a "non-palindrome" tag? :) – AlexRacer – 2016-11-03T23:06:48.033

5What if the string is not a palindrome to begin with? – Xavon_Wrentaile – 2016-11-03T23:45:37.033

2What is expected output when the string is one char long? What is the expected output for empty string (is empty string a palindrome to begin with?)? – Viktor Mellgren – 2016-11-04T11:50:51.307

@LuisMendo BTW, I didn't -1 but I also didn't +1. – Rɪᴋᴇʀ – 2016-11-04T13:09:24.473

1@EasterlyIrk You must not play Magic the Gathering. – corsiKa – 2016-11-04T21:43:51.900

@Xavon_Wrentaile I agree with the notion of that. I almost only see special forms of list accessing, instead of interesting logic, simply because recognizing palindromes does not seem to be a requirement. – Zelphir Kaltstahl – 2016-11-06T12:26:41.987

Answers

14

Julia, 21 15 bytes

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

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

Rɪᴋᴇʀ

Posted 2016-11-02T23:37:49.327

Reputation: 7 410

2end/2 is cool feature – Downgoat – 2016-11-03T03:07:19.937

@Downgoat yes, dennis showed it to me. – Rɪᴋᴇʀ – 2016-11-03T03:16:04.510

12

05AB1E, 3 bytes

2ä¬

Uses the CP-1252 encoding. Try it online!

Adnan

Posted 2016-11-02T23:37:49.327

Reputation: 41 965

6

Python 2, 23 bytes

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

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

Kade

Posted 2016-11-02T23:37:49.327

Reputation: 7 463

2If you are running on android, you can use QPython from the google play store. It's the best I've found :) – Yytsi – 2016-11-03T08:29:07.823

termux apt-get install python2 – Matt – 2016-11-03T12:43:01.603

@Matt That's overkill if all you want is Python. – mbomb007 – 2016-11-03T13:56:02.587

@Matt as well as that if you can find apt-get on your phone, it's probably not a normal phone. – Lawful Lazy – 2016-11-06T00:04:05.717

@MathManiac termux is installed from Google Play onto any non rooted Android phone. Can't get much more normal than that. – Matt – 2016-11-09T13:45:17.493

6

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.

Rɪᴋᴇʀ

Posted 2016-11-02T23:37:49.327

Reputation: 7 410

5

05AB1E, 5 bytes

Dg;î£

Try it online!

Explanation:

D      Duplicate
 g;î   Divide length by two and round up
    £  First b letters of a

acrolith

Posted 2016-11-02T23:37:49.327

Reputation: 3 728

5

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

Downgoat

Posted 2016-11-02T23:37:49.327

Reputation: 27 116

4

Pyth - 4 bytes

hc2Q

Test Suite.

Maltysen

Posted 2016-11-02T23:37:49.327

Reputation: 25 023

4

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)

Hedi

Posted 2016-11-02T23:37:49.327

Reputation: 1 857

1You can shorten to just s=>s.slice(0,s.length/2+1) Since length will always be odd – Downgoat – 2016-11-02T23:56:01.070

@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 – 2016-11-03T00:19:13.427

2-s.length/2 works for both odd and even lengths. – Neil – 2016-11-03T10:17:00.040

4

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"

milk

Posted 2016-11-02T23:37:49.327

Reputation: 3 043

3

Haskell, 27 bytes

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

Pointfree version of

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

which is also 27 bytes.

nimi

Posted 2016-11-02T23:37:49.327

Reputation: 34 639

3

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]

Luis Mendo

Posted 2016-11-02T23:37:49.327

Reputation: 87 464

1Wow, that is a very neat way to handle complex values as arguments for slicing – miles – 2016-11-03T00:55:26.303

@miles Thanks! Yes, it's handy. The imaginary unit works as end, and colons between the array elements are implicit – Luis Mendo – 2016-11-03T01:01:47.513

3

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

Luis Mendo

Posted 2016-11-02T23:37:49.327

Reputation: 87 464

3

V, 12 bytes

Two completely different solutions, both 12 bytes.

ò"Bx$xh|ò"bP

Try it online!

Ó./&ò
MjdGÍî

Try it online!

James

Posted 2016-11-02T23:37:49.327

Reputation: 54 537

3

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

Fatalize

Posted 2016-11-02T23:37:49.327

Reputation: 32 976

3

Java 7, 57 bytes

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

Numberknot

Posted 2016-11-02T23:37:49.327

Reputation: 885

You're missing a closing } (so it's 57 bytes). – Kevin Cruijssen – 2016-11-03T09:09:26.363

1@KevinCruijssen fixed. – Numberknot – 2016-11-03T09:24:36.720

3

Dyalog APL, 9 bytes

⊢↑⍨2÷⍨1+≢

the argument

↑⍨ truncated at

2÷⍨ half of

1+ one plus

the length

TryAPL online!

Adám

Posted 2016-11-02T23:37:49.327

Reputation: 37 779

3

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.

Ton Hospel

Posted 2016-11-02T23:37:49.327

Reputation: 14 114

2

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.

Titus

Posted 2016-11-02T23:37:49.327

Reputation: 13 814

As the input is defined as coming from this (http://codegolf.stackexchange.com/questions/98325/palindromize-this-string) challenge it's length will always be odd, so you can just go with your shorter one.

– user59178 – 2016-11-03T12:34:37.910

2

TI-Basic, 14 bytes

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

sub(Ans,1,.5+.5length(Ans

Timtech

Posted 2016-11-02T23:37:49.327

Reputation: 12 038

2

GameMaker Language, 59 bytes

a=argument0 return string_copy(a,1,ceil(string_length(a)/2)

Timtech

Posted 2016-11-02T23:37:49.327

Reputation: 12 038

2

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);
}

Karl Napf

Posted 2016-11-02T23:37:49.327

Reputation: 4 131

@KevinCruijssen fixed – Karl Napf – 2016-11-03T08:59:21.610

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 – 2016-11-03T09:00:37.927

2Well, it has the same length now, works for even+odd and looks golfier. I am okay with this. – Karl Napf – 2016-11-03T09:02:30.657

This is arguably a memory leak :-) – ShreevatsaR – 2016-11-04T01:47:50.810

@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 – 2016-11-05T00:18:37.140

Good point! (We're keeping around memory during the program's execution that we don't need, but you're right that when the string is freed, all of its memory is freed.) – ShreevatsaR – 2016-11-05T00:25:04.297

1I think you can remove the space in char* c – Cyoce – 2016-12-13T16:25:25.483

@Cyoce Yes i can, thank you! – Karl Napf – 2016-12-17T00:22:11.717

Does setting the middle character to zero delete the rest of the string? (Not too familiar with C) – Cyoce – 2016-12-17T00:26:38.007

@Cyoce A string in C is delimited by a \0, so printing it afterwards yields only the first half. Technically, the tail is still present, but all string related functions only count up to the \0. – Karl Napf – 2016-12-17T00:36:42.777

2

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

Mego

Posted 2016-11-02T23:37:49.327

Reputation: 32 998

6 bytes: ;l½K@H :D – Sherlock9 – 2016-11-08T18:59:23.360

2

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.

Denis Ibaev

Posted 2016-11-02T23:37:49.327

Reputation: 876

1you can save 3 chars by replacing length() with y|||c – ardnew – 2016-11-03T20:33:16.533

2

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.

Oliver Ni

Posted 2016-11-02T23:37:49.327

Reputation: 9 650

2

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

ElPedro

Posted 2016-11-02T23:37:49.327

Reputation: 5 301

1If 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. – Steven H. – 2016-11-07T10:08:02.263

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 :) – ElPedro – 2016-11-07T10:16:54.890

1No problem! If you ever need any help, try pinging me in the Nineteenth byte. – Steven H. – 2016-11-07T10:19:35.200

2

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.

Denis Ibaev

Posted 2016-11-02T23:37:49.327

Reputation: 876

2You can save two bytes, you don't need to set -n and -a because -F does so implicitly. – simbabque – 2016-11-04T12:05:36.600

@simbabque Yes. But for 5.20.0+ only. – Denis Ibaev – 2016-11-04T18:24:48.030

2

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.

Linus

Posted 2016-11-02T23:37:49.327

Reputation: 1 948

1

You beat me by half an hour and 7 bytes :) http://befunge.tryitonline.net/#code=K346MGAhI3ZfXDEKLzIrJTI6JDx2Cl9AIzotMSxcPA&input=Y29kZWdvbGZsb2dlZG9j

– Brian Gradin – 2016-11-03T22:18:42.377

@BrianGradin, nice. now I've beat beat you by 9 bytes ;) – Linus – 2016-11-03T22:42:56.793

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 – 2016-11-03T23:07:39.700

2

Brainfuck, 20 bytes

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

Try it online.

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

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

Mitch Schwartz

Posted 2016-11-02T23:37:49.327

Reputation: 4 899

1

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

redstarcoder

Posted 2016-11-02T23:37:49.327

Reputation: 1 771

1

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

user85052

Posted 2016-11-02T23:37:49.327

Reputation:

1

R, 35 bytes

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

Try it online!

Sumner18

Posted 2016-11-02T23:37:49.327

Reputation: 1 334

1

Python 2, 23 bytes

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

Rɪᴋᴇʀ

Posted 2016-11-02T23:37:49.327

Reputation: 7 410

I think this requires Python 2; you should indicate that in your answer – Luis Mendo – 2016-11-03T00:09:38.770

@LuisMendo oh, thanks! – Rɪᴋᴇʀ – 2016-11-03T00:10:37.230

1

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.

Luis Mendo

Posted 2016-11-02T23:37:49.327

Reputation: 87 464

@StewieGriffin Thanks! I don't know what I was thinking... – Luis Mendo – 2016-11-03T09:08:26.560

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 :) – Stewie Griffin – 2016-11-03T09:10:29.800

1

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.

hstde

Posted 2016-11-02T23:37:49.327

Reputation: 159

1

Pyke, 4 bytes

le>_

Try it here!

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

Blue

Posted 2016-11-02T23:37:49.327

Reputation: 26 661

1

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

ElPedro

Posted 2016-11-02T23:37:49.327

Reputation: 5 301

1

Retina, 24 bytes

I basically took this from another answer of mine.

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

Try it online

mbomb007

Posted 2016-11-02T23:37:49.327

Reputation: 21 944

1

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"

rnso

Posted 2016-11-02T23:37:49.327

Reputation: 1 635

1

CJam, 7 bytes

r_,-2/<

Try it online!

Linus

Posted 2016-11-02T23:37:49.327

Reputation: 1 948

1

GO, 43 bytes

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

Try it online!

powelles

Posted 2016-11-02T23:37:49.327

Reputation: 1 277

1

Python 2, 22 bytes

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

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

Niclas M

Posted 2016-11-02T23:37:49.327

Reputation: 61

Welcome to PPCG! However, I get edcba instead of abcde when I give the string abcdedcba. Please fix this. – Oliver Ni – 2016-11-05T20:00:48.443

You can actually fix it by changing len(_)/2: to :-~len(_)/2. – Oliver Ni – 2016-11-05T20:03:09.800

@Oliver It is depalindromized ;p I'll fix it ASAP. – Niclas M – 2016-11-05T20:14:48.937

1

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.

Mitchell Humphrey

Posted 2016-11-02T23:37:49.327

Reputation: 348

Welcome to PPCG! – Oliver Ni – 2016-11-14T03:05:17.840

1

Hexagony, 56 bytes

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

Golfed:

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

In a hexagon:

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

Way to many no-ops...

Blue

Posted 2016-11-02T23:37:49.327

Reputation: 1 986

0

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.

L3viathan

Posted 2016-11-02T23:37:49.327

Reputation: 3 151

0

Japt, 5 bytes

¯ÒUÊz

Try it here

Shaggy

Posted 2016-11-02T23:37:49.327

Reputation: 24 623

0

Ruby, 21 19 bytes

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

dkudriavtsev

Posted 2016-11-02T23:37:49.327

Reputation: 5 781

you can use size – Cristian Lupascu – 2016-11-03T15:30:41.100

0

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.

xnor

Posted 2016-11-02T23:37:49.327

Reputation: 115 687

0

Perl 6, 21 bytes

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

Expanded:

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

Brad Gilbert b2gills

Posted 2016-11-02T23:37:49.327

Reputation: 12 713

0

Dyalog APL, 9 bytes

⊢↑⍨2÷⍨1+⍴

Thanks to Adám for saving 4 bytes.

Zacharý

Posted 2016-11-02T23:37:49.327

Reputation: 5 710

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 . – Adám – 2016-11-03T10:53:04.143

0

Brain-Flak, 45 bytes

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

Try it online!

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

James

Posted 2016-11-02T23:37:49.327

Reputation: 54 537

0

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]

FlipTack

Posted 2016-11-02T23:37:49.327

Reputation: 13 242

I think you can use one / for the second one – Farhan.K – 2016-11-04T16:36:50.090

@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 – FlipTack – 2016-11-04T16:42:23.460

Oh right, didn't see that it was python 3 :) – Farhan.K – 2016-11-07T09:32:29.973

0

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

Kevin Cruijssen

Posted 2016-11-02T23:37:49.327

Reputation: 67 575

2i post it before you and this time i beat you – Numberknot – 2016-11-03T08:54:20.867

@Numberknot It fails on palindromes of even length though. :) "abcddcba" outputs abcdd instead of abcd. – Kevin Cruijssen – 2016-11-03T08:56:32.633

@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.. – Kevin Cruijssen – 2016-11-03T08:58:51.157

0

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

hintss

Posted 2016-11-02T23:37:49.327

Reputation: 9

You could replace len(c)+1 with -~len(c) and remove the parentheses – Loovjo – 2016-11-03T11:02:18.443

or len(c)//2+1 as I did in my solution – FlipTack – 2016-11-03T16:33:32.343

0

R, 59 bytes

Not pretty but it gets the job done.

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

rturnbull

Posted 2016-11-02T23:37:49.327

Reputation: 3 689

0

GolfSharp, 20 bytes

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

downrep_nation

Posted 2016-11-02T23:37:49.327

Reputation: 1 152

0

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

Chavez

Posted 2016-11-02T23:37:49.327

Reputation: 109

This seems to print the characters on different lines. https://tio.run/nexus/powershell#@6@SWJReHG0QG22gp6ehAePp@aTmpZdk6Bpq6htpxv7//z85PyU1PT8nLSc/PTUlPxkA Or am I doing something wrong?

– Dennis – 2016-12-10T17:08:47.427

Yeah, it does. As far as the rules go this should be fine though. – Chavez – 2016-12-12T07:24:01.683

0

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.

steenbergh

Posted 2016-11-02T23:37:49.327

Reputation: 7 772

0

Lua, 28 bytes

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

IDid

Posted 2016-11-02T23:37:49.327

Reputation: 101

0

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.

coredump

Posted 2016-11-02T23:37:49.327

Reputation: 6 292

0

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.

PhiNotPi

Posted 2016-11-02T23:37:49.327

Reputation: 26 739

0

Scala, 21 bytes

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

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

corvus_192

Posted 2016-11-02T23:37:49.327

Reputation: 1 889

0

Clojure, 30 bytes

Clojure port of basically every answer here:

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

Should be self explanatory.

Carcigenicate

Posted 2016-11-02T23:37:49.327

Reputation: 3 295

0

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);           
    }
}

BananyaDev

Posted 2016-11-02T23:37:49.327

Reputation: 41

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. – BananyaDev – 2016-11-05T20:44:41.660

Probably because it looks like you just copied @Numberknot's answer, without giving him any credit...

– Dada – 2016-11-06T09:57:43.880

0

C#, 26 bytes

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

This removes the end of the string.

Hedi

Posted 2016-11-02T23:37:49.327

Reputation: 1 857

0

R, 35 bytes

Always late to the party.

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

Billywob

Posted 2016-11-02T23:37:49.327

Reputation: 3 363

0

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"

IMP1

Posted 2016-11-02T23:37:49.327

Reputation: 510