4
\$\begingroup\$

I am not sure if this is possible. I have tried it and couldn't make it work at all, but I am being hopeful.

If this is not possible please flag this question and I will promptly delete it.

Conditions are simple,

Write code that will output same amount of character in the same shape as the code.

For example (A very stupid example).

If code says

#include<stdio.h>

main()
{
    printf("Hello World");
}

Then output must be

kjh#$@#)(12398n,s

lkrjop
t
    jherl;k@&##*$&@3n,2m$*
t

I really do not care which characters are being used in the output or anything like that except that space is space and tab is tab. So whatever is the shape of the code, the output should be the same.

Winner: Either popularity , or help me decide the criteria.

The only way I managed to make this work, is I created a file reader that reads the code and does rot13 on every character in the code and prints it out. However I was not fond of it, because the shape was not interesting, nor do I want to use file reader to do something like this because it is too easy.

\$\endgroup\$
25
  • \$\begingroup\$ "Most complex and interesting" aren't exactly objective judging criteria. \$\endgroup\$
    – Iszi
    Nov 22, 2013 at 16:07
  • \$\begingroup\$ @Iszi please let me know what you think I should have as winning criteria? I was having a big difficulty deciding. \$\endgroup\$
    – Quillion
    Nov 22, 2013 at 16:23
  • 1
    \$\begingroup\$ The problem is that the 'coolest' shape is subjective: for example if there's a circle and a triangle as code, then perhaps you like the triangle but I like the circle. An objective winning criteria is a criteria where the opinion of someone doesn't matter. \$\endgroup\$
    – ProgramFOX
    Nov 22, 2013 at 16:37
  • 1
    \$\begingroup\$ @Iszi ok I gave what shape to make. \$\endgroup\$
    – Quillion
    Nov 22, 2013 at 17:19
  • 2
    \$\begingroup\$ If you do not care about the characters being used, any quine will pass your conditions, right? \$\endgroup\$
    – saeedn
    Nov 24, 2013 at 6:47

13 Answers 13

20
\$\begingroup\$

JS

I know it's still being contested, but I thought I'd have a go:

          (function (){a='';
       a           ;         a=
     '             ';           h
    =             '#'            ;
   s             = ' '            ;
  s             ;  s  ;            s
 ;             s   ;   s            ;
s             ;    s    ;            s
;            s     ;     s           ;
s           ;      s      ;          s
;          s       ;       s         ;
 b        =       '';       s       ;
  s      ;      return(      a     +
   b    +     b+arguments[    b   +
    'ca'   +'llee'].toString   ( )
      .replace(/\S/g,h)+'###');s;
          s                 ;
               s;s;s})()

When run in Chrome's console produces:

Chrome's console output

Not 100% accurate, but I thought it was quite close. As intangible as the criteria were to begin with, I did like the idea of the challenge, despite how easy/difficult is in differing languages.

\$\endgroup\$
1
  • \$\begingroup\$ I love it dude :) and the shape itself is my favorite too. Let's wait and see what we decide on for criteria, but this is the type of answer I had in mind. \$\endgroup\$
    – Quillion
    Nov 22, 2013 at 18:23
17
\$\begingroup\$

on the Dyalog APL terminal

abcde fghij

outputs

VALUE ERROR
\$\endgroup\$
2
  • \$\begingroup\$ Well to be completely accurate it outputs VALUE ERROR abcde fghij ^. (and some spaces that won't show up.) \$\endgroup\$
    – marinus
    Nov 25, 2013 at 4:45
  • \$\begingroup\$ Dyalog prints the result of evaluating a line of code (unless the result is "shy"), so you can reduce the solution to a one-digit number literal: 0 prints 0 The empty program is also valid, but it can be argued that it doesn't print, because lack of a result is considered a shy result. \$\endgroup\$
    – ngn
    Jun 13, 2014 at 12:31
13
\$\begingroup\$

BF

>++++++++[>+++++++>+++++++<<-]>++>++++++<>++++++<<<++++++++++[>++++<-]>[>.>.<<-]

It just outputs a set of laughing smileys:

:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D:D

Size of code and its output are both 80.

\$\endgroup\$
7
\$\begingroup\$

Python, 0 (kind of cheating)

Here's a totally valid Python program:



And here's the output of that program, of same shape:



:)

\$\endgroup\$
6
  • 2
    \$\begingroup\$ Empty code block: <pre></pre>. \$\endgroup\$
    – manatwork
    Nov 25, 2013 at 13:31
  • 1
    \$\begingroup\$ Technically, it wont print \$\endgroup\$
    – user80551
    Dec 5, 2013 at 5:08
  • \$\begingroup\$ This is a polyglot actually. \$\endgroup\$
    – Cruncher
    Dec 5, 2013 at 15:28
  • \$\begingroup\$ @user80551 Technically print("") won't print. \$\endgroup\$
    – Cruncher
    Dec 5, 2013 at 15:29
  • \$\begingroup\$ @Cruncher But the output wont look like ydluicbe7 either. \$\endgroup\$
    – user80551
    Dec 6, 2013 at 12:53
4
\$\begingroup\$

Python

Smallest thing I could think of.

print 'a'*5+' '+'a'*16

Output

aaaaa aaaaaaaaaaaaaaaa

That space in the middle is part of a string so it doesn't count.

\$\endgroup\$
3
  • \$\begingroup\$ print 'a'*5+'\x20'+'a'*19 \$\endgroup\$
    – gnibbler
    Nov 25, 2013 at 10:22
  • \$\begingroup\$ for python3 you can do print('a'*13) \$\endgroup\$
    – Cruncher
    Dec 6, 2013 at 13:48
  • \$\begingroup\$ that would require a from __future__ import print_function \$\endgroup\$
    – user80551
    Dec 6, 2013 at 17:16
3
\$\begingroup\$

Quines

Any quine (a program producing a copy of its own source code) would do the trick. There are thousand examples on the internet written in many different languages. For example:

Perl, 34 Author: V Vinay

$_=q(print"\$_=q($_);eval;");eval;

Python, 33 Author: Frank Stajano

l='l=%s;print l%%`l`';print l%`l`

Java, 252 Bertram Felgenhauer

class S{public static void main(String[]a){String s="class S{public static void main(String[]a){String s=;char c=34;System.out.println(s.substring(0,52)+c+s+c+s.substring(52));}}";char c=34;System.out.println(s.substring(0,52)+c+s+c+s.substring(52));}}

Tcl, 34 Author: Joe Miller

join {{} \{ \}} {join {{} \{ \}} }
\$\endgroup\$
1
  • \$\begingroup\$ Right. This is a more lax condition on quines \$\endgroup\$
    – Cruncher
    Dec 5, 2013 at 15:30
1
\$\begingroup\$

or if you are picky

5 ⍴ 1 + 1

outputs

2 2 2 2 2
\$\endgroup\$
2
  • \$\begingroup\$ What language is it? \$\endgroup\$
    – Timtech
    Dec 2, 2013 at 0:08
  • \$\begingroup\$ @Timtech, it's APL. \$\endgroup\$
    – epidemian
    Dec 3, 2013 at 1:28
1
\$\begingroup\$

Perl

Any program which reads it's own source file will do (technically, it's not considered to be a quine). Here's a simple Perl script (my lack of inspiration restricted me from giving to the code a shape more complex than a diamond/rhombus)!

         use
      warnings;
   use strict;open
(my $IN,'<',$0)or die
   "PCG FTW";print
     <$IN>;close
         $IN

Which will print:

         use
      warnings;
   use strict;open
(my $IN,'<',$0)or die
   "PCG FTW";print
     <$IN>;close
         $IN
\$\endgroup\$
1
  • \$\begingroup\$ You need longer code to make an interesting picture :) \$\endgroup\$
    – Cruncher
    Dec 5, 2013 at 15:32
1
\$\begingroup\$

HQ9+

 +++    +++
 +++    +++

     ++
++   +Q   ++
++        ++
 ++      ++
  +++  +++
    ++++

Or, something like this:

                  ___                       __   _____ 
  ____  ____   __| _/____      ____   ____ |  |_/ ____\
_/ ___\/  _ \ / __ |/ __ \    / ___\ /  _ \|  |\   __\ 
\  \__(  <_> ) /_/ \  ___/   / /_/  >  <_> )  |_|  |   
 \___  >____/\____ |\___  >  \___  / \____/|____/__|   
     \/           \/    \/  /_____/                  Q  

Credits to this site ;)

Basically, anything will work, as long as it has exactly:

  • 0 Hs
  • 0 9s
  • 1 Q

Or, some minimalist solutions:

  • 1 char

    Q
    
  • 0 chars

\$\endgroup\$
1
\$\begingroup\$

Perl 44 chars

open O,"<$0";while(<O>){y|!-~|P-~!-O|;print}

In action:

echo $'#!/usr/bin/perl\nopen O,"<$0";while(<O>){y|!-~|P-~!-O|;print}' >/tmp/autohide.pl
chmod  +x /tmp/autohide.pl 
/tmp/autohide.pl 
RP^FDC^3:?^A6C=
@A6? ~[QkS_QjH9:=6Wk~mXLJMP\OM!\OP\~MjAC:?EN

reverse:

/tmp/autohide.pl | perl -pe 'y|!-~|P-~!-O|'
#!/usr/bin/perl
open O,"<$0";while(<O>){y|!-~|P-~!-O|;print}

Rot47 vs Rot13:

To correspond to behaviour of Rot13, but from ! to ~, there is 94 chars. I wrote rot47:

#!/usr/bin/perl -s
$                 s
?           $     O
=          *      STDIN
:                   open
$                   O
,                "<"
.                  $
0           ;while
(       <$O>
)      {#
y|!-~|P-~!-O|
;    print   }

(I've tried to draw beavis, but this is not very well!)

Store them, for sample in autohide.pl:

chmod  +x autohide.pl 

with executable rights,

./autohide.pl
RP^FDC^3:?^A6C= \D
S                 D
n           S     ~
l          Y      $%sx}
i                   @A6?
S                   ~
[                QkQ
]                  S
_           jH9:=6
W       kS~m
X      LR
JMP\OM!\OP\~M
j    AC:?E   N

Than, there is no reverse operation, it's only repeat same translation.

When run with -s parameter, this use STDIN instead of himself code, so running ./autohide.pl -s <autohide.pl do same as if run without argument:

./autohide.pl -s <autohide.pl | ./autohide.pl -s
#!/usr/bin/perl -s
$                 s
?           $     O
=          *      STDIN
:                   open
$                   O
,                "<"
.                  $
0           ;while
(       <$O>
)      {#
y|!-~|P-~!-O|
;    print   }
\$\endgroup\$
0
\$\begingroup\$

Python

Generic solution. Just save the file as 'source.py' and execute.

import random

def main():
    f = open('source.py')
    s = ''
    d = {' ':1,
     '\t':2,
     '\n' :3}
    rstring = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`1234567890-=~!@#$%^&*()_+,./<>?;:[]{}|'
    for line in f:
        for char in line:
            if char in d:
                s += char
            else:
                s += random.choice(rstring)
    print s

main()
\$\endgroup\$
0
\$\begingroup\$

May be simplest solution for Python

print'*'*11

and for Py3K

print('*'*13)

and another generic solution for Py3K

import random, sys

def main():
    f = open(__file__).read()
    d = ' \t\n'
    r = ''.join(chr(c) for c in range(33, 127))
    sys.stdout.write(''.join(c if c in d else random.choice(r) for c in f))

if __name__ == '__main__':
    main()
\$\endgroup\$
2
  • 3
    \$\begingroup\$ print-1e5 and print(-1e7) are shorter :P \$\endgroup\$ Nov 24, 2013 at 23:53
  • 1
    \$\begingroup\$ For Py3K works also print(1e7) \$\endgroup\$
    – AMK
    Nov 26, 2013 at 15:43
0
\$\begingroup\$

Bash 9 8 chars (Thanks @FireFly)

rot13<$0

This match requirement:

  • output same amount of character in the same shape as the code
  • rot13

Practical

echo  >autorot13.sh 'rot13 <$0' 
chmod  +x autorot13.sh 
./autorot13.sh 
ebg13<$0
\$\endgroup\$
2
  • \$\begingroup\$ You can drop the space before the <; being an operator character means it's forced to delimit the word token before it. \$\endgroup\$
    – FireFly
    Dec 1, 2013 at 10:52
  • \$\begingroup\$ @FireFly Thanks, I've missed this! \$\endgroup\$ Dec 1, 2013 at 11:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.