frisk@rhi.hi.is (Fridrik Skulason) (03/16/89)
I am looking for some silly but difficult problems for use in a contest. One example is the following: "write a one line C program that produces the source code to itself when run." Any suggestions ? -- Fridrik Skulason University of Iceland UUCP frisk@rhi.uucp BIX frisk Guvf yvar vagragvbanyyl yrsg oynax .................
maart@cs.vu.nl (Maarten Litmaath) (03/18/89)
frisk@rhi.hi.is (Fridrik Skulason) writes:
\ "write a one line C program that produces the source code
\ to itself when run."
Spoilers:
Actually a 2 liner (doesn't exceed 80 character limit):
----------8<----------8<----------8<----------8<----------8<----------
char*p="char*p=%c%s%c%c;main(){printf(p,34,p,34,10,10);}%c"
;main(){printf(p,34,p,34,10,10);}
----------8<----------8<----------8<----------8<----------8<----------
A true 1 liner (join the 2 lines):
----------8<----------8<----------8<----------8<----------8<----------
char*p="char*p=%c%s%c;main(){printf(p,34,p,34,10);}%c";main(){printf(p,34,p,34,
10);}
----------8<----------8<----------8<----------8<----------8<----------
The program below won't print a terminating newline, so the source shouldn't
contain '\n'. (Is this OK with ANSI?)
----------8<----------8<----------8<----------8<----------8<----------
char*p="char*p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
----------8<----------8<----------8<----------8<----------8<----------
A cheat.
----------8<----------8<----------8<----------8<----------8<----------
main(){execlp("cat","",__FILE__,(char*)0);}
----------8<----------8<----------8<----------8<----------8<----------
--
Those against Rushdie haven't |Maarten Litmaath @ VU Amsterdam:
learned anything from the Dark Ages. |maart@cs.vu.nl, mcvax!botter!maart
ftw@masscomp.UUCP (Farrell Woods) (03/19/89)
In article <817@krafla.rhi.hi.is> frisk@rhi.hi.is (Fridrik Skulason) writes: >I am looking for some silly but difficult problems for use in a contest. I saw this once on a midterm exam given in a first-semester Pascal course. It was one of those "find all the syntax errors in the following program" deals. The same stunt can be applied in C as well. For sake of brveity, I've shortened the example: program foo(input, output) begin [ body of foo ] (* some miscellaneous comment in the middle of foo * ) (* which of course was obscured by other comments *) end. Aside from missing semi-colons, parens, quotes, etc. there was the space between the splat and close paren on that one line. It was rather subtle. -- Farrell T. Woods Voice: (508) 392-2471 Concurrent Computer Corporation Domain: ftw@masscomp.com 1 Technology Way uucp: {backbones}!masscomp!ftw Westford, MA 01886 OS/2: Half an operating system
gillies@p.cs.uiuc.edu (03/19/89)
Here are some memorable problems from some high school programming contests ten years ago. About 1/5 of the entrants solved 7 problems (all of them) in 3 hours: 1. Compute 2^60 EXACTLY and print out the result (note: this can be done cleverly using two or three 32-bit words, and manually shifting the overflow to the second or third variable) 2. Compute 1/(2^60) and print out the result. [Next year's problem] 3. Define a "perfect card deck shuffle". Assume the cards are numbered 1..52. Where is 13 after 2087 (or some huge number) of "perfect shuffles". 4. Write a program to translate integers to roman numerals, and print them (judge tests the program interactively) 5. Compute the number of ways to make change for $1, $2, and/or $5, using (whatever assumptions you want about legality of dollar bills). If you have young contestants, remember to include a few "easy" problems so nobody goes home unhappy. Good luck with your contest!
net@tub.UUCP (Oliver Laumann) (03/19/89)
In article <817@krafla.rhi.hi.is> frisk@rhi.hi.is (Fridrik Skulason) writes: > "write a one line C program that produces the source code > to itself when run." > > Any suggestions ? main(s){printf(s,34,s="main(s){printf(s,34,s=%c%s%c,34);}",34);} [Yes, I know, it is not portable, assumes ASCII, etc.] Regards, -- Regards, Oliver Laumann, Technical University of Berlin, Germany. ...!pyramid!tub!net or net@TUB.BITNET
utoddl@ecsvax.UUCP (Todd M. Lewis) (03/20/89)
In article <984@masscomp.UUCP>, ftw@masscomp.UUCP (Farrell Woods) writes: > In article <817@krafla.rhi.hi.is> frisk@rhi.hi.is (Fridrik Skulason) writes: > >I am looking for some silly but difficult problems for use in a contest. > > I saw this once on a midterm exam given in a first-semester Pascal course. > It was one of those "find all the syntax errors in the following program" > deals. The same stunt can be applied in C as well. For sake of brveity, > I've shortened the example: > > program foo(input, output) > > begin > > [ body of foo ] (* some miscellaneous comment in the middle of foo * ) > (* which of course was obscured by other comments *) > > end. Which is an excellent example demonstrating the desirability of (* Nested (* Comments *) *). If nested comments were the norm, the above program would have produced a compile-time error to the effect of "comment from line ## not terminated. Unexpected end of text". This can be applied in C as well (is there an echo in here). In that case, you wouldn't have to use those ugly preprocessor directives to "comment out" sections of code. Don't get me wrong, I like the preprocessor for what it is, but using it for comment delineation is Simply Wrong (IMHO). _____ | Todd M. Lewis Disclaimer: If you want my employer's ||\/| utoddl@ecsvax.uncecs.edu ideas, you'll have to || || _buy_ them. | || |___ (Never write a program bigger than your screen.)
bph@buengc.BU.EDU (Blair P. Houghton) (03/22/89)
In article <2179@fireball.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes: >frisk@rhi.hi.is (Fridrik Skulason) writes: >\ "write a one line C program that produces the source code >\ to itself when run." [...] "the source code to itself" -- that's what it sez. >A cheat. >----------8<----------8<----------8<----------8<----------8<---------- >main(){execlp("cat","",__FILE__,(char*)0);} >----------8<----------8<----------8<----------8<----------8<---------- Not so big a cheat. It seems it really would produce it's own source, rather than just a copy or whatever its buggy little self happens to contain. This way, you are _certain_ that the program is what its source looks like. Except now you have to keep them together forever...but it may be the only answer that fits the spec perfectly. --Blair "Maybe there should be some 'find' and 'diff' action in there, just to be sure..."
ams@auspex.UUCP (Allan Schwartz) (03/22/89)
In article <817@krafla.rhi.hi.is> frisk@rhi.hi.is (Fridrik Skulason) writes: >I am looking for some silly but difficult problems for use in a contest. >One example is the following: > > "write a one line C program that produces the source code > to itself when run." (seen before on this net): main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);} -allan -- Allan M. Schwartz ams@auspex.com +1 408 970-8970 {uunet,sun}!auspex!ams
sam@neoucom.UUCP (Scott A. Mason) (03/23/89)
In article <817@krafla.rhi.hi.is> frisk@rhi.hi.is (Fridrik Skulason) writes: > > "write a one line C program that produces the source code > to itself when run." > I'm assuming that the intent of a "one line" program is that it is one line of code, not including the header and variable declarations. The only other assumption made is that the source has a ".c" extension to the program being run. Otherwise, here's my program. ---------------------------------- #include <stdio.h> main (argc, argv) int argc; char *argv[]; { FILE *fp; int ch; for ((strcat (argv[0], ".c")) && (fp = fopen (argv[0], "r")); (ch = getc (fp)) != EOF; putchar (ch) ) ; } ---------------------------------- Notice, that the code part is only one statement long. It certainly could have been written as one text line, but it would have been slightly less readable by many terminals. -------------------------------------------------------------------------------- "If it ain't broke, don't fix it," and certainly don't blame me. UUCP: {pitt, scooter, hal, cwjcc}!neoucom!sam INTERNET: sam@neoucom.UUCP Scott A. Mason Coordinator of Systems Operations, NEOUCOM