Palindrome Golf

2019-01-30 05:31发布

The goal: Any language. The smallest function which will return whether a string is a palindrome. Here is mine in Python:

R=lambda s:all(a==b for a,b in zip(s,reversed(s)))

50 characters.

The accepted answer will be the current smallest one - this will change as smaller ones are found. Please specify the language your code is in.

30条回答
Juvenile、少年°
2楼-- · 2019-01-30 05:34

PHP:

function p($s){return $s==strrev($s);} // 38 chars

or, just

$s==strrev($s); // 15 chars
查看更多
贼婆χ
3楼-- · 2019-01-30 05:34

18 character perl regex

/^(.?|(.)(?1)\2)$/
查看更多
霸刀☆藐视天下
4楼-- · 2019-01-30 05:35

Golfscript, 5 char

.-1%=

$ echo -n abacaba | ruby golfscript.rb palindrome.gs
1

$ echo -n deadbeef | ruby golfscript.rb palindrome.gs
0
查看更多
老娘就宠你
5楼-- · 2019-01-30 05:35

Clojure using 37 characters:

user=> (defn p[s](=(seq s)(reverse(seq s))))
#'user/p
user=> (p "radar")
true
user=> (p "moose")
false
查看更多
放我归山
6楼-- · 2019-01-30 05:36

Haskell, 28 chars, needs Control.Arrow imported.

p=uncurry(==).(id&&&reverse)
查看更多
Evening l夕情丶
7楼-- · 2019-01-30 05:37

With C# and LINQ operators:

public bool IsPalindrome(string s)
{
    return s.Reverse().SequenceEqual(s);
}

If you consider Reverse as cheating, you can do the entire thing with a reduction:

public bool IsPalindrome(string s)
{
    return s.Aggregate(new StringBuilder(),
                       (sb, c) => sb.Insert(0, c),
                       (sb) => sb.ToString() == s);
}
查看更多
登录 后发表回答