Some Self Duplicating Code

Take a look at the python code below. Which segment of code should be replaced in place of the first line( ??????????? \color{#D61F06} {\text{???????????}} ) so that the when the code runs, it prints exactly itself ?

1
2
3
???????????
print "x =", x
for i in x: print i

x = ['print "x =", x', 'for i in x: print i'] x = ['print "x =', x", 'for i in x: print i'] x = ['print x=x','for i in x:print i'] x = "['print "x =", x', 'for i in x: print i']"

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Thaddeus Abiy
Apr 18, 2014

The answer is

1
2
3
x = ['print "x =", x', 'for i in x: print i']
print "x =", x
for i in x: print i

Pieces of code that do this are called Quines. Take a look at the wikipedia page for more.

Also in the 'see also' section of the page there is another interesting page on Tupper's self-referential formula

Tupper Tupper

which graphs itself when plotted

tupp tupp

I suck at proggraming any good websites to learn computer science

Mardokay Mosazghi - 7 years, 1 month ago

Log in to reply

Hey I know some cool sites which may help you :

http://www.codecademy.com/learn { best and the most user friendly site : recommended } http://www.cppforschool.com/
http://www.learnjavaonline.org/
http://www.javabeginner.com/

Sushant Samuel - 7 years, 1 month ago

You can try thenewboston on youtube if you are a visual guy,but I am not a fan of videos. Personally I learnt python here back when I was your age.Once you learn a few of the basics,just do problems ,try projecteuler most of the problems are really hard but you will learn a lot.

I am assuming you do not know how to program.

Thaddeus Abiy - 7 years, 1 month ago

There is also : http://codecombat.com

A very fun way to learn.

Vitor da Silva - 7 years, 1 month ago

one word -quine

Daniel Lim - 7 years, 1 month ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...