Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Palindrome Golf

7 characters in J: Not sure if this is the best way, I'm somewhat new to J :)

p=:-:|.

explanation: |. reverses the input. -: compares. the operands are implicit.

p 'radar'
1

p 'moose'
0

Here's mine; it's written in a domain-specific language I invented, called 'palindrome'.

p

Edit: Less flippant version (i386 asm, AT&T syntax)

xor %eax, %eax
mov %esi, %edi
#cld    not necessary, assume DF=0 as per x86 ABI
repne scasb
scan:
    dec %edi
    cmpsb
    .byte 0x75, 6    #jnz (short) done
    dec %edi
    cmp %esi, %edi
    .byte 0x72, -9    #jb (short) scan
inc %eax
done:

16 bytes, string pointer goes in ESI, result is in EAX.


Sadly, I'm unable to get under a thousand words...

alt text

(LabVIEW. Yeah, they'll let just about any hobo post here ;)


Haskell, 15 chars:

p=ap(==)reverse

More readable version, 16 chars:

p x=x==reverse x

Another python version that is rather shorter (21 chars):

R=lambda s:s==s[::-1]