回文高尔夫

发布于 2024-07-07 13:52:39 字数 213 浏览 10 评论 0原文

目标:任何语言。 返回字符串是否为回文的最小函数。 这是我的 Python 版本:

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

50 个字符。

接受的答案将是当前最小的答案 - 这将随着发现更小的答案而改变。 请指定您的代码所使用的语言。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

羁绊已千年 2024-07-14 13:52:40

使用 C# 和 LINQ 运算符:

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

如果您认为 Reverse 是作弊,那么您可以通过归约来完成整个事情:

public bool IsPalindrome(string s)
{
    return s.Aggregate(new StringBuilder(),
                       (sb, c) => sb.Insert(0, c),
                       (sb) => sb.ToString() == s);
}

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);
}
我很坚强 2024-07-14 13:52:40

Perl(27 个字符):

sub p{$_[0]eq reverse$_[0]}

Ruby(24 个字符):

def p(a)a==a.reverse end

Perl (27 chars):

sub p{$_[0]eq reverse$_[0]}

Ruby (24 chars):

def p(a)a==a.reverse end
陌上芳菲 2024-07-14 13:52:40

73 干净、可读、用 java

boolean p(String s){return s.equals(""+new StringBuffer(s).reverse());}

和平编写的字符:)

73 clean, readable, chars written in java

boolean p(String s){return s.equals(""+new StringBuffer(s).reverse());}

peace :)

弥繁 2024-07-14 13:52:40

无意义的 Haskell 版本(15 个字符,但除非包含 Control.Arrow 和 Control.Monad 并忽略单态限制,否则实际上不起作用):

p=ap(==)reverse

Pointless Haskell version (15 chars, though doesn't really work unless you include Control.Arrow and Control.Monad and ignore the monomorphism restriction):

p=ap(==)reverse
野の 2024-07-14 13:52:40

Lua 更注重可读性而不是简洁性,但确实有一个诚实的 37 个字符:

function p(s)return s==s:reverse()end

变体,只是为了好玩(相同大小):

p=function(s)return s==s:reverse''end

JavaScript 版本更详细(55 个字符),因为它没有字符串反转功能:

function p(s){return s==s.split('').reverse().join('')}

Lua aims more at readability than conciseness, yet does an honest 37 chars:

function p(s)return s==s:reverse()end

variant, just for fun (same size):

p=function(s)return s==s:reverse''end

The JavaScript version is more verbose (55 chars), because it doesn't has a string reverse function:

function p(s){return s==s.split('').reverse().join('')}
凤舞天涯 2024-07-14 13:52:40
(equal p (reverse p))

口齿不清。 18 个字符。

好吧,这是一个特殊情况。 如果直接输入 lisp 解释器并且 p 已经定义,那么这将起作用。

否则,这将是必要的:

(defun g () (equal p (reverse p)))

28 个字符。

(equal p (reverse p))

lisp. 18 characters.

ok, this is a special case. This would work if typed directly into a lisp interpreter and p was already defined.

otherwise, this would be necessary:

(defun g () (equal p (reverse p)))

28 characters.

薄暮涼年 2024-07-14 13:52:40

我会更进一步:完整的 C 代码,编译并运行。

90 个字符

main(int n,char**v){char*b,*e;b=e=v[1];while(*++e);for(e--;*b==*e&&b++<e--;);return b>e;}

I'll take it a little bit further: full c code, compile and go.

90 characters

main(int n,char**v){char*b,*e;b=e=v[1];while(*++e);for(e--;*b==*e&&b++<e--;);return b>e;}
岁月无声 2024-07-14 13:52:40

F#(很像 C# 示例)

let p s=let i=0;let l=s.Length;while(++i<l)if(s[i]!=[l-i-1]) 0; 1;;

F# (a lot like the C# example)

let p s=let i=0;let l=s.Length;while(++i<l)if(s[i]!=[l-i-1]) 0; 1;;
黑色毁心梦 2024-07-14 13:52:40

PHP:

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

或者,只是

$s==strrev($s); // 15 chars

PHP:

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

or, just

$s==strrev($s); // 15 chars
傲鸠 2024-07-14 13:52:40

在你的语言中使用反向函数是不是有点作弊? 我的意思是,看看 Ruby 解决方案,

def p(a)a==a.reverse end

您可以轻松地将其重写为

def p(a)a==a.r end

,只需说您在代码中创建了一个扩展方法,以便“r”称为反向。 我希望看到人们发布不包含对其他函数的调用的解决方案。 当然,字符串长度函数应该是允许的。

没有反向的 Ruby - 41 个字符

def m(a)a==a.split('').inject{|r,l|l+r}end

VB.Net - 173 个字符

Function P(ByVal S As String) As Boolean
    For i As Integer = 0 To S.Length - 1
        If S(i) <> S(S.Length - i - 1) Then
            Return False
        End If
    Next
    Return True
End Function

Isn't using the reverse function in your language kind of cheating a bit? I mean, looking at the Ruby solution give as

def p(a)a==a.reverse end

you could easily rewrite that as

def p(a)a==a.r end

and just say that you made an extension method in your code so that "r" called reverse. I'd like to see people post solutions that don't contain calls to other functions. Of course, the string length function should be permitted.

Ruby without reverse - 41 characters

def m(a)a==a.split('').inject{|r,l|l+r}end

VB.Net - 173 Chars

Function P(ByVal S As String) As Boolean
    For i As Integer = 0 To S.Length - 1
        If S(i) <> S(S.Length - i - 1) Then
            Return False
        End If
    Next
    Return True
End Function
巾帼英雄 2024-07-14 13:52:40

高尔夫脚本,5 个字符

.-1%=

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

$ echo -n deadbeef | ruby golfscript.rb palindrome.gs
0

Golfscript, 5 char

.-1%=

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

$ echo -n deadbeef | ruby golfscript.rb palindrome.gs
0
佞臣 2024-07-14 13:52:40

Common Lisp,简短的作弊版本(23 个字符):

#L(equal !1(reverse !1))

#L 是由 SHARPL-READER 在 iterate 包中实现的读取器宏字符。 它基本上等同于 (lambda (!1) ...)。

Common Lisp,仅使用原语的长版本(137 个包括空格,可压缩到 108 个):

(defun p (s)
  (let ((l (1- (length s))))
    (iter (for i from l downto (/ l 2))
          (always (equal (elt s i) (elt s (- l i)))))))

它再次使用 iterate,这基本上是内置 LOOP 工具的更清晰版本,因此我倾向于将其视为核心语言。

Common Lisp, short-and-cheating version (23 chars):

#L(equal !1(reverse !1))

#L is a reader macro character implemented by SHARPL-READER in the iterate package. It's basically equivalent to (lambda (!1) ...).

Common Lisp, long version using only primitives (137 including whitespace, compressible down to 108):

(defun p (s)
  (let ((l (1- (length s))))
    (iter (for i from l downto (/ l 2))
          (always (equal (elt s i) (elt s (- l i)))))))

Again, it uses iterate, which is basically a cleaner version of the builtin LOOP facility, so I tend to treat it as being in the core language.

孤千羽 2024-07-14 13:52:40

不是最短的,而且非常事后,但我忍不住在 MATLAB 中尝试一下:

R=@(s)all(s==fliplr(s));

24 个字符。

Not the shortest, and very after-the-fact, but I couldn't help giving it a try in MATLAB:

R=@(s)all(s==fliplr(s));

24 chars.

酒与心事 2024-07-14 13:52:40

C# 无反向函数 84 个字符

int p(char[]s){int i=0,l=s.Length,t=1;while(++i<l)if(s[i]!=s[l-i-1])t&=0;return t;} 

C# 无反向函数 86 个字符

int p(char[]s){int i=0;int l=s.Length;while(++i<l)if(s[i]!=s[l-i-1])return 0;return 1;}

VBScript 41 个字符

function p:p=s=strreverse(s):end function

C# Without Reverse Function 84 chars

int p(char[]s){int i=0,l=s.Length,t=1;while(++i<l)if(s[i]!=s[l-i-1])t&=0;return t;} 

C# Without Reverse Function 86 chars

int p(char[]s){int i=0;int l=s.Length;while(++i<l)if(s[i]!=s[l-i-1])return 0;return 1;}

VBScript 41 chars

function p:p=s=strreverse(s):end function
一花一树开 2024-07-14 13:52:40

18 个字符的 Perl 正则表达式

/^(.?|(.)(?1)\2)$/

18 character perl regex

/^(.?|(.)(?1)\2)$/
嘿哥们儿 2024-07-14 13:52:40

C 语言中的 52 个字符,但需要注意的是,最多一半的字符串将被覆盖:

p(char*s){return!*s||!(s[strlen(s)-1]-=*s) &&p(++s);}

如果没有库调用,它是 64 个字符:

p(char*s){char*e​​=s;while(*e)++e;return! *s||!(*--e-=*s)&&p(++s);}

52 characters in C, with the caveat that up to half the string will be overwritten:

p(char*s){return!*s||!(s[strlen(s)-1]-=*s)&&p(++s);}

Without library calls it's 64 characters:

p(char*s){char*e=s;while(*e)++e;return!*s||!(*--e-=*s)&&p(++s);}

浅笑依然 2024-07-14 13:52:40

受上一篇文章的启发,69 个字符

p(char*a){char*b=a,q=0;while(*++b);while(*a)q|=*a++!=*--b;return!q;}

编辑:向下一个字符:

p(char*a){char*b=a,q=0;while(*++b);while(*a)q|=*a++%*--b;return!q;}

编辑2:65 个字符:

p(char*a){char*b=a;while(*b)b++;while(*a&&*a++==*--b);return!*a;}

Inspired by previous post, 69 characters

p(char*a){char*b=a,q=0;while(*++b);while(*a)q|=*a++!=*--b;return!q;}

EDIT: Down one char:

p(char*a){char*b=a,q=0;while(*++b);while(*a)q|=*a++%*--b;return!q;}

EDIT2: 65 chars:

p(char*a){char*b=a;while(*b)b++;while(*a&&*a++==*--b);return!*a;}
清引 2024-07-14 13:52:40

Haskell,28 个字符,需要导入 Control.Arrow。

p=uncurry(==).(id&&&reverse)

Haskell, 28 chars, needs Control.Arrow imported.

p=uncurry(==).(id&&&reverse)
梦亿 2024-07-14 13:52:40

使用标准库函数在 C 中直接实现,受到其他 C 答案中 strlen 的启发。

字符数:57

p(char*s){char*r=strdup(s);strrev(r);return strcmp(r,s);}

坦白:我不释放这里的r就是坏人。 我目前的尝试是:

p(char*s){char*r=strdup(s);s[0]=strcmp(strrev(r),s);free(r);return s[0];}

将其增加到 73 个字符; 我正在考虑有什么办法可以缩短时间。

Straightforward implementation in C using standard library functions, inspired by the strlen in the other C answer.

Number of characters: 57

p(char*s){char*r=strdup(s);strrev(r);return strcmp(r,s);}

Confession: I'm being the bad guy by not freeing r here. My current attempt at being good:

p(char*s){char*r=strdup(s);s[0]=strcmp(strrev(r),s);free(r);return s[0];}

brings it to 73 characters; I'm thinking of any ways to do it shorter.

比忠 2024-07-14 13:52:40

Clojure 使用 37 个字符:

user=> (defn p[s](=(seq s)(reverse(seq s))))
#'user/p
user=> (p "radar")
true
user=> (p "moose")
false

Clojure using 37 characters:

user=> (defn p[s](=(seq s)(reverse(seq s))))
#'user/p
user=> (p "radar")
true
user=> (p "moose")
false
执着的年纪 2024-07-14 13:52:40

Perl 中的 24 个字符。

sub p{$_[0]eq+reverse@_}

24 characters in Perl.

sub p{$_[0]eq+reverse@_}
不即不离 2024-07-14 13:52:40

格罗维 17B:

p={it==it[-1..0]}

缺点是它不适用于空字符串。

再想一想,抛出空字符串异常是合理的,因为你无法判断是否有任何东西是回文。

Groovy 17B:

p={it==it[-1..0]}

Downside is that it doesn't work with emptry string.

On second thought, throwing exception for empty string is reasonable since you can't tell if nothing is palindrome or not.

萌梦深 2024-07-14 13:52:40

不使用任何库函数(因为您实际上还应该添加 #include 成本),这是 96 中的 C++ 版本:

int p(char*a,char*b=0,char*c=0){return c?b<a||p(a+1,--b,c)&&*a==*b:b&&*b?p(a,b+1):p(a,b?b:a,b);}

Without using any library functions (because you should really add in the #include cost as well), here's a C++ version in 96:

int p(char*a,char*b=0,char*c=0){return c?b<a||p(a+1,--b,c)&&*a==*b:b&&*b?p(a,b+1):p(a,b?b:a,b);}
一身仙ぐ女味 2024-07-14 13:52:40

我在 C 中的尝试(70 个字符):

P(char*s){char*e=s+strlen(s)-1;while(s<e&&*s==*e)s++,e--;return s>=e;}

[编辑]现在正在实际工作
[编辑 2] 使用默认 int return 从 74 减少到 70

回应一些评论:我不确定预处理器滥用是否计数 - 您可以在命令行上定义整个内容并使函数成为一个字符。

My attempt in C (70 chars):

P(char*s){char*e=s+strlen(s)-1;while(s<e&&*s==*e)s++,e--;return s>=e;}

[Edit] Now actually working
[Edit 2] Reduced from 74 to 70 by using default int return

In response to some of the comments: I'm not sure if that preprocessor abuse counts - you could just define the whole thing on the command line and make the function one character.

剩一世无双 2024-07-14 13:52:39

J 中的 7 个字符:不确定这是否是最好的方法,我对 J 有点陌生:)

p=:-:|.

解释:|。 反转输入。 -:比较。 操作数是隐式的。

p 'radar'
1

p 'moose'
0

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
私藏温柔 2024-07-14 13:52:39

这是我的; 它是用我发明的一种特定于领域的语言编写的,称为“回文”。

p

编辑: 更少的轻率版本(i386 asm,AT&T 语法)

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 个字节,字符串指针位于 ESI 中,结果位于 EAX 中。

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.

瞄了个咪的 2024-07-14 13:52:39

遗憾的是,我无法理解一千个字...

alt text

(LabVIEW.是的,他们会让任何流浪汉在这里发帖;)

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

alt text

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

不弃不离 2024-07-14 13:52:39

Haskell,15 个字符:

p=ap(==)reverse

更可读的版本,16 个字符:

p x=x==reverse x

Haskell, 15 chars:

p=ap(==)reverse

More readable version, 16 chars:

p x=x==reverse x
高冷爸爸 2024-07-14 13:52:39

另一个相当短的 python 版本(21 个字符):

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

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

R=lambda s:s==s[::-1]
冬天旳寂寞 2024-07-14 13:52:39

冒着投票失败的风险,大多数这些都只是调用某种隐藏所有真实编程逻辑的命令reverse

我想知道在每种语言中执行此操作的最短手动方法是什么。

At the risk of getting down votes, most all of these just call a command reverse of some sort that hides all the real programming logic.

I wonder what the shortest manual way to do this is in each of these languages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文