• Skip to primary navigation
  • Skip to main content

Algorithm.co.il

  • About
  • Best Posts
  • Origami
  • Older Projects

A classic programming challenge, in Python

Posted on 2007-12-26 by lorg 6 Comments

It has become a tradition for computer scientists to create various self referential ‘strange loops’. Traditions such as writing a compiler in the language it compiles are actually quite useful – and also very interesting. This tradition also branched to another one (also mentioned in the linked article) of writing programs that output their own source (without disk access and other dirty tricks).

The challenge is obviously to write such a program in Python, in as few lines as possible. Here is my solution, which is at two lines. I urge you to try it for yourself before looking, it is a very educating challenge. I’ll be very much interested in seeing a one-liner for this problem, or a proof that such a one-liner does not exist.

If you are interested in the bigger challenge, of writing an interpreter for Python in Python itself, go check out PyPy first.

For those interested in other ‘strange loops’, find a copy of ‘Godel Escher Bach’. If you happen to live in Israel, and can come to Haifa, I might even lend you my copy (once I get it back :)

Filed under: Challenges, computer science, Programming Philosophy, Python

Reader Interactions

Comments

  1. Adam Morrison says

    2007-12-26 at 2:09 pm

    Imri, I think you need to define one-liner more precisely.
    But anyway the solution will depend on the lanaguage.
    For C you I’m quite sure you can prove impossibility by case
    analysis of all I/O functions.

    But you can “cheat”, e.g. take Python++, where the string class has the
    following method:

    def foo(self):
    return self + ‘ % “‘ + self + “.foo()”

    >>> print ‘%s’ % “print ‘%s'”.foo()
    print ‘%s’ % “print ‘%s'”.foo()

    Reply
  2. lorg says

    2007-12-26 at 3:05 pm

    I agree, the solution obviously depends on the language. I was really referring to regular Python, were a one-liner is just one line of python code – in this case probably a print statement.

    With cheating you can achieve the following (which I think were discussed in literature):
    1. The empty program, if legal in your language of choice, is a valid solution (if you consider it a one-liner and not a zero-liner), as it prints a copy of itself: nothing at all.
    2. You can write a new language in which the interpreter prints the source of the program before running it, or a compiler that adds a part that prints the source of the program when run. In both cases, any program may be easily considered self-printing. (The second case is very reminiscent of the article I linked to, “Reflections on Trusting Trust”).

    Reply
  3. Erez says

    2007-12-26 at 4:28 pm

    Here is my one-liner:

    print [s % repr(s) for s in [‘print [s %% repr(s) for s in [%s]][0]’]][0]

    Though I think I once wrote a better one. I’ll try to dig it up.

    Reply
  4. lorg says

    2007-12-27 at 1:40 pm

    A really good idea using list comprehension. Excellent!

    Reply
  5. Matteo says

    2008-01-09 at 4:58 am

    You mean quines?

    (couldn’t read the Bell Labs page, the server is down ATM…)

    Here’s a C one liner:

    int main() { char *s = “int main() { char *s = %c%s%c; printf(s, 34, s, 34); }”; printf(s, 34, s, 34); }

    (couldn’t try that, I’m on windows at the moment and it complains about missing definition for printf, I know gcc only spits a warning normally…)

    I found it at:

    http://en.wikipedia.org/wiki/Quine_%28computing%29

    See also:

    http://www.nyx.net/~gthompso/quine.htm

    Cheers,
    Matteo

    Reply
  6. lorg says

    2008-01-09 at 5:06 am

    I was talking about a one-liner in Python, but yes, I meant quines.
    The article was the well-known “Reflections on Trusting Trust” by Ken Thompson.

    Reply

Leave a Reply Cancel reply

© 2026 Algorithm.co.il - Algorithms, for the heck of it