5

Python Quines

 2 years ago
source link: https://z-rui.github.io/post/2015/08/python-quines/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python Quines

Sun Aug 30, 2015

Quine指的是一种特殊的程序,其输出内容等于其自身代码。想写出这样的程序需要动一番脑筋。

如下代码即为Python的quine:

s='s=%s;print(s%%repr(s))';print(s%repr(s))

Berkeley的CS 61A课程作业中有一个challenge problem是用Python写一个quine程序,并且,不能使用字符串的%运算符,相对而言更困难一些。

答案如下:

s='s="s="+repr(s)+";"+s;print(s)';s="s="+repr(s)+";"+s;print(s)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK