1. (fwd) self-printing program

Any Euphoria solution for this one?

>Date: Sun, 19 Apr 1998 00:08:49 -0400 (Eastern Daylight Time)
>From: Brendan Connell <bconnell at mbhs.edu>
>To: hs-computing at delos.com
>Subject: self-printing program
>
>This doesn't really seem like an olympiad-style problem, but it's an
>interesting programming challenge anyway...
>
>The basic problem is simple:  Write a completely self-contained program
>that prints out its own source code.
>
>A couple guidelines:
>  * You can use practically any modern programming language--C, Pascal,
>    Perl, etc.
>  * Your program should be as short as possible.
>  * By "self-contained", I mean the program shouldn't access any
>    external storage.  (This also means that once compiled, the program
>    should work even if the original source code is discarded.)
>
>It should quickly become obvious that (in most languages, at least) a
>one-line program consisting of a single "print" statement won't do the
>trick.
>
>I'll post my own solution if anyone wants to see it.
>
>-Brendan
>
>         (__)     ___                 _               bconnell at mbhs.edu
>         (oo)    | _ )_ _ ___ _ _  __| |__ _ _ _      connell at mnsinc.com
>  /-------\/     | _ \ '_/ -_) ' \/ _` / _` | ' \
> / |     ||      |___/_| \___|_||_\__,_\__,_|_||_|    Montgomery Blair HS
>*  ||----||        http://www.mbhs.edu/~bconnell         Class of '98
>   ^^    ^^

new topic     » topic index » view message » categorize

2. Re: (fwd) self-printing program

On Wed, 6 May 1998, Andy Kurnia wrote:

> Any Euphoria solution for this one?
> >The basic problem is simple:  Write a completely self-contained program
> >that prints out its own source code.

Okay, here's my entry:

constant codes = {"constant ","codes"," = ","s","\\n","for i",
"1 to length(s) do","    ","els","e","if s[i]","0","-1","-2"," then",
"{","}"," < ","puts(1, ","-","s[i]",")","? s",
"for j = 1 to length(codes)-1 do","34&","codes[","j]&","44)",
"end for","length(codes)]&34)","])","end if"}
constant s = {1,2,3,16,-2,17,-10,1,4,3,-1,-10,6,3,7,-10,8,11,18,14,15,

for i = 1 to length(s) do
    if s[i] < -2 then
        puts(1, -s[i])
    elsif s[i] = -1 then
        ? s
    elsif s[i] = -2 then
        for j = 1 to length(codes)-1 do
            puts(1, 34&codes[j]&34&44)
        end for
        puts(1, 34&codes[length(codes)]&34)
    else
        puts(1, codes[s[i]])
    end if
end for

---- code ends ----

exact formatting looks to be impossible (the lines breaks are in the wrong
place) but otherwise the code prints itself.

Can anyone do better?
--
 _______  ______  _______  ______
[    _  \[    _ ][ _   _ ][    _ ]
[/| [_] |[/| [_\][/ | | \][/| [_\]
  |  ___/  |  _]    | |     |  _]
[\| [/]  [\| [_/] [\| |/] [\| [_/]
[_____]  [______] [_____] [______]
     xseal at harborside.com

New sig! 208 bytes down from 678

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu