写一段代码,这段代码就是打印这段代码,谁来试试☻

2016-05-20 16:39:37 +08:00
 dikT

如题

4737 次点击
所在节点    编程
29 条回复
hanai
2016-05-20 16:42:30 +08:00
function funcX() {console.log(arguments.callee.toString())}
SakuraSa
2016-05-20 16:46:59 +08:00
sinalvee
2016-05-20 16:52:34 +08:00
function func() {
console.log(func);
}
yangg
2016-05-20 17:00:03 +08:00
funcX, func 的都不是吧,你们都没调用。
@hanai
@sinalvee
6IbA2bj5ip3tK49j
2016-05-20 17:07:50 +08:00
定位到本文件,读出来,打印……😂
cdwyd
2016-05-20 17:08:28 +08:00
读取源码,然后打印出来?
innoink
2016-05-20 17:10:20 +08:00
以前看过好几个 c 语言版本的,看之前还以为 c 语言做不到。。
SakuraSa
2016-05-20 17:11:09 +08:00
作为 py 使用者表示, py 写 Quine 还是比较简单的
```python
s = 's = %r\nprint(s%%s)'
print(s%s)
```
sinalvee
2016-05-20 17:12:38 +08:00
@yangg 好吧,逼我
(function func() {
console.log('(' + func + ')();');
})();
kindjeff
2016-05-20 17:13:40 +08:00
function a() { console.log(a.toString(), "a()"); } a()
GtDzx
2016-05-20 17:15:50 +08:00
@innoink 任何一个图灵完全的语言能能写出无限多种 quine
Troevil
2016-05-20 17:16:56 +08:00
@sinalvee 自我补全 lol
lovedebug
2016-05-20 17:18:32 +08:00
eval('var a = arguments.callee.toString();console.log(a.slice(a.indexOf("{") + 1, a.lastIndexOf("}")));');
congeec
2016-05-20 17:20:09 +08:00
``` C
char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";main(){printf(s,34,s,34);}
```
blahgeek
2016-05-20 17:25:30 +08:00
从备忘录里面找出来的:

Quine: Python & Brainfuck

```python
s='s=print s[:2]+chr(39)+s+chr(39)+chr(10)+s[2:]'
print s[:2]+chr(39)+s+chr(39)+chr(10)+s[2:]
```

So … the key is … 把那些在字符串中表达方式不一样的字符用其他方式代替(`”’”`, `”\n ”`)

另外…用`repr`(`%r`)算作弊吧……= =

```python
s='s={};print s.format(repr(s))';print s.format(repr(s))
```

更加 general 的方法:

```python
import sys
w = sys.stdout.write
s = [105,109,112,111,114,116,32,115,121,115,10,119,32,61,32,115,121,115,46,115,116,100,111,117,116,46,119,114,105,116,101,10,115,32,61,32,91,10,119,40,39,39,46,106,111,105,110,40,109,97,112,40,99,104,114,44,32,115,91,58,51,55,93,41,41,41,10,119,40,39,44,39,46,106,111,105,110,40,109,97,112,40,115,116,114,44,32,115,41,41,41,10,119,40,39,39,46,106,111,105,110,40,109,97,112,40,99,104,114,44,32,115,91,51,55,58,93,41,41,41,10
w(''.join(map(chr, s[:37])))
w(','.join(map(str, s)))
w(''.join(map(chr, s[37:])))
```

And … Brainf**k! (use `cat quine.bf | sed 's/#.*//' | tr '\n' ' ' | sed 's/ //g ’`to remove comments and white spaces)

```
# Character map (according to frequency):
# + > < [ ] - .
# 1 2 3 4 5 6 7
# 43 62 60 91 93 45 46

# start
>>>

# data
>++
>++
>++

>++++
>+++
>+++++
>++
>++++
>++++
>+++
>+
>+++
>+
>++
>++
>++++++
>+++++
>+++
>++++
>++
>+
>+++
>++++++
>+++++
>++
>++++
>++
>+++++
>++
>++++
>++
>+++++
>+
>++++
>+++
>+++++
>+++
>++++
>+++
>+++++
>++
>++++++
>++++
>++++
>++
>+++++
>++
>++++
>++
>+++++
>+++
>+
>++++
>+++
>+++++
>+++
>++++
>+++
>+++++
>++
>++++++
>+++++
>++
>+++++
>+++
>+++
>++++++
>++
>++
>++
>++++
>++++
>++
>+++++
>++
>++++
>++
>+++++
>++
>+
>+
>+
>+
>+
>+
>+
>+
>++++
>+++
>+
>+
>+
>+
>+
>++
>++++++
>+++++
>+++
>+
>+
>+
>++++
>+++
>+++++
>+++
>++++
>+++
>+++++
>++
>++++++
>++++
>++++
>++
>+++++
>++
>++++
>++
>+++++
>+++
>++
>+
>+
>+
>+
>++++
>+++
>+
>+
>+
>+
>++
>++++++
>+++++
>+++
>+
>+
>+
>++++
>+++
>+++++
>+++
>++++
>+++
>+++++
>++
>++++++
>++++
>++++
>++
>+++++
>++
>++++
>++
>+++++
>+++
>++++++
>++++++
>++++
>+++
>+++++
>+++
>++++
>+++
>+++++
>++
>++++++
>++++
>++++
>++
>+++++
>++
>++++
>++
>+++++
>+++
>++
>+
>+
>+
>+
>+
>++++
>+++
>+
>+
>+
>+
>+
>+
>++
>++++++
>+++++
>+++
>+
>++++
>+++
>+++++
>+++
>++++
>+++
>+++++
>++
>++++++
>++++
>++++
>++
>+++++
>++
>++++
>++
>+++++
>+++
>+
>+
>++++
>+++
>+++++
>+++
>++++
>+++
>+++++
>++
>++++++
>++++
>++++
>++
>+++++
>++
>++++
>++
>+++++
>+++
>++
>+
>+
>+
>+
>+
>+
>+
>++++
>+++
>++++++
>++++++
>++++++
>++++++
>++++++
>++++++
>++++++
>++
>++++++
>+++++
>+++
>+
>++++
>+++
>+++++
>+++
>++++
>+++
>+++++
>++
>++++++
>++++
>++++
>++
>+++++
>++
>++++
>++
>+++++
>+++
>+
>++++
>+++
>+++++
>+++
>++++
>+++
>+++++
>++
>++++++
>+++++
>+++++
>+++++
>+++++
>+++++
>+++++
>++
>+++++
>+
>+
>+
>++++
>++
>++++
>+++
>+++
>+
>++
>++
>++++++
>+++++
>+++
>++++
>++
>+
>+++
>++++++
>+++++
>+++
>+++++++
>++
>++
>++++++
>+++++
>+++
>++++
>+++
>+++++
>++++++
>+++
>+
>++++
>+++
>+
>+++++
>+++
>++++
>+++
>+++++
>++
>++++
>+++
>+++
>+
>+
>+
>+
>+
>+
>+
>+
>++++
>++
>+
>+
>+
>+
>+
>+
>+
>+
>+++
>++++++
>+++++
>++
>++++++
>++++++
>+++++++
>+++
>+
>+
>+
>+
>++++
>++
>++++++
>++++++
>++++++
>++++++
>+++
>++++++
>+++++
>++
>++++++
>++++++
>++++++
>++
>++++
>+++
>+++++++
>++
>++++++
>+++++
>+++
>++++
>++++++
>+++++
>++
>++
>+++++
>+
>++++
>++
>+
>+++++
>++
>++
>++++
>+++++++
>++
>+++++

# return to first char
[<] >

# copy data
[ # for each data
[<+<+>>-] # copy to left two cells
<[>+<-]> # copy back from left cell

[>]>[>]+
[<]<[<]>-
[ # while greater than 0
[>]> [>]< +
[<]<[<]> -
]
>
]

<<->>
>

# Apply character map
[
# go to end of data plus 1; adds up to 43
[>]> [>]
>++++++++ # for range(8)
[<+++++>-] # adds 5
<+++ # adds 3
[<]<[<]> # return to the character

- # minus 1

# if greater than 0
[
# total 19
[>]> [>]<
>++++ # for range(4)
[<++++>-] # adds 4
<+++ # adds 3 (19 totally)
[<]<[<]>
-

[
[>]> [>]< -- # 62 to 60
[<]<[<]>
- # minus 1

[
[>]> [>]<
>+++++ # for range(5)
[<++++++>-] # adds 6
<+ # adds 1 (31 totally)
[<]<[<]>
-
[
[>]> [>]< ++ # adds 2
[<]<[<]> # go back to char
- # minus 1
# 93 to 45
[
[>]> [>]<
>+++++++ # for range(7)
[<------->-] # minus 7
<+ # adds 1 (minus 48 totally)
[<]<[<]>
-
[
[>]> [>]< + # adds 1
[<]<[<]> # go back to char
- # minus 1
]
]
]
]
]
]
> # go to next char
]

# Print first 3 ascii
+++
[
# move the data to left cell
> [<<+>>-] <
# move to right cell
[>+<-]
<.>> # print it
-
]

<[<]

# go to orig data
-<

+[<+]
<[<]>

# Print data (data format)
[
<<++++++++ # for range(8)
[>++++++++<-] # plus 8
>-- # 62
. # ">"
# 62 to 43
<++++ # for range(4)
[>----<-] # minus 4
>--- # 43
>
[<.>-] # print "+"
<[-]> # reset left cell to zero
>
]

+[>+] # go until 255
>>
[.>]
```

aka.

```
>>>>++>++>++>++++>+++>+++++>++>++++>++++>+++>+>+++>+>++>++>++++++>+++++>+++>++++>++>+>+++>++++++>+++++>++>++++>++>+++++>++>++++>++>+++++>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>++++>++++>++>+++++>++>++++>++>+++++>+++>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>+++++>++>+++++>+++>+++>++++++>++>++>++>++++>++++>++>+++++>++>++++>++>+++++>++>+>+>+>+>+>+>+>+>++++>+++>+>+>+>+>+>++>++++++>+++++>+++>+>+>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>++++>++++>++>+++++>++>++++>++>+++++>+++>++>+>+>+>+>++++>+++>+>+>+>+>++>++++++>+++++>+++>+>+>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>++++>++++>++>+++++>++>++++>++>+++++>+++>++++++>++++++>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>++++>++++>++>+++++>++>++++>++>+++++>+++>++>+>+>+>+>+>++++>+++>+>+>+>+>+>+>++>++++++>+++++>+++>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>++++>++++>++>+++++>++>++++>++>+++++>+++>+>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>++++>++++>++>+++++>++>++++>++>+++++>+++>++>+>+>+>+>+>+>+>++++>+++>++++++>++++++>++++++>++++++>++++++>++++++>++++++>++>++++++>+++++>+++>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>++++>++++>++>+++++>++>++++>++>+++++>+++>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>+++++>+++++>+++++>+++++>+++++>+++++>++>+++++>+>+>+>++++>++>++++>+++>+++>+>++>++>++++++>+++++>+++>++++>++>+>+++>++++++>+++++>+++>+++++++>++>++>++++++>+++++>+++>++++>+++>+++++>++++++>+++>+>++++>+++>+>+++++>+++>++++>+++>+++++>++>++++>+++>+++>+>+>+>+>+>+>+>+>++++>++>+>+>+>+>+>+>+>+>+++>++++++>+++++>++>++++++>++++++>+++++++>+++>+>+>+>+>++++>++>++++++>++++++>++++++>++++++>+++>++++++>+++++>++>++++++>++++++>++++++>++>++++>+++>+++++++>++>++++++>+++++>+++>++++>++++++>+++++>++>++>+++++>+>++++>++>+>+++++>++>++>++++>+++++++>++>+++++[<]>[[<+<+>>-]<[>+<-]>[>]>[>]+[<]<[<]>-[[>]>[>]<+[<]<[<]>-]>]<<->>>[[>]>[>]>++++++++[<+++++>-]<+++[<]<[<]>-[[>]>[>]<>++++[<++++>-]<+++[<]<[<]>-[[>]>[>]<--[<]<[<]>-[[>]>[>]<>+++++[<++++++>-]<+[<]<[<]>-[[>]>[>]<++[<]<[<]>-[[>]>[>]<>+++++++[<------->-]<+[<]<[<]>-[[>]>[>]<+[<]<[<]>-]]]]]]>]+++[>[<<+>>-]<[>+<-]<.>>-]<[<]-<+[<+]<[<]>[<<++++++++[>++++++++<-]>--.<++++[>----<-]>--->[<.>-]<[-]>>]+[>+]>>[.>]
```
blahgeek
2016-05-20 17:26:43 +08:00
(好像太长了……求不 block ………
blahgeek
2016-05-20 17:26:43 +08:00
(好像太长了……求不 block ………
oott123
2016-05-20 17:29:59 +08:00
要不怎么说 PHP 是世界上最好的语言呢……
<?php
// 随便写啥
highlight_file(__FILE__);
// 也可以随便写啥

逃(。
blahgeek
2016-05-20 17:34:42 +08:00
不过最短的应该是:

#!/bin/cat
est
2016-05-20 17:38:32 +08:00
@blahgeek 有 python/js 的短么?

>>> 1
1

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/280066

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX