Computer Science Canada

Quick contest (all languages)

Author:  md [ Mon Oct 24, 2005 11:10 pm ]
Post subject:  Quick contest (all languages)

Ok... so I gave the challenge on IRC, but I figure I'll open it to anyone on compsci

The Challenge: write a function to check to see if a string is a palendrome. It should only one parameter, the string, and return a boolean value of true if the string is a palendrome, or false if it is not.
[edit] You can optionally also pass a second parameter to specify the length of the string, just to reduce the overhead of recursion...

The Judges: I'll judge who wins by who can write the shortest code (instruction wise), who can write the fastest code (order n wise), and who's code is easiest to read. I might get a few other people (wtd, rizzix) to help judge too if I can.

The prize: the proze will be all my bits devided amongst the winners, or if there is a mod who will sponsor this however much they feel like giving.

Author:  [Gandalf] [ Mon Oct 24, 2005 11:15 pm ]
Post subject: 

code:
fcn checkPalindrome (word : string) : boolean
    for i : 1 .. length (word) div 2
        if word (i) ~= word (length (word) - i + 1) then
            result false
        end if
    end for
    result true
end checkPalindrome

Author:  md [ Mon Oct 24, 2005 11:18 pm ]
Post subject: 

My C/C++ entry so people have an idea how one could do it

c++:

bool Palindrome(char *string, int length)
{
        return (length <= 0) || (string[0] == string[length-1]) && Palindrome(++string, length-2);
}

Author:  beard0 [ Mon Oct 24, 2005 11:20 pm ]
Post subject: 

Beat this :)
Turing:
fcn isPalindrome (str : string) : boolean
    result length(str)<=1 or (str (1) = str (length (str)) and isPalindrome (str (2 .. length (str) - 1)))
end isPalindrome

Hooray for Turing short circuit logic evaluation!

Author:  beard0 [ Mon Oct 24, 2005 11:29 pm ]
Post subject: 

By the way Cornflake's "idea how one could do it" doesn't actually work, you need to expand on it.

Author:  md [ Mon Oct 24, 2005 11:31 pm ]
Post subject: 

Yes, yes... so my mind was a little slow after my exam... it's changed and it does now work, I've tested it.

[edit] arg... it's harder then it seems to write it on one line... see my sig
[edit 2] my god I'm dumb... but it now works... tested verilly...

Author:  wtd [ Tue Oct 25, 2005 12:14 am ]
Post subject: 

O'Caml:

code:
let rec palindrome str =
  let len = String.length str in
    if len <= 1 then true
    else if str.[0] = str.[len - 1] then
      palindrome (String.sub str 1 (len - 2))
    else false

Author:  Martin [ Tue Oct 25, 2005 12:18 am ]
Post subject: 

For an added challenge:
Do it as a one liner recursive call to main in C, returning a number other than 0 if argv[0] is a palindrome, 0 if it isn't.

code:
int main (int argc; char**argv) {
    return <yourcodehere>;
}

Author:  wtd [ Tue Oct 25, 2005 12:42 am ]
Post subject: 

Martin wrote:
For an added challenge:
Do it as a one liner recursive call to main in C, returning a number other than 0 if argv[0] is a palindrome, 0 if it isn't.

code:
int main (int argc; char**argv) {
    return <yourcodehere>;
}


Did you actually mean argv[0]? That's typically the executable name.

Author:  Martin [ Tue Oct 25, 2005 1:15 am ]
Post subject: 

Sorry, yeah, I forgot about that. It's been a while since I've used C. argv[1];

Author:  TokenHerbz [ Tue Oct 25, 2005 2:02 am ]
Post subject: 

To: Cornflake

you said you'd gimmy 5 bits on IRC for trying Smile

When can i expect them?

Author:  wtd [ Tue Oct 25, 2005 3:10 am ]
Post subject: 

Ruby:

code:
class String
   def is_palindrome?
      if length <= 1
         true
      elsif self[0] == self[-1]
         self[1..-2].is_palindrome?
      else
         false
      end
   end
end

Author:  Tony [ Tue Oct 25, 2005 7:41 am ]
Post subject: 

Ruby:

class String
  def is_palindrome?
    self == self.reverse
  end
end

Laughing

Author:  md [ Wed Oct 26, 2005 10:51 am ]
Post subject: 

Methinks I'll leave this open until next weekend and then I'll judge winners... and martin; I've almost figured out how to beat your challenge Razz

Author:  goomba [ Wed Oct 26, 2005 5:56 pm ]
Post subject: 

Python for the win!

code:

def isPalindrome(word):
        if word[::-1] in word:
                return True
        return False

Author:  Hikaru79 [ Fri Oct 28, 2005 10:00 pm ]
Post subject: 

goomba wrote:
Python for the win!

code:

def isPalindrome(word):
        if word[::-1] in word:
                return True
        return False


Drat!! Sad I was just about to post that.

Just for the record, you can make this a one-liner:
code:
def isPalindrome(word): return word[::-1]==word

Author:  goomba [ Fri Oct 28, 2005 10:03 pm ]
Post subject: 

Ooh... that's true. Razz

Author:  rizzix [ Fri Oct 28, 2005 10:31 pm ]
Post subject: 

Haskell:
isPalindrome s = s == reverse s


pwned? Wink

Author:  wtd [ Fri Oct 28, 2005 10:50 pm ]
Post subject: 

Why bother with the explicit type signature?

Author:  rizzix [ Fri Oct 28, 2005 11:09 pm ]
Post subject: 

true true... fixed... eitherway do you know a good one-liner?

here's the best i could come up with:
Haskell:
fmap (\s -> s == reverse s) (Just "aaaa")

Author:  goomba [ Sat Oct 29, 2005 1:22 pm ]
Post subject: 

Hikaru79 wrote:
goomba wrote:
Python for the win!

code:

def isPalindrome(word):
        if word[::-1] in word:
                return True
        return False


Drat!! Sad I was just about to post that.

Just for the record, you can make this a one-liner:
code:
def isPalindrome(word): return word[::-1]==word


Going even further, how about a real one-liner:

Python:
isPalindrome=lambda word: word[::-1]==word


There we go. Razz

Author:  md [ Sun Oct 30, 2005 1:57 pm ]
Post subject: 

Weekend -> Contest is over. However I'm lazy and have lots of study to do so methinks I'll leave hte judging until later... (if at all). There are some interesting solutions, though the one line solutions are only one line because all the work is being done by the language and other routines (ei. because they cheat Razz). I did leave it open to all languages however so I suppose I can't complain.

And so no one can cheat... here are all the entries...

Gandalf:
code:
fcn checkPalindrome (word : string) : boolean
    for i : 1 .. length (word) div 2
        if word (i) ~= word (length (word) - i + 1) then
            result false
        end if
    end for
    result true
end checkPalindrome



beard0:
code:
fcn isPalindrome (str : string) : boolean
    result length(str)<=1 or (str (1) = str (length (str)) and isPalindrome (str (2 .. length (str) - 1)))
end isPalindrome



wtd:
code:
let rec palindrome str =
  let len = String.length str in
    if len <= 1 then true
    else if str.[0] = str.[len - 1] then
      palindrome (String.sub str 1 (len - 2))
    else false

code:
class String
   def is_palindrome?
      if length <= 1
         true
      elsif self[0] == self[-1]
         self[1..-2].is_palindrome?
      else
         false
      end
   end
end


Tony:
code:
class String
  def is_palindrome?
    self == self.reverse
  end
end


Goomba:
code:
def isPalindrome(word):
        if word[::-1] in word:
                return True
        return False

code:
isPalindrome=lambda word: word[::-1]==word


Hiraku79:
code:

def isPalindrome(word): return word[::-1]==word


rizzix:
code:
isPalindrome s = s == reverse s

code:
fmap (\s -> s == reverse s) (Just "aaaa")


Cornflake:
code:
bool Palindrome(char *string, int length)
{
        return (length <= 0) || (string[0] == string[length-1]) && Palindrome(++string, length-2);
}


: