Goodie which tests if a string is a palindrome. Format 'is <string> a[n] palindrome[?]'.

master
Ben Mays 2012-05-01 19:25:27 -04:00
parent 418102d6da
commit 347cb52489
1 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package DDG::Goodie::Palindrome;
# ABSTRACT: Return if the a string is a palindrome, formatted like: is <string> a palindrome
# TODO: Add flexibility to query. Regex? (^is\s)|\s((a|an)\s)|(palindrome\?$|palindrome$)|(^isPalindrome)
use DDG::Goodie;
triggers query_clean => qr/(^is\s)|\s((a|an)\s)(palindrome\?$|palindrome$)|(^isPalindrome)/i;
handle query_clean => sub {
#Remove the trigger text from the query.
$query = $_ if $_ =~ s/(^is\s)|\s((a|an)\s)(palindrome\?$|palindrome$)|(^isPalindrome)//g;
#Return if no parameter is passed/matched.
return if !$query;
#Reverse the string.
$rev = (scalar reverse $query);
$resp = $query . " is not a palindrome.";
$resp = $query . " is a palindrome!" if ($query eq $rev);
return $resp;
return;
};
zci is_cached => 1;
1;