代码高尔夫:反向奎因
编写一个程序,将其源代码的反转输出为字符串。如果源是
abcd
efg
(即 C 字符串 "abcd\nefg"
),
输出应该是
gfe
dcba
(即 C 字符串 "gfe\ndcba"
)
那么 使用诸如 Brainf*ck 之类的深奥语言。
*编辑:**删除了不必要的 \0 字符。+
Write a program that outputs the reverse of its source code as a string. If the source is
abcd
efg
(i.e., the C string "abcd\nefg"
)
Then the output should be
gfe
dcba
(i.e., the C string "gfe\ndcba"
)
Bonus points for using esoteric languages such as brainf*ck.
*EDIT:** Removed the unnecessary \0 characters.+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
C:0个字符
参考:http://www0.us.ioccc.org/年.html#1994_smr
C : 0 chars
Ref: http://www0.us.ioccc.org/years.html#1994_smr
HQ9+:
在HQ9+深奥语言中,此代码可能是:
在这里您可以找到该语言的解释器。
Ruby:
来自此处的反向奎因。
HQ9+:
In HQ9+ esoteric language this code might be:
Here You can find interpreter for that language.
Ruby:
Reversed quine from here.
Powershell FTW(1 个字符):
将其直接放在命令行上或脚本内。
Powershell FTW (1 character):
Put it directly on the command line, or inside a script.
这是两行代码,改编自 NeatQuine.py :
Here's a two-liner, adapted from NeatQuine.py:
C89
C89
Bash 脚本
(10 个字符)
必须将其保存为脚本文件才能在同一目录中工作和执行。
另一个解决方案是使用 python(或任何其他脚本语言)零字节源代码文件!它不会打印任何回报。规则中没有规定它不能是 0byte 文件:)。
Bash script
(10 Charecters)
This must be saved as a script file to work and executed on the same directory.
Another solution would be in python (or any other scripting languages) a zero byte source code file! it will print nothing in return. There's nothing in the rules saying it can't be 0byte file :).
J,26 个字符。
产生输出:
J, 26 characters.
Produces the output:
我可能会在高尔夫比赛中输掉比赛,但它给我上了重要的一课,让我了解了
reverse()
的微妙之处。 Perl 方式太多 (142) 个字符:这证明
sprintf()
/reverse()
组合不是解决这个问题的方法。更好的 Perl 解决方案无疑会使用 eval()。巨大改进:45 个字符:
请注意,源文件应以空行结尾。空行被计入字符计数 - 否则你认为我们如何从两行相同的代码中得到奇数字符计数?
I'm going to lose at golf, but it taught me an important lesson about the subtleties of
reverse()
. Perl in way too many (142) characters:This proves that the
sprintf()
/reverse()
combination is not the way to approach this problem. A better Perl solution will undoubtedly useeval()
.Vast improvement: 45 characters:
Note that the source file should end in a blank line. The blank line is counted in the character count - how else do you think we got an odd character count out of two identical lines of code?
Python 2(55 个字符):
更好的高尔夫球手可能能够稍微缩短此长度,因此欢迎任何改进。
编辑(43个字符):
也感谢@stephan202捕捉打印上的空白
Python 2 (55 char):
A better golfer might be able to shorten this somewhat, so any improvements are welcome.
Edit (43 char):
also thanks to @stephan202 for catching the whitespace on prints
F#(659 个字符)
插入换行符(这会破坏程序,但使其在此处更具可读性):
F# (659 chars)
Inserting line breaks (that break the program, but make it more readable here):
在 C 语言中,217 个字符:
In C , 217 chars :
Javascript - IE/Chrome/FF
Javascript - IE/Chrome/FF
在 Python 交互式提示符处:
27 个字符。
At the Python interactive prompt:
27 characters.
C89,119 个字符
不幸的是,这需要使用高度非标准的函数
strrev()
:C89, 119 characters
Unfortunately, this requires use of the highly non-standard function
strrev()
:Bash, 75 chars
好吧,当然一个空的 shell 脚本不会输出任何内容,但是下一个我能想到的最好的事情:
同样的技巧。
Bash, 75 chars
Well, of course an empty shell script will output nothing, but the next best thing I can think of:
Same ol' trick.
UNIX shell:
我倾向于重复我自己:这些程序应该被称为“Goedels”,因为大多数此类事物背后的想法(在现代)首先由 Kurt Goedel 在证明他的第一个不完备性定理中使用(库尔特·哥德尔的全集 I,第 175 页)。
UNIX shell:
I tend to repeat my self: these programs ought to be referred to as `Goedels' because the idea behind a majority of such things was first used (in modern times) by Kurt Goedel in the proof of his First Incompleteness Theorem (Kurt Goedel's Collected Works I, p.175).
Perl
73 个字符。
__DATA__
才能开始打开 DATA
文件句柄。$/
设置为数字的引用,会导致readline()
一次读取那么多字节。seek(DATA,0,0)
将指针设置为文件的开头,而不是__DATA__
部分的开头。#!
...)__DATA__
后面需要换行符,否则它不是有效的 Perl。Perl
73 characters.
__DATA__
at the end for theDATA
file-handle to start out opened.$/
to a reference of a number, causesreadline()
to read that many bytes at a time.seek(DATA,0,0)
is required to set the pointer to the beginning of the file, instead of at the beginning of the__DATA__
section.#!
...)__DATA__
requires a newline after it, or it isn't valid Perl.Haskell,79 个字符
标准技巧。
Haskell, 79 characters
Standard trick.
Java:
绝对不是最短的,但到底是什么(一行):
public class R{public static void main(String[] a){StringBuffer s = new StringBuffer("public类R{public static void main(String[] a){StringBuffer s = new StringBuffer();s.reverse();System.out.print(s.substring(0,152));System.out.write(34) ;System.out.print(s);System.out.write(34);System.out.println(s.substring(152));}}");s.reverse();System.out.print( s.substring(0,152));System.out.write(34);System.out.print(s);System.out.write(34);System.out.println(s.substring(152));} }
Java:
Definitely not the shortest possible, but what the heck (one line):
public class R{public static void main(String[] a){StringBuffer s = new StringBuffer("public class R{public static void main(String[] a){StringBuffer s = new StringBuffer();s.reverse();System.out.print(s.substring(0,152));System.out.write(34);System.out.print(s);System.out.write(34);System.out.println(s.substring(152));}}");s.reverse();System.out.print(s.substring(0,152));System.out.write(34);System.out.print(s);System.out.write(34);System.out.println(s.substring(152));}}
PHP 36 字节
PHP 36 bytest
C89(155 字节)
C89 (155 bytes)
python(34 个字符):
python (34 chars):
PHP
Snip - 删除了不正确的答案
现在要赎回自己;
PHP
Snip - removed incorrect answer
And now to redeem myself;
JavaScript:您想要多少个字符?
第二个最简单的有效程序(使用我的方法):240 个字符! (包括空格但不包括换行)
JavaScript: How many characters do you want?
SECOND SIMPLEST VALID PROGRAM (using my method): 240 Chars! (including spaces but not new lines)
C/C++:
C/C++: