免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1628 | 回复: 0
打印 上一主题 下一主题

Shortest Python Quine? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-17 06:48 |只看该作者 |倒序浏览

Shortest Python Quine?
by Sean B. Palmer
This morning I scribbled down, pen on paper, a Python
Quine
. I hadn't actively looked into Quine techniques before; it was nice to start from scratch. Here's what I got, with a little reduction when I typed it up (my scribbled version had used a function instead of lambda):-
print (lambda s:s+`s`+')')("print (lambda s:s+`s`+')')(")
At this point, I went searching for the shortest Python Quine that anyone had come up with. I found
Seth Schoen's challenge
again, though it doesn't actually contain the code for the Quine that he came up with. Searching elsewhere, I found a couple of sites (
The Quines List
,
The Quine Page
) that had basically the same Quine (one credited to Manus Hand, the other to Frank Stajano):-
_='_=%s;print _%%`_`';print _%`_`
It was shorter than mine (since it assigns first and then prints), but I noticed that it could easily be made shorter still. So here I present what might just be the shortest possible non-empty Python Quine, at thirty characters:-
_='_=%r;print _%%_';print _%_
It seems odd that the original authors didn't use %r in the first place (perhaps they were going by a version of Python that didn't have %r; when was it added?). For total authenticity, you should include a line break after it. Putting a comma after each %_ would stop the line break, but make it two characters longer, and therefore longer overall by one character.
As for my print-first approach, I managed to shorten it to the following:-
print (lambda s='print (lambda s=%r:s%%s)()':s%s)()
Which is fairly respectible, at fifty-two characters.
I wonder what the shortest possible multi-quine is? (Check out David Madore's
investigation into Quines
for a definition.) Also, I wonder if it's possible to prove that a Quine in a given langauge is the shortest possible for that language, other than by using the evidence that no-one has come up with a shorter one yet? Feedback most welcome.


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8780/showart_185867.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP