General Development

Quines - Programming Challenge – HanClinto

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:


#include <stdio.h>
void main(void)
{
printf("#include <stdio.h>\n");
printf("void main(void)\n");
printf("{\n");
printf(" printf(\"#include <stdio.h>\\n\");\n");
....etc
}

(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,
clint

------------------
http://www.includingjudas.com/christiangame.html

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....

------------------
I am not ashamed of the gospel, because it is the power of God for salvation of everyone who believes...

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

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

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?

------------------
It is better to hear the rebuke of the wise, than for a man to hear the song of fools. - Ecc 7:5

[VoHW]

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...

------------------
It is better to hear the rebuke of the wise, than for a man to hear the song of fools. - Ecc 7:5

[VoHW]

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
quote:
Originally posted by HanClinto:

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)

Is that it? What else does it do?

------------------
Linux is best for me and Windows or Mac is best for you. End of age long argument.
--
See The 2 Month Game Blog

Learn How to Use Linux

Jari

Member

Posts: 1471
From: Helsinki, Finland
Registered: 03-11-2005
quote:
Originally posted by CPUFreak91:
Is that it? What else does it do?


It's not simple as it sounds because it must print the source code which includes the code that prints...

------------------
It is better to hear the rebuke of the wise, than for a man to hear the song of fools. - Ecc 7:5

[VoHW]

HanClinto

Administrator

Posts: 1828
From: Indiana
Registered: 10-11-2004
quote:
Originally posted by jari:
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...



Great job, Jari! And yes, I think #defines are perfectly allowed -- it's not something I can use in C#, but hey, use whatever the language gives you, yes?

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

------------------
http://www.includingjudas.com/christiangame.html

Jari

Member

Posts: 1471
From: Helsinki, Finland
Registered: 03-11-2005
quote:
Originally posted by HanClinto:

Let's hold off on posting any of our solutions until say... tomorrow or the day after? I'll post my solution this Friday.

OK, I'll be posting after you in case I got the idea wrong.

------------------
It is better to hear the rebuke of the wise, than for a man to hear the song of fools. - Ecc 7:5

[VoHW]

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

------------------
http://www.includingjudas.com/christiangame.html

Jari

Member

Posts: 1471
From: Helsinki, Finland
Registered: 03-11-2005
quote:
Originally posted by HanClinto:
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.

OK then I got.

quote:
Originally posted by HanClinto:
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).

I didn't bother to keep spaces and tabs the same, just line changes.

------------------
It is better to hear the rebuke of the wise, than for a man to hear the song of fools. - Ecc 7:5

[VoHW]

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

------------------
http://www.includingjudas.com/christiangame.html

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.

------------------
globalrant.tk

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

------------------
http://www.includingjudas.com/christiangame.html

Klumsy

Administrator

Posts: 1061
From: Port Angeles, WA, USA
Registered: 10-25-2001
quote:
What would the "30 LIST" do?

30 is the line number and list is a basic command to list its sourcecode..

so 30 list would just show 30 List
could be 10 or whatever or whatever line number you wish actually you go do

20 a = 5
30 b = a + 10
40 c = b * b
50 List

so basic is a very quine friendly language.


------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

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?

------------------
"Be nice to the nerds because later on, you'll be working for them" - Bill Gates

could make an MMORPG in three lines, given I had a single DLL with the entire thing in it programed in a different language :P

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

------------------
http://www.includingjudas.com/christiangame.html

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
So is this correct? (Python)

correct = "Is this correct?"
print correct
print "correct = \"Is this correct?\"\nprint correct"

------------------
Linux is best for me and Windows or Mac is best for you. End of age long argument.
--
See The 2 Month Game Blog

Learn How to Use Linux

[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?

------------------
globalrant.tk

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!

------------------
(yes, i know im stupid)

Blessed are those who suffer for doing what is right.
The kingdom of hevan bleongs to them.-Matthew 5:10

PM ME YOUR DESCRIPTION OF ME! ILL PUT IT HERE!

Here's all the comments!

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.

------------------
globalrant.tk

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

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

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.

------------------
globalrant.tk

HanClinto

Administrator

Posts: 1828
From: Indiana
Registered: 10-11-2004
quote:
Originally posted by CPUFreak91:
So is this correct? (Python)

To quote code, you can use: a [ code ] tag.


correct = "Is this correct?"
print correct
print "correct = \"Is this correct?\"\nprint correct"

Okay, I don't know Python, but what is the output of that program? It looks like it would be:


correct = "Is this correct?"
print correct

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

------------------
http://www.includingjudas.com/christiangame.html

firemaker103

Member

Posts: 643
From:
Registered: 07-13-2005
nvm. (....)

------------------
"Be nice to the nerds because later on, you'll be working for them" - Bill Gates

[This message has been edited by firemaker103 (edited October 13, 2005).]

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
quote:
Originally posted by crazyishone:
hmm...python seems to bare similarities to HTML. perhaps only skin-deep simliarities?


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.

------------------
Linux is best for me and Windows or Mac is best for you. End of age long argument.
--
See The 2 Month Game Blog

Learn How to Use Linux

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

------------------
Linux is best for me and Windows or Mac is best for you. End of age long argument.
--
See The 2 Month Game Blog

Learn How to Use Linux

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:
Yow! I've just lost the SOURCE CODE for all my QUINE PROGRAMS! What will I DO NOW with just the BINARIES?

That one made me laugh out loud.

--clint

------------------
http://www.includingjudas.com/christiangame.html

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
Here's another page:

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

------------------
Linux is best for me and Windows or Mac is best for you. End of age long argument.
--
See The 2 Month Game Blog

Learn How to Use Linux

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.

------------------
It is better to hear the rebuke of the wise, than for a man to hear the song of fools. - Ecc 7:5

[VoHW]

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
quote:
Originally posted by jari:
Those code's in the sites which CPUFreak linked are cleverly simple.
Mine is originally bloated.


I don't really understand them cause of being in C
Oh well, if I make one I'll be more prepared for the Compute Science course in College that i want to major in.

------------------
Linux is best for me and Windows or Mac is best for you. End of age long argument.
--
See The 2 Month Game Blog

Learn How to Use Linux

vincent

Member

Posts: 129
From: Amersfoort, the Netherlands
Registered: 12-23-2002
here is mine, in Java:

package org.vanbeveren.quine;

public class Quine {

private static final int[] data = {
0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x20, 0x6f, 0x72,
0x67, 0x2e, 0x76, 0x61, 0x6e, 0x62, 0x65, 0x76, 0x65, 0x72,
0x65, 0x6e, 0x2e, 0x71, 0x75, 0x69, 0x6e, 0x65, 0x3b, 0xa,
0xa, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20, 0x63, 0x6c,
0x61, 0x73, 0x73, 0x20, 0x51, 0x75, 0x69, 0x6e, 0x65, 0x20,
0x7b, 0xa, 0x9, 0xa, 0x9, 0x70, 0x72, 0x69, 0x76, 0x61,
0x74, 0x65, 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x20,
0x66, 0x69, 0x6e, 0x61, 0x6c, 0x20, 0x69, 0x6e, 0x74, 0x5b,
0x5d, 0x20, 0x64, 0x61, 0x74, 0x61, 0x20, 0x3d, 0x20, 0x7b,
0xa, 0x0, 0x9, 0x7d, 0x3b, 0xa, 0x9, 0xa, 0x9, 0x70,
0x75, 0x62, 0x6c, 0x69, 0x63, 0x20, 0x73, 0x74, 0x61, 0x74,
0x69, 0x63, 0x20, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x6d, 0x61,
0x69, 0x6e, 0x28, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x5b,
0x5d, 0x20, 0x61, 0x72, 0x67, 0x73, 0x29, 0x20, 0x7b, 0xa,
0x9, 0x9, 0x66, 0x6f, 0x72, 0x20, 0x28, 0x69, 0x6e, 0x74,
0x20, 0x69, 0x20, 0x3d, 0x20, 0x30, 0x3b, 0x20, 0x69, 0x20,
0x3c, 0x20, 0x64, 0x61, 0x74, 0x61, 0x2e, 0x6c, 0x65, 0x6e,
0x67, 0x74, 0x68, 0x3b, 0x20, 0x69, 0x2b, 0x2b, 0x29, 0x20,
0x7b, 0xa, 0x9, 0x9, 0x9, 0x63, 0x68, 0x61, 0x72, 0x20,
0x64, 0x20, 0x3d, 0x20, 0x28, 0x63, 0x68, 0x61, 0x72, 0x29,
0x64, 0x61, 0x74, 0x61, 0x5b, 0x69, 0x5d, 0x3b, 0xa, 0x9,
0x9, 0x9, 0x69, 0x66, 0x20, 0x28, 0x64, 0x20, 0x3d, 0x3d,
0x20, 0x30, 0x29, 0x20, 0x7b, 0xa, 0x9, 0x9, 0x9, 0x9,
0xa, 0x9, 0x9, 0x9, 0x9, 0x66, 0x6f, 0x72, 0x20, 0x28,
0x69, 0x6e, 0x74, 0x20, 0x6a, 0x20, 0x3d, 0x20, 0x30, 0x3b,
0x20, 0x6a, 0x20, 0x3c, 0x20, 0x64, 0x61, 0x74, 0x61, 0x2e,
0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x3b, 0x20, 0x6a, 0x2b,
0x2b, 0x29, 0x20, 0x7b, 0xa, 0x9, 0x9, 0x9, 0x9, 0x9,
0x69, 0x66, 0x20, 0x28, 0x6a, 0x20, 0x25, 0x20, 0x31, 0x30,
0x20, 0x3d, 0x3d, 0x20, 0x30, 0x29, 0x20, 0x7b, 0xa, 0x9,
0x9, 0x9, 0x9, 0x9, 0x9, 0x53, 0x79, 0x73, 0x74, 0x65,
0x6d, 0x2e, 0x6f, 0x75, 0x74, 0x2e, 0x70, 0x72, 0x69, 0x6e,
0x74, 0x28, 0x22, 0x5c, 0x74, 0x5c, 0x74, 0x22, 0x29, 0x3b,
0xa, 0x9, 0x9, 0x9, 0x9, 0x9, 0x7d, 0xa, 0x9, 0x9,
0x9, 0x9, 0x9, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
0x6f, 0x75, 0x74, 0x2e, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x28,
0x22, 0x30, 0x78, 0x22, 0x20, 0x2b, 0x20, 0x49, 0x6e, 0x74,
0x65, 0x67, 0x65, 0x72, 0x2e, 0x74, 0x6f, 0x48, 0x65, 0x78,
0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x28, 0x64, 0x61, 0x74,
0x61, 0x5b, 0x6a, 0x5d, 0x29, 0x29, 0x3b, 0xa, 0x9, 0x9,
0x9, 0x9, 0x9, 0xa, 0x9, 0x9, 0x9, 0x9, 0x9, 0x69,
0x66, 0x20, 0x28, 0x6a, 0x20, 0x2b, 0x20, 0x31, 0x20, 0x21,
0x3d, 0x20, 0x64, 0x61, 0x74, 0x61, 0x2e, 0x6c, 0x65, 0x6e,
0x67, 0x74, 0x68, 0x29, 0x20, 0x7b, 0xa, 0x9, 0x9, 0x9,
0x9, 0x9, 0x9, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
0x6f, 0x75, 0x74, 0x2e, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x28,
0x22, 0x2c, 0x20, 0x22, 0x29, 0x3b, 0xa, 0x9, 0x9, 0x9,
0x9, 0x9, 0x7d, 0xa, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9,
0x9, 0x9, 0x9, 0x9, 0xa, 0x9, 0x9, 0x9, 0x9, 0x9,
0x69, 0x66, 0x20, 0x28, 0x6a, 0x20, 0x25, 0x20, 0x31, 0x30,
0x20, 0x3d, 0x3d, 0x20, 0x39, 0x29, 0x20, 0x7b, 0xa, 0x9,
0x9, 0x9, 0x9, 0x9, 0x9, 0x53, 0x79, 0x73, 0x74, 0x65,
0x6d, 0x2e, 0x6f, 0x75, 0x74, 0x2e, 0x70, 0x72, 0x69, 0x6e,
0x74, 0x6c, 0x6e, 0x28, 0x29, 0x3b, 0xa, 0x9, 0x9, 0x9,
0x9, 0x9, 0x7d, 0xa, 0x9, 0x9, 0x9, 0x9, 0x9, 0xa,
0x9, 0x9, 0x9, 0x9, 0x7d, 0xa, 0x9, 0x9, 0x9, 0x9,
0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x6f, 0x75, 0x74,
0x2e, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x6c, 0x6e, 0x28, 0x29,
0x3b, 0xa, 0x9, 0x9, 0x9, 0x7d, 0x20, 0x65, 0x6c, 0x73,
0x65, 0x20, 0x7b, 0xa, 0x9, 0x9, 0x9, 0x9, 0x53, 0x79,
0x73, 0x74, 0x65, 0x6d, 0x2e, 0x6f, 0x75, 0x74, 0x2e, 0x70,
0x72, 0x69, 0x6e, 0x74, 0x28, 0x64, 0x29, 0x3b, 0xa, 0x9,
0x9, 0x9, 0x7d, 0xa, 0x9, 0x9, 0x7d, 0xa, 0x9, 0x7d,
0xa, 0x9, 0xa, 0x7d, 0xa
};

public static void main(String[] args) {
for (int i = 0; i < data.length; i++) {
char d = (char)data[i];
if (d == 0) {

for (int j = 0; j < data.length; j++) {
if (j % 10 == 0) {
System.out.print("\t\t");
}
System.out.print("0x" + Integer.toHexString(data[j]));

if (j + 1 != data.length) {
System.out.print(", ");
}

if (j % 10 == 9) {
System.out.println();
}

}
System.out.println();
} else {
System.out.print(d);
}
}
}

}


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:


#include <iostream>
#include <list>
#include <string>
using namespace std;

struct hmm { string s;int l; hmm(string s,int l):s(s),l(l){} };
list<hmm> codeList;

#define h(str,l) cout<<str<<flush; for(int i=0;i<l;i++) cout<<endl; codeList.push_back(hmm(str,l));
#define h2() for(list<hmm>::iterator iter = codeList.begin(); iter != codeList.end(); iter++) {cout<<'h'<<'('<<(char)34<<iter->s<<(char)34<<','<<iter->l<<')'<<flush; for(int i=0;i<iter->l;i++) cout<<endl; } cout<<'h'<<'2'<<'('<<')'<<endl;
#define h3(str) cout<<'h'<<'3'<<'('<<(char)34<<str<<(char)34<<')'<<endl<<endl; cout<<str<<flush;

int main(int argc, char *argv[]) {

h("",1)
h("#include <iostream>",1)
h("#include <list>",1)
h("#include <string>",1)
h("using namespace std;",2)
h("struct hmm { string s;int l; hmm(string s,int l):s(s),l(l){} };",1)
h("list<hmm> codeList;",2)
h("#define h(str,l) cout<<str<<flush; for(int i=0;i<l;i++) cout<<endl; codeList.push_back(hmm(str,l));",1)
h("#define h2() for(list<hmm>::iterator iter = codeList.begin(); iter != codeList.end(); iter++) {cout<<'h'<<'('<<(char)34<<iter->s<<(char)34<<','<<iter->l<<')'<<flush; for(int i=0;i<iter->l;i++) cout<<endl; } cout<<'p'<<'('<<')'<<endl;",1)
h("#define h3(str) cout<<'h'<<'3'<<'('<<(char)34<<str<<(char)34<<')'<<endl<<endl; cout<<str<<flush;",2)
h("int main(int argc, char *argv[]) {",2)
h2()
h3("return EXIT_SUCCESS; }")

return EXIT_SUCCESS; }

------------------
It is better to hear the rebuke of the wise, than for a man to hear the song of fools. - Ecc 7:5

[VoHW]

Jari

Member

Posts: 1471
From: Helsinki, Finland
Registered: 03-11-2005
That's cool Vincent!
Heh we posted almost at the same time.

------------------
It is better to hear the rebuke of the wise, than for a man to hear the song of fools. - Ecc 7:5

[VoHW]

vincent

Member

Posts: 129
From: Amersfoort, the Netherlands
Registered: 12-23-2002
quote:
Originally posted by jari:
That's cool Vincent!
Heh we posted almost at the same time.

Ya Java doesn't have a define... could have been handy... I just wrote a program that encoded the source into hex numbers, except for the content of the array.. then it just a matter of reconstruction.

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
quote:
Originally posted by vincent:
here is mine, in Java:

Hey! I might be able to work off of this! As I understand Java much better than C.
I hope to write a python one (with an explanation of what something in python means).

Still though... back to studying Quines.

------------------
Linux is best for me and Windows or Mac is best for you. End of age long argument.
--
See The 2 Month Game Blog

Learn How to Use Linux

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
quote:
Originally posted by jari:
Well here's my so called quine code which probably doesn't fit in any standards:

quote:
Originally posted by jari:
Well here's my so called quine code which probably doesn't fit in any standards

I don't mean to discourage you, but...

h("",1)
h("#include <iostream>",1)
h("#include <list>",1)
h("#include <string>",1)
h("using namespace std;",2)
h("struct hmm { string s;int l; hmm(string s,int l):s(s),l(l){} };",1)
h("list<hmm> codeList;",2)
h("#define h(str,l) cout<<str<<flush; for(int i=0;i<l;i++) cout<<endl; codeList.push_back(hmm(str,l));",1)
h("#define h2() for(list<hmm>::iterator iter = codeList.begin(); iter != codeList.end(); iter++) {cout<<'h'<<'('<<(char)34<<iter->s<<(char)34<<','<<iter->l<<')'<<flush; for(int i=0;i<iter->l;i++) cout<<endl; } cout<<'p'<<'('<<')'<<endl;",1)
h("#define h3(str) cout<<'h'<<'3'<<'('<<(char)34<<str<<(char)34<<')'<<endl<<endl; cout<<str<<flush;",2)
h("int main(int argc, char *argv[]) {",2)
h2()
h3("return EXIT_SUCCESS; }")

I think you have to print the

h("#define h3(str) cout<<'h'<<'3'<<'('<<(char)34<<str
as well!

Did you see this?

------------------
Linux is best for me and Windows or Mac is best for you. End of age long argument.
--
See The 2 Month Game Blog

Learn How to Use Linux

Jari

Member

Posts: 1471
From: Helsinki, Finland
Registered: 03-11-2005
CPUFreak91, sorry I don't see what's wrong with it...

------------------
It is better to hear the rebuke of the wise, than for a man to hear the song of fools. - Ecc 7:5

[VoHW]

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:


public class MyTest {
static string MyString="\tstatic void Main() \n\t{ \n\t\tSystem.Console.WriteLine(\"public class MyTest { \\n\" +\n\t\t\t\t\t\t\t\t\"\\tstatic string MyString=\\\"\" + MyString.Replace(\"\\\\\", \"\\\\\\\\\").Replace(\"\\\"\", \"\\\\\\\"\").Replace(\"\\n\", \"\\\\n\").Replace(\"\\t\", \"\\\\t\") + \"\\\";\\n\" + \n\t\t\t\t\t\t\t\tMyString);\n\t}\n}";
static void Main()
{
System.Console.WriteLine("public class MyTest { \n" +
"\tstatic string MyString=\"" + MyString.Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("\n", "\\n").Replace("\t", "\\t") + "\";\n" +
MyString);
}
}

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

------------------
http://www.includingjudas.com/christiangame.html

crazyishone

Member

Posts: 1685
From:
Registered: 08-25-2004
so...this is a competition...
how will the winner be decided?

------------------
globalrant.tk

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

------------------
http://www.includingjudas.com/christiangame.html

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
quote:
Originally posted by Jari:
CPUFreak91, sorry I don't see what's wrong with it...


I don't think you've printed the code for printing the code (like the #include and stuff with the h(...))!

------------------
Linux is best for me and Windows or Mac is best for you. End of age long argument.
--
See The 2 Month Game Blog

Learn How to Use Linux

Jari

Member

Posts: 1471
From: Helsinki, Finland
Registered: 03-11-2005
quote:
Originally posted by CPUFreak91:
I don't think you've printed the code for printing the code (like the #include and stuff with the h(...))!

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.
The way it works is just unnecessarily complicated

------------------
It is better to hear the rebuke of the wise, than for a man to hear the song of fools. - Ecc 7:5

[VoHW]

Jari

Member

Posts: 1471
From: Helsinki, Finland
Registered: 03-11-2005
quote:
Originally posted by HanClinto:

Here's my Quine, pardon the mess! Mine's not formatted nearly as nicely as Vincent's is:

Well done!

------------------
It is better to hear the rebuke of the wise, than for a man to hear the song of fools. - Ecc 7:5

[VoHW]

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:


// quine.c

#include <process.h>

int main()
{
system("type quine.c");
return 0;
}

Throw a system("cc quine.c -o quine.exe; quine.exe"); in there, and things could get interesting....

------------------
Brian

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".

------------------
http://www.includingjudas.com/christiangame.html

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?

------------------
Brian

HanClinto

Administrator

Posts: 1828
From: Indiana
Registered: 10-11-2004
Whoa -- trippy!

That's pretty cool.

------------------
http://www.includingjudas.com/christiangame.html

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.

------------------
Brian

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
infinite recursive nature to it. ("no solution" here means that the program
will not terminate normally since it reproducing itself in an infinite
time period) Of course you can 'cheat' or mimic the problem, but it doesn’t
solve the basic spirit of the problem, as HanClinto pointed out.

HanClinto hit the nail on the head; it is "ad-infinitum". The very simple
nature of self-replicating code has inherently recursive behavior thus
leading to an infinite loop, as pointed out earlier. The code that I have
seen here, does not address this problem in the classical sense
therefore making any solutions posted here (or elsewhere) invalid.

Of course I am drawing from the fact that HanClinto's proposal is
slightly diffrent from the proposaled question in school addressing
this problem. I don't see anyone addressing the problem I've talked about
above. If I could solve this problem, I'd write my dissertation, hit the
lecture circuit and get that Noble Prize. Anybody want to join me?!

quote:
...then I woke up on Saturday and continued coding...

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'm glad you enjoyed the read, and are able to laugh with me about staying up late coding, falling asleep, and then continue as soon as you wake up as if there hadn't been a pause in the middle.

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

------------------
http://www.includingjudas.com/christiangame.html

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
the mouse in the other with a pencil wedged between the teeth is something
I sometimes still do these days. Its funny how some people get hooked onto
an idea or there 'on a roll' coding to the wee mornings and just cant seem
to break away to get rest. There just is not enough hours in a day is there(?)

Your absolutely right about "[none] self-referencing" code which is clearly
a no-brainer. This is somewhat what I was referencing to when I stated "the spirit of the problem". However your challenge is still not solvable
because of the "ad-infinitum" or its infinite recursive nature. One has to
address this recursive property to prevent one's computer from reaching
crital mass and causing a "China Syndrom". (Poorly thought out joke there!)


j.

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:
Originally posted by seven7:

Your absolutely right about "[none] self-referencing" code which is clearly
a no-brainer. This is somewhat what I was referencing to when I stated "the spirit of the problem". However your challenge is still not solvable

Hi y'all
Here is a piece of code that does not reference itself and has the "potential" to be a Quine.


Print_Random_String_of_Length(33)

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:
Originally posted by HanClinto:

What are the odds that would work, anyways?

It depends on your character-set assuming an extended asci character set with 256 characters.
I think it would be 256 to the 33 power which is a little less than
1 in 29,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
Well, I suppose this seems like it would never happen,
But we could massage things a bit so that the function call was just
one or two characters long! Thus increasing our chances to 1 in
281474976710656.
This is "do-able" on most modern processors with this brute force approch.
With a language like lisp it would be pretty smooth.


[This message has been edited by jachin (edited November 14, 2005).]

[This message has been edited by jachin (edited November 15, 2005).]