An iterated quine in Python
A Quine is a program that prints its own source code. Writing one is considered a rite of passage for programmers. In his Turing award lecture, “Reflections on trusting trust”, Ken Thompson presents quines in the context of trojan horses hidden in compilers.
Quines are easy to write in Python, and a pretty short one can be written as:
='a=%s;print a%%`a`';print a%`a` a
Iterated quines are a finite sequence of programs each of which prints the source code of the next one. The final prints the source code of the first one. Here is my version that allows an arbitrary length n
of the sequence:
=1,0,'\nprint ("n,p,s=%d,%d,"%(n,(p+1)%n))+repr(s),s'
n,p,sprint ("n,p,s=%d,%d,"%(n,(p+1)%n))+repr(s),s