HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
Hey all! Well now that the "Signature" and "Avatar" contests are over, I thought that I would propose a new challenge, and this one's in the form of a programming challenge. A quine is a program that presents an interesting problem -- basically it's a program that needs to output its own source code (no, you can't just read your original source file). Basically, if you wrote the program in C, the program should output things like:
(note, the above code certainly wouldn't work, but hopefully that illustrates some of the challenge to you). I have always found quines to be an interesting mental problem, and recently I wrote my own in C# (I'll post it sometime soon if enough people are interested, but I don't want to give my method away too early). I'd love to see people write quines in their particular language-of-choice -- whether it be C++, Python, Perl, C#, PHP, or any other one that you like to use. FYI, mine took me about 5 hours to write -- a Friday evening, then I woke up on Saturday and continued coding. Jennifer was a little puzzled when I was so excited that I woke her up on Saturday with excitement that I finally had gotten it to work perfectly right. It's not hard to get the source close, but to get it *exact* is trickier. Anyway, I hope you all find this as interesting and challenging as I did! In Christ, ------------------ |
||
Brandon![]() Member Posts: 594 From: Kansas City, Mo, USA Registered: 02-02-2004 |
Wow, I have never even thought of such a thing... you have my interest now though. Hmmmm.... ------------------ |
||
Klumsy![]() Administrator Posts: 1061 From: Port Angeles, WA, USA Registered: 10-25-2001 |
this maybe cheating but in c64 basic you could do 30 List ------------------ |
||
Jari![]() Member Posts: 1471 From: Helsinki, Finland Registered: 03-11-2005 |
This is interesting. I started working on one in c++ but don't know if I can figure it out. It would be really cool to see how you did it Clint? ------------------ |
||
Jari![]() Member Posts: 1471 From: Helsinki, Finland Registered: 03-11-2005 |
Wow I think I got it... I used defines which made it pretty easy, is that allowed? I'll post the code later so that I don't spoil any one's fun... ------------------ |
||
CPUFreak91![]() Member Posts: 2337 From: Registered: 02-01-2005 |
quote: Is that it? What else does it do? ------------------ |
||
Jari![]() Member Posts: 1471 From: Helsinki, Finland Registered: 03-11-2005 |
quote: It's not simple as it sounds because it must print the source code which includes the code that prints... ------------------ |
||
HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
quote:
Let's hold off on posting any of our solutions until say... tomorrow or the day after? I'll post my solution this Friday. What would the "30 LIST" do? Cheers! --clint ------------------ |
||
Jari![]() Member Posts: 1471 From: Helsinki, Finland Registered: 03-11-2005 |
quote: OK, I'll be posting after you in case I got the idea wrong. ------------------ |
||
HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
Basically, you should be able to run your program, take the output of the program, put it into a file, then compile that file, run that new program, and repeat ad-infinitum, with the output of the program always being the same. Some people make it simpler on themselves by having the whole program be on a single line (easy to do in C++, not so easy in VB), but I chose to have my program keep its formatting (complete with tabs and newlines and whatnot). Cheers! --clint ------------------ |
||
Jari![]() Member Posts: 1471 From: Helsinki, Finland Registered: 03-11-2005 |
quote: OK then I got.
quote: I didn't bother to keep spaces and tabs the same, just line changes. ------------------ |
||
HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
Excellent! I look forward to seeing your solution, Jari! ![]() I didn't come up with my solution entirely on my own (kindof wish I had), but I viewed at least one quine solution to get a basic idea of how one might do it, and then I took that idea and made my own. --clint ------------------ |
||
crazyishone![]() Member Posts: 1685 From: Registered: 08-25-2004 |
interesting.... it seems that if you dont do it right, the program should perpetually execute.. is this true? maybe not, because what it prints needs to be compiled. I respect those of you with the knowledge to do this stuff. ------------------ |
||
HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
Depending on how you write it, I suppose you could make it have an infinite loop. I didn't use any loops in the version that I wrote though (and I don't think Jari did either). --clint ------------------ |
||
Klumsy![]() Administrator Posts: 1061 From: Port Angeles, WA, USA Registered: 10-25-2001 |
quote: 30 is the line number and list is a basic command to list its sourcecode.. so 30 list would just show 30 List 20 a = 5 so basic is a very quine friendly language. ------------------ |
||
firemaker103![]() Member Posts: 643 From: Registered: 07-13-2005 |
you're confusing me here. ![]() So, I think what you are saying is that you have your lanugage run it's own language, like iterpret it to run? ------------------ |
||
HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
@Firemaker: No, the program doesn't need to compile and run itself -- it just needs to output a copy of itself on the standard output. And Klumsy, yes, it appears that Basic is a very Quine-friendly language, but while it would accomplish the task, I think it's breaking the rules of "not allowed to read its own source file". --clint ------------------ |
||
CPUFreak91![]() Member Posts: 2337 From: Registered: 02-01-2005 |
So is this correct? (Python)
------------------ [This message has been edited by CPUFreak91 (edited October 12, 2005).] [This message has been edited by CPUFreak91 (edited October 13, 2005).] |
||
crazyishone![]() Member Posts: 1685 From: Registered: 08-25-2004 |
hmm...python seems to bare similarities to HTML. perhaps only skin-deep simliarities? ------------------ |
||
Realm Master![]() Member Posts: 1971 From: USA Registered: 05-15-2005 |
or maybe... maaayybbeee.... Yeah, that DOES bear a strking resemblance to HTML (Which none of these blasted forums seem to use!! i HATE UBB, learn HTML and then you hafta switch! grr.)... but i think its only skin deep... i mean, its Java that does all the cool stuff on the web sites! ------------------ Blessed are those who suffer for doing what is right. PM ME YOUR DESCRIPTION OF ME! ILL PUT IT HERE! |
||
crazyishone![]() Member Posts: 1685 From: Registered: 08-25-2004 |
it seems like the tags are very similar. some of the specific tags are the same, as well as some attributes...such as the fact that they open and close. ------------------ |
||
Klumsy![]() Administrator Posts: 1061 From: Port Angeles, WA, USA Registered: 10-25-2001 |
its not like HTML, its just he tried to use HTML to quote the python but it showed up as text instead ![]() ------------------ |
||
crazyishone![]() Member Posts: 1685 From: Registered: 08-25-2004 |
o, i see. well, i kinda feel dumb now. by the way, why isnt html allowed in the posts? I can imagine some repercussions, i suppose...but i dont think anybody here would do anything malicious. UBB works just fine for forums, so im not complaining. Just wondering. ------------------ |
||
HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
quote: To quote code, you can use: a [ code ] tag.
Okay, I don't know Python, but what is the output of that program? It looks like it would be:
It would still need the final line in order for it to be correct -- the output needs to be exactly the same as the source file -- though I think you're definitely on the right track! I may be wrong though -- you might have gotten it right and I just don't know Python well enough. --clint ------------------ |
||
firemaker103![]() Member Posts: 643 From: Registered: 07-13-2005 |
nvm. (....) ------------------ [This message has been edited by firemaker103 (edited October 13, 2005).] |
||
CPUFreak91![]() Member Posts: 2337 From: Registered: 02-01-2005 |
quote: Nope. Not at all. I can't get the white box thingy to work. Python is used a lot on the web, you just never notice it. I don't think anyone's done fancy internet stuff though like with Java. Ppl prefer the power of python for mySQL databases and such. ------------------ |
||
CPUFreak91![]() Member Posts: 2337 From: Registered: 02-01-2005 |
This might fix some questions and mistakes about quines: http://www.madore.org/~david/computers/quine.html ------------------ |
||
HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
Wow, great link, thanks CPU! That read was way over my head at parts (I feel like since I left college, my brain has notched down quite a bit), but I really liked this quote from the end: quote: That one made me laugh out loud. --clint ------------------ |
||
CPUFreak91![]() Member Posts: 2337 From: Registered: 02-01-2005 |
Here's another page: http://www.nyx.net/~gthompso/quine.htm ------------------ |
||
Jari![]() Member Posts: 1471 From: Helsinki, Finland Registered: 03-11-2005 |
Those code's in the sites which CPUFreak linked are cleverly simple. Mine is originally bloated. ![]() ------------------ |
||
CPUFreak91![]() Member Posts: 2337 From: Registered: 02-01-2005 |
quote: I don't really understand them cause of being in C ------------------ |
||
vincent![]() Member Posts: 129 From: Amersfoort, the Netherlands Registered: 12-23-2002 |
here is mine, in Java:
|
||
Jari![]() Member Posts: 1471 From: Helsinki, Finland Registered: 03-11-2005 |
Well here's my so called quine code which probably doesn't fit in any standards:
------------------ |
||
Jari![]() Member Posts: 1471 From: Helsinki, Finland Registered: 03-11-2005 |
That's cool Vincent! Heh we posted almost at the same time. ------------------ |
||
vincent![]() Member Posts: 129 From: Amersfoort, the Netherlands Registered: 12-23-2002 |
quote: Ya |
||
CPUFreak91![]() Member Posts: 2337 From: Registered: 02-01-2005 |
quote: Hey! I might be able to work off of this! As I understand Java much better than C. Still though... back to studying Quines. ------------------ |
||
CPUFreak91![]() Member Posts: 2337 From: Registered: 02-01-2005 |
quote:
quote: I don't mean to discourage you, but...
I think you have to print the as well! Did you see this? ------------------ |
||
Jari![]() Member Posts: 1471 From: Helsinki, Finland Registered: 03-11-2005 |
CPUFreak91, sorry I don't see what's wrong with it... ------------------ |
||
HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
Wow, great job with the quines, everyone! Here's my Quine, pardon the mess! Mine's not formatted nearly as nicely as Vincent's is:
A lot of that mess is just to do the formatting for the newlines and the tabs, but when you run it, it comes out looking all tabbed and nice and such. Again, great job, Vincent and Jari! Way to pull it off quickly! --clint ------------------ |
||
crazyishone![]() Member Posts: 1685 From: Registered: 08-25-2004 |
so...this is a competition... how will the winner be decided? ------------------ |
||
HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
It wasn't really a competition per-say, it was more of a "challenge" -- just seeing who can step up to the plate and complete a specific programming task. Though it would be fun to have a computer programming competition or two at some point -- that could be neat (I think we should hold off on one of those until after the two-month-game project is done -- I wouldn't want to interfere with that) --clint ------------------ |
||
CPUFreak91![]() Member Posts: 2337 From: Registered: 02-01-2005 |
quote: I don't think you've printed the code for printing the code (like the #include and stuff with the h(...))! ------------------ |
||
Jari![]() Member Posts: 1471 From: Helsinki, Finland Registered: 03-11-2005 |
quote: Oh it's all there... you can compile and run the code and then copy and paste the output from console and compile it again. ------------------ |
||
Jari![]() Member Posts: 1471 From: Helsinki, Finland Registered: 03-11-2005 |
quote: Well done! ------------------ |
||
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
Is this good, or does it count as just reading the original source file: ![]()
Throw a system("cc quine.c -o quine.exe; quine.exe"); in there, and things could get interesting.... |
||
HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
Brian, that generally doesn't work, no -- I'm not familiar with those command line options you put there, but I assume that you're talking about if the program is in a different directory than the binary? (or if you are on Linux, which uses cat instead of type... ![]() Nice try though! One good one is a completely blank file -- in many languages, that will compile, and it will run, and it will also have no output (which is the same as the output). That is also considered "unfair" and "I'm not going to fall for it again". ------------------ |
||
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
The command line stuff (cc quine.c -o quine.exe) will compile (cc) quine.c and output (-o) quine.exe. That line would be different depending on your compiler, of course. What it would do in theory is not only output its own source code, but then also compile and run it, which would again output the source code, compile it again, run it again, .... or is that just demented? |
||
HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
Whoa -- trippy! ![]() That's pretty cool. ------------------ |
||
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
You could go further, and pass an argument to your program, like "1" (without the quotes, and have it output quine1.c and quine1.exe, then have it call "quine1.exe 2" to generate quine2.c and quine2.exe, and so on - run it once, and fill up your harddrive. ![]() ![]() Or how about this: get a back-and-forth quine going. Have program A output the source code to program B, then compile and run it. Program B outputs the source to program A, then compiles and runs it. And every 1,000,000th iteration, have it send your friends an email telling them how much of a geek you are. |
||
Seven7 Member Posts: 50 From: USA Registered: 03-16-2005 |
I had this problem come up in a graduate class several years ago. The problem which was proposed to us in class was very similar to HanClinto proposal. (HanClinto's proposal is slightly different from the 'classical self-replicating logic' that I had in grad school) The 'spirit' of the problem has no solution since its has an inherited HanClinto hit the nail on the head; it is "ad-infinitum". The very simple Of course I am drawing from the fact that HanClinto's proposal is
quote: Boy that takes me back to grad. school days when I would be working on some problem 5:00am and needing to get some sleep only to wake up 3 hours later working on the same problem. Once I would find a solution, I'd run through the dorms shouting! I found you comment funny. A great and interesting problem HanClinto!! [This message has been edited by seven7 (edited October 19, 2005).] |
||
HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
Thanks for the feedback, Seven7! ![]() ![]() I didn't understand everything about the problem that you were proposing -- are you talking about a quine that is not self-referencing? I'm pretty sure that's impossible, in order to make a quine, you have to have some sort of self-referencing statement. Is that what you're talking about, or did I go off in left field? --clint ------------------ |
||
Seven7 Member Posts: 50 From: USA Registered: 03-16-2005 |
HanClinto, Yea, working into the early mornings with a cup of coffee in one hand and Your absolutely right about "[none] self-referencing" code which is clearly
|
||
Jachin![]() Member Posts: 113 From: Independence, OR, USA Registered: 01-03-2003 |
Does this bring up any metaphysical aguments like nothing can be created only "copied?" |
||
Jachin![]() Member Posts: 113 From: Independence, OR, USA Registered: 01-03-2003 |
quote: Hi y'all
The price of no self reverence meens that it only works once in a great great while. [This message has been edited by jachin (edited November 14, 2005).] |
||
Jachin![]() Member Posts: 113 From: Independence, OR, USA Registered: 01-03-2003 |
How does this relate to bootstrapping a compiler? | ||
HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
Hrm, interesting thought, Jachin! (about the random length string) What are the odds that would work, anyways? Cheers! --clint |
||
Jachin![]() Member Posts: 113 From: Independence, OR, USA Registered: 01-03-2003 |
quote: It depends on your character-set assuming an extended asci character set with 256 characters.
[This message has been edited by jachin (edited November 15, 2005).] |