1. (fwd) self-printing program - Reply

Andy Kurnia relayed:

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

1. It sort of disqualifies all interpreted (only) languages.
2. More importantly, how do you print the source you have already
   discarded? And, please, don't tell me you are supposed to keep it in
   an internal string... Jiri

new topic     » topic index » view message » categorize

2. Re: (fwd) self-printing program - Reply

>1. It sort of disqualifies all interpreted (only) languages.
>2. More importantly, how do you print the source you have already
>   discarded? And, please, don't tell me you are supposed to keep it in
>   an internal string... Jiri


Yeah! It seems the contest is to create a C/Java/etc.. decompiler! (and
without symbolic table).

Regards,
    Daniel Berstein
    daber at pair.com

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

3. Re: (fwd) self-printing program - Reply

>The basic problem is simple:  Write a completely self-contained program
>that prints out its own source code.
>  * Your program should be as short as possible.

My entry is 101 bytes long, one line (final \n does NOT exist* -- Euphoria
*can* interpret such .ex files!)... can anyone do better?
--[prtself.ex]--
constant a="constant a=",b="puts(1,a&34&a&34&44&98&61&34&b&34&b)"puts(1,a&34&a&3
4&44&98&61&34&b&34&b)
--end [prtself.ex]--

* To make such a file, use COPY CON prtself.ex and immediately ^Z (F6)
  after the last char, or:
   1. Write the whole 101 chars line in any editor supporting that. (p.ex)
      While ed.ex does not support it (I think), Microsoft's EDIT.COM does,
      as does the following: (change the ellipsis to complete the above)
        echo constant a="constant a=",b="..."puts(1,...)>p.ex
        ^^^^^                                           ^^^^^ <- add this
   2. ex p >prtself.ex
   3. del p.ex
  And you have the prtself.ex without final \n blink

And yep, it works (under EX 2.0, at least) without a "line too long"
complaint blink

>Date:    Thu, 7 May 1998 10:15:40 +1200
>From:    "BABOR, JIRI" <J.Babor at GNS.CRI.NZ>
>
>1. It sort of disqualifies all interpreted (only) languages.
>2. More importantly, how do you print the source you have already
>   discarded? And, please, don't tell me you are supposed to keep it in
>   an internal string... Jiri

(1) Nope, all interpreted-only languages (I consider Euphoria NOT to be
    interpreted-only, because it can shroud and bind, i.e. it can
    transform its source to some non-human-understandable form) are also
    eligible for the challenge, as long as they don't open the source
    file and display it... --> system("type prtself.ex",2) -- will do!!!

(2) Read Pete's reply. Or my 101 bytes code above. So it's kept in an
    internal string, so what? It's still not an easy challenge then... blink

>Date:    Thu, 7 May 1998 19:22:49 -0300
>From:    Daniel Berstein <daber at PAIR.COM>
>
>Yeah! It seems the contest is to create a C/Java/etc.. decompiler! (and
>without symbolic table).

To create a decompiler? No! I quote: (the 'I' below doesn't refer to me)
>  * 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.)

"Shouldn't access any external storage" means: No (file) open. You mayn't
read the source file, nor the executable. And if you want to read the
compiled bytes in the memory, it won't be better than Pete's entry because:
 1. It should run and print the same thing (i.e. its source) regardless of
    its current form (whether it is in source form, in shrouded form, as an
    executable, or whatever). A decompiler would need the executable code,
    and would fail if what you have is the source (just execute it w/ ex).
 2. It would certainly be bigger than my entry, and thus violates
    >  * Your program should be as short as possible.

>From:    Pete Eberlein <xseal at HARBORSIDE.COM>
>Subject: Re: (fwd) self-printing program
>
>Okay, here's my entry:
[snip]
>exact formatting looks to be impossible (the lines breaks are in the wrong
>place) but otherwise the code prints itself.

Nice solution, indeed.

The problem with it is that the printed source won't run. (Line too long.)
You can add an extra if and some constants to \n out the long line...

It should be possible to do this (assuming filename is prtself.ex):
    ex prtself>prtself2.ex
    ex prtself2>prtself3.ex
(at this time prtself.ex, prtself2.ex and prtself3.ex should have the exact
same content -- use fc /b) --> indent the source as it would print itself.

The indentation doesn't matter in Euphoria, and this fact can be used to
shorten the source.

>Can anyone do better?

I can blink but can anyone *else* do even better?

>--
> _______  ______  _______  ______
>[    _  \[    _ ][ _   _ ][    _ ]
>[/| [_] |[/| [_\][/ | | \][/| [_\]
>  |  ___/  |  _]    | |     |  _]
>[\| [/]  [\| [_/] [\| |/] [\| [_/]
>[_____]  [______] [_____] [______]
>     xseal at harborside.com
>
>New sig! 208 bytes down from 678

Finally blink

But I don't like those triangles at the end of most lines...
How about a SHORTER one? Like:
 ___   ___   ___   ___
[ _ ] [ __] [   ] [ __]
[  _] [ _]   [ ]  [ _]
[_]   [___]  [_]  [___]
--

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

Search



Quick Links

User menu

Not signed in.

Misc Menu