代码高尔夫:反向奎因

发布于 2024-08-03 03:36:47 字数 293 浏览 5 评论 0原文

编写一个程序,将其源代码的反转输出为字符串。如果源是

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 技术交流群。

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

发布评论

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

评论(25

拥醉 2024-08-10 03:36:47

HQ9+:

HQ9+深奥语言中,此代码可能是:

Q

在这里您可以找到该语言的解释器

Ruby:

来自此处的反向奎因。

eval s=%q(puts "eval s=%q(#{s})".reverse)

HQ9+:

In HQ9+ esoteric language this code might be:

Q

Here You can find interpreter for that language.

Ruby:

Reversed quine from here.

eval s=%q(puts "eval s=%q(#{s})".reverse)
昇り龍 2024-08-10 03:36:47

Powershell FTW(1 个字符):

1

将其直接放在命令行上或脚本内。

Powershell FTW (1 character):

1

Put it directly on the command line, or inside a script.

山川志 2024-08-10 03:36:47

这是两行代码,改编自 NeatQuine.py :

me = 'me = %(me)r\nprint (me %% locals())[::-1]'
print (me % locals())[::-1]

Here's a two-liner, adapted from NeatQuine.py:

me = 'me = %(me)r\nprint (me %% locals())[::-1]'
print (me % locals())[::-1]
柒夜笙歌凉 2024-08-10 03:36:47

C89

int sprintf(char*,char*,...);char*d=
"int sprintf(char*,char*,...);char*d=%c%c%s%c%c,b[999],*p=b+251;main(){for(sprintf(b+1,d,10,34,d,34,10,10,10);*p;)putchar(*p--);}%c"
,b[999],*p=b+251;main(){for(sprintf(b+1,d,10,34,d,34,10,10,10);*p;)putchar(*p--);}

C89

int sprintf(char*,char*,...);char*d=
"int sprintf(char*,char*,...);char*d=%c%c%s%c%c,b[999],*p=b+251;main(){for(sprintf(b+1,d,10,34,d,34,10,10,10);*p;)putchar(*p--);}%c"
,b[999],*p=b+251;main(){for(sprintf(b+1,d,10,34,d,34,10,10,10);*p;)putchar(*p--);}
塔塔猫 2024-08-10 03:36:47

Bash 脚本

(10 个字符)

cat $0|rev

必须将其保存为脚本文件才能在同一目录中工作和执行。

另一个解决方案是使用 python(或任何其他脚本语言)零字节源代码文件!它不会打印任何回报。规则中没有规定它不能是 0byte 文件:)。

Bash script

(10 Charecters)

cat $0|rev

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 :).

疯狂的代价 2024-08-10 03:36:47

J,26 个字符。

|.(,~,2#{:)'|.(,~,2#{:)'''

产生输出:

'''):{#2,~,(.|'):{#2,~,(.|

J, 26 characters.

|.(,~,2#{:)'|.(,~,2#{:)'''

Produces the output:

'''):{#2,~,(.|'):{#2,~,(.|
给不了的爱 2024-08-10 03:36:47

我可能会在高尔夫比赛中输掉比赛,但它给我上了重要的一课,让我了解了 reverse() 的微妙之处。 Perl 方式太多 (142) 个字符:

#!/usr/bin/perl
$_='#!/usr/bin/perlc%$_=c%s%c%;print sprintf~~reverse,10,39,~~reverse,39,10;c%';print sprintf~~reverse,10,39,~~reverse,39,10;

这证明 sprintf()/reverse() 组合不是解决这个问题的方法。更好的 Perl 解决方案无疑会使用 eval()。


巨大改进:45 个字符:

print~~reverse <<''x2
print~~reverse <<''x2

请注意,源文件应以空行结尾。空行被计入字符计数 - 否则你认为我们如何从两行相同的代码中得到奇数字符计数?

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:

#!/usr/bin/perl
$_='#!/usr/bin/perlc%$_=c%s%c%;print sprintf~~reverse,10,39,~~reverse,39,10;c%';print sprintf~~reverse,10,39,~~reverse,39,10;

This proves that the sprintf()/reverse() combination is not the way to approach this problem. A better Perl solution will undoubtedly use eval().


Vast improvement: 45 characters:

print~~reverse <<''x2
print~~reverse <<''x2

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?

顾冷 2024-08-10 03:36:47

Python 2(55 个字符):

x='x=%s;print(x%%repr(x))[::-1]';print(x%repr(x))[::-1]

更好的高尔夫球手可能能够稍微缩短此长度,因此欢迎任何改进。

编辑(43个字符):

x='x=%r;print(x%%x)[::-1]';print(x%x)[::-1]

也感谢@stephan202捕捉​​打印上的空白

Python 2 (55 char):

x='x=%s;print(x%%repr(x))[::-1]';print(x%repr(x))[::-1]

A better golfer might be able to shorten this somewhat, so any improvements are welcome.

Edit (43 char):

x='x=%r;print(x%%x)[::-1]';print(x%x)[::-1]

also thanks to @stephan202 for catching the whitespace on prints

雨的味道风的声音 2024-08-10 03:36:47

F#(659 个字符)

open System;let R(s:String)=new System.String(s|>List.of_seq|>List.rev|>Array.of_list) in let q=char 34 in let Q(s:String)=s.Replace(new String([|q|]),new String([|char 92;q|])) in let Quine s=String.Format("let s={0}{1}{2} in printf {3}%s%s{4} (R(Quine s)) (R s)",[|box q;box(Q s);box q;box q;box q|]) in let s="open System;let R(s:String)=new System.String(s|>List.of_seq|>List.rev|>Array.of_list) in let q=char 34 in let Q(s:String)=s.Replace(new String([|q|]),new String([|char 92;q|])) in let Quine s=String.Format(\"let s={0}{1}{2} in printf {3}%s%s{4} (R(Quine s)) (R s)\",[|box q;box(Q s);box q;box q;box q|]) in " in printf "%s%s" (R(Quine s)) (R s)

插入换行符(这会破坏程序,但使其在此处更具可读性):

open System;
let R(s:String)=new System.String(s|>List.of_seq|>List.rev|>Array.of_list) in 
let q=char 34 in 
let Q(s:String)=s.Replace(new String([|q|]),new String([|char 92;q|])) in 
let Quine s=String.Format("let s={0}{1}{2} in printf {3}%s%s{4} (R(Quine s)) (R s)",
    [|box q;box(Q s);box q;box q;box q|]) in 
let s="open System;
       let R(s:String)=new System.String(s|>List.of_seq|>List.rev|>Array.of_list) in 
       let q=char 34 in 
       let Q(s:String)=s.Replace(new String([|q|]),new String([|char 92;q|])) in 
       let Quine s=String.Format(\"let s={0}{1}{2} in printf {3}%s%s{4} (R(Quine s)) (R s)\",
           [|box q;box(Q s);box q;box q;box q|]) in " in
printf "%s%s" (R(Quine s)) (R s)

F# (659 chars)

open System;let R(s:String)=new System.String(s|>List.of_seq|>List.rev|>Array.of_list) in let q=char 34 in let Q(s:String)=s.Replace(new String([|q|]),new String([|char 92;q|])) in let Quine s=String.Format("let s={0}{1}{2} in printf {3}%s%s{4} (R(Quine s)) (R s)",[|box q;box(Q s);box q;box q;box q|]) in let s="open System;let R(s:String)=new System.String(s|>List.of_seq|>List.rev|>Array.of_list) in let q=char 34 in let Q(s:String)=s.Replace(new String([|q|]),new String([|char 92;q|])) in let Quine s=String.Format(\"let s={0}{1}{2} in printf {3}%s%s{4} (R(Quine s)) (R s)\",[|box q;box(Q s);box q;box q;box q|]) in " in printf "%s%s" (R(Quine s)) (R s)

Inserting line breaks (that break the program, but make it more readable here):

open System;
let R(s:String)=new System.String(s|>List.of_seq|>List.rev|>Array.of_list) in 
let q=char 34 in 
let Q(s:String)=s.Replace(new String([|q|]),new String([|char 92;q|])) in 
let Quine s=String.Format("let s={0}{1}{2} in printf {3}%s%s{4} (R(Quine s)) (R s)",
    [|box q;box(Q s);box q;box q;box q|]) in 
let s="open System;
       let R(s:String)=new System.String(s|>List.of_seq|>List.rev|>Array.of_list) in 
       let q=char 34 in 
       let Q(s:String)=s.Replace(new String([|q|]),new String([|char 92;q|])) in 
       let Quine s=String.Format(\"let s={0}{1}{2} in printf {3}%s%s{4} (R(Quine s)) (R s)\",
           [|box q;box(Q s);box q;box q;box q|]) in " in
printf "%s%s" (R(Quine s)) (R s)
面犯桃花 2024-08-10 03:36:47

在 C 语言中,217 个字符:

a="};)01(rahctup;)--p*(rahctup);p*;43=p*(rof;)a(ftnirp;))a,b=p(tacrts(nelrts=+p{)p*rahc(niam;}7393422{=]99[b;";b[99]={2243937};main(char*p){p+=strlen(strcat(p=b,a));printf(a);for(*p=34;*p;)putchar(*p--);putchar(10);}

In C , 217 chars :

a="};)01(rahctup;)--p*(rahctup);p*;43=p*(rof;)a(ftnirp;))a,b=p(tacrts(nelrts=+p{)p*rahc(niam;}7393422{=]99[b;";b[99]={2243937};main(char*p){p+=strlen(strcat(p=b,a));printf(a);for(*p=34;*p;)putchar(*p--);putchar(10);}
梨涡 2024-08-10 03:36:47

Javascript - IE/Chrome/FF

(function(){alert(("("+arguments.callee+")()").split("").reverse().join(""))})()

Javascript - IE/Chrome/FF

(function(){alert(("("+arguments.callee+")()").split("").reverse().join(""))})()
盛夏尉蓝 2024-08-10 03:36:47

在 Python 交互式提示符处:

xatnys dilavni :rorrExatnyS

27 个字符。

At the Python interactive prompt:

xatnys dilavni :rorrExatnyS

27 characters.

兰花执着 2024-08-10 03:36:47

C89,119 个字符

不幸的是,这需要使用高度非标准的函数 strrev()

main(){char*a="};)43,)b(verrts,43,a(ftnirp;)a(pudrts=b*,%c%s%c=a*rahc{)(niam",*b=strdup(a);printf(a,34,strrev(b),34);}

C89, 119 characters

Unfortunately, this requires use of the highly non-standard function strrev():

main(){char*a="};)43,)b(verrts,43,a(ftnirp;)a(pudrts=b*,%c%s%c=a*rahc{)(niam",*b=strdup(a);printf(a,34,strrev(b),34);}
篱下浅笙歌 2024-08-10 03:36:47

Bash, 75 chars

好吧,当然一个空的 shell 脚本不会输出任何内容,但是下一个我能想到的最好的事情:

a=\;printf\ \"a=%q%s\"\ \"\$a\"\ \"\$a\"\|rev;printf "a=%q%s" "$a" "$a"|rev

同样的技巧。

Bash, 75 chars

Well, of course an empty shell script will output nothing, but the next best thing I can think of:

a=\;printf\ \"a=%q%s\"\ \"\$a\"\ \"\$a\"\|rev;printf "a=%q%s" "$a" "$a"|rev

Same ol' trick.

青丝拂面 2024-08-10 03:36:47

UNIX shell:

z=\' a='z=\\$z a=$z$a$z\;eval echo \$a\|rev';eval echo $a|rev

我倾向于重复我自己:这些程序应该被称为“Goedels”,因为大多数此类事物背后的想法(在现代)首先由 Kurt Goedel 在证明他的第一个不完备性定理中使用(库尔特·哥德尔的全集 I,第 175 页)。

UNIX shell:

z=\' a='z=\\$z a=$z$a$z\;eval echo \$a\|rev';eval echo $a|rev

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).

私野 2024-08-10 03:36:47

Perl

73 个字符。

#! /opt/perl/bin/perl
seek DATA,0,0;$/=\1;print reverse <DATA>;
__DATA__
​
  • 您必须在末尾添加 __DATA__ 才能开始打开 ​​DATA 文件句柄。
  • $/ 设置为数字的引用,会导致 readline() 一次读取那么多字节。
  • 需要 seek(DATA,0,0) 将指针设置为文件的开头,而不是 __DATA__ 部分的开头。
  • 可以删除或缩短 shebang 行 (#! ...)
  • __DATA__ 后面需要换行符,否则它不是有效的 Perl。

Perl

73 characters.

#! /opt/perl/bin/perl
seek DATA,0,0;$/=\1;print reverse <DATA>;
__DATA__
​
  • You have to have __DATA__ at the end for the DATA file-handle to start out opened.
  • Setting $/ to a reference of a number, causes readline() 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.
  • Could remove, or shorten the shebang line (#! ...)
  • __DATA__ requires a newline after it, or it isn't valid Perl.
半寸时光 2024-08-10 03:36:47

Haskell,79 个字符

a=";main=putStr.reverse$\"a=\"++show a++a";main=putStr.reverse$"a="++show a++a

标准技巧。

Haskell, 79 characters

a=";main=putStr.reverse$\"a=\"++show a++a";main=putStr.reverse$"a="++show a++a

Standard trick.

硬不硬你别怂 2024-08-10 03:36:47

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));}}

大姐,你呐 2024-08-10 03:36:47

PHP 36 字节

<?echo strrev(join(file(__FILE__)));

PHP 36 bytest

<?echo strrev(join(file(__FILE__)));
注定孤独终老 2024-08-10 03:36:47

C89(155 字节)

#define q(k)r(char*s){if(*s)r(s+1);putchar(*s);}main(){r(#k"\nq("#k")\n");}
q(#define q(k)r(char*s){if(*s)r(s+1);putchar(*s);}main(){r(#k"\nq("#k")\n");})

C89 (155 bytes)

#define q(k)r(char*s){if(*s)r(s+1);putchar(*s);}main(){r(#k"\nq("#k")\n");}
q(#define q(k)r(char*s){if(*s)r(s+1);putchar(*s);}main(){r(#k"\nq("#k")\n");})
×眷恋的温暖 2024-08-10 03:36:47

python(34 个字符):

print(open(__file__).read()[::-1])

python (34 chars):

print(open(__file__).read()[::-1])
手心的海 2024-08-10 03:36:47

PHP

Snip - 删除了不正确的答案

现在要赎回自己;

<?php echo strrev(file_get_contents(__FILE__)); ?>

PHP

Snip - removed incorrect answer

And now to redeem myself;

<?php echo strrev(file_get_contents(__FILE__)); ?>
無心 2024-08-10 03:36:47

JavaScript:您想要多少个字符?
第二个最简单的有效程序(使用我的方法):240 个字符! (包括空格但不包括换行)

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script>
var string = "<!DOCTYPE HTML>\n<html>\n" + document.documentElement.innerHTML + "\n</html>";
var string2 = string.split("").reverse().join("");
document.write(string2);
</script>
</body>
</html>

JavaScript: How many characters do you want?
SECOND SIMPLEST VALID PROGRAM (using my method): 240 Chars! (including spaces but not new lines)

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script>
var string = "<!DOCTYPE HTML>\n<html>\n" + document.documentElement.innerHTML + "\n</html>";
var string2 = string.split("").reverse().join("");
document.write(string2);
</script>
</body>
</html>
你的他你的她 2024-08-10 03:36:47

C/C++:

#include<stdio.h>
int main() {
    char c;
    freopen("thisfilename.c","r",stdin); //or thisfilename.cpp
    while(scanf("%c",&c)!=-1)
        printf("%c",c);
return 0; }

C/C++:

#include<stdio.h>
int main() {
    char c;
    freopen("thisfilename.c","r",stdin); //or thisfilename.cpp
    while(scanf("%c",&c)!=-1)
        printf("%c",c);
return 0; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文