Same code shape and output shape

4

1

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.

Quillion

Posted 2013-11-22T16:00:35.080

Reputation: 809

Question was closed 2013-12-09T16:11:25.263

"Most complex and interesting" aren't exactly objective judging criteria. – Iszi – 2013-11-22T16:07:00.970

@Iszi please let me know what you think I should have as winning criteria? I was having a big difficulty deciding. – Quillion – 2013-11-22T16:23:10.513

A possible winning criteria could be "fewest bytes of code", but then you would have to tag your question as code-golf, not as code-challenge or code-bowling. – ProgramFOX – 2013-11-22T16:31:37.457

@ProgramFOX The problem is that I would like to see some really nice complex shaped code. Like a circle that is hollow inside, or homer simpson's head. Having very few bytes doesn't make it as entertaining because it defeats the purpose of trying to make a cool shape. I was originally thinking of making it smallest code, but that defeats being creative. Unless a lot of people agree on this. – Quillion – 2013-11-22T16:34:45.517

1The 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. – ProgramFOX – 2013-11-22T16:37:06.633

@ProgramFOX I agree with you. but what criteria should I use that would not hinder creativity? – Quillion – 2013-11-22T16:41:31.563

Let the other members decide: after some days, ask in your question "Please post a comment to the most creative answer (in your opinion), containing 'This is the most creative answer', and the comment that get most upvotes, this answer wins". I think you need to re-formulate my bad-formulated phrases, but it is an idea. – ProgramFOX – 2013-11-22T16:45:14.403

@ProgramFOX That still leaves us with a subjective criteria, where entries are judged based on opinions rather than some absolute qualitative value. – Iszi – 2013-11-22T16:47:26.983

@Iszi: You're right. What would you suggest as criteria then? – ProgramFOX – 2013-11-22T16:52:04.010

@ProgramFOX It's nigh impossible to define an objective criteria for a challenge such as this, without defining the desired output shape. It may even be necessary to define the dimensions in order to be truly objective. Changing it to [tag:code-golf] would be a very bad idea, without further restrictions, since many scripting languages could easily tie for first with a single character. – Iszi – 2013-11-22T16:54:33.583

@Iszi: Then unfortunately, I think the only thing we can do is vote to close the question. – ProgramFOX – 2013-11-22T16:57:03.530

In its current form, I could take it to PowerShell and draw some ASCII pr0n with some long numbers. It's rather trivial, really. In fact, I'd probably get more upvotes just for the boobies than I would for any real creativity. – Iszi – 2013-11-22T16:58:32.387

@Iszi ok fine you are right :( Shortest code wins, unless we come up with something better – Quillion – 2013-11-22T17:03:06.977

@Quillion You missed my comment about [tag:code-golf] for this question, then. I could win that in PowerShell, and I'm sure a lot of others could tie me in other scripting languages, with any single-digit number. – Iszi – 2013-11-22T17:04:22.277

@Iszi then what do I do? – Quillion – 2013-11-22T17:05:32.313

@Quillion As I said, without any specification as to what the shape is or its dimensions, there's really nothing that can be done for this question. Unless someone else has a better idea for an objective winning criteria, the only option I see is closure. – Iszi – 2013-11-22T17:06:44.837

1@Iszi ok I gave what shape to make. – Quillion – 2013-11-22T17:19:03.407

@Quillion So, does the code itself have to be in that shape or does it just have to be some sort of visual representation of that shape? The former may restrict the languages that can be used, or force a lot of comments, depending on how tolerant some languages are when it comes to massive amounts of whitespace. The latter again makes the challenge relatively trivial for some languages. Again I could put together a block of numbers, which would compose a fair visual representation of that shape, and easily have the code and ouptut shapes match (in fact, the code is the output) in PowerShell. – Iszi – 2013-11-22T17:25:40.673

How about using the popularity-contest tag? – marinus – 2013-11-22T17:26:36.643

@marinus Wait - we have that? – Iszi – 2013-11-22T17:27:26.157

@Iszi what do you think? Is that ok? – Quillion – 2013-11-22T17:34:39.477

@Quillion I'm not keen on the tag myself, but I'm also relatively new on the site. I'll let others weigh in on this bit. – Iszi – 2013-11-22T18:19:21.290

@Iszi agreed, let's hear other people's voice – Quillion – 2013-11-22T18:20:14.293

For future reference, please use the sandbox in Meta (currently, Mk V) to test new questions - especially if you're feeling iffy on its viability or the judging criteria. – Iszi – 2013-11-22T18:26:38.657

2If you do not care about the characters being used, any quine will pass your conditions, right? – saeedn – 2013-11-24T06:47:42.820

Answers

20

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.

Dom Hastings

Posted 2013-11-22T16:00:35.080

Reputation: 16 415

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. – Quillion – 2013-11-22T18:23:16.683

17

on the Dyalog APL terminal

abcde fghij

outputs

VALUE ERROR

protist

Posted 2013-11-22T16:00:35.080

Reputation: 570

Well to be completely accurate it outputs VALUE ERROR abcde fghij ^. (and some spaces that won't show up.) – marinus – 2013-11-25T04:45:22.817

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. – ngn – 2014-06-13T12:31:32.590

13

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.

saeedn

Posted 2013-11-22T16:00:35.080

Reputation: 1 241

7

Python, 0 (kind of cheating)

Here's a totally valid Python program:



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



:)

psxls

Posted 2013-11-22T16:00:35.080

Reputation: 311

2Empty code block: <pre></pre>. – manatwork – 2013-11-25T13:31:53.940

1Technically, it wont print – user80551 – 2013-12-05T05:08:55.190

This is a polyglot actually. – Cruncher – 2013-12-05T15:28:05.937

@user80551 Technically print("") won't print. – Cruncher – 2013-12-05T15:29:29.587

@Cruncher But the output wont look like ydluicbe7 either. – user80551 – 2013-12-06T12:53:12.757

@user80551 My point is, that not printing, is functionally equivalent to printing blank. They are completely indistinguishable. So not writing anything is an equivalent program to print(""), which does print itself. – Cruncher – 2013-12-06T13:26:59.547

4

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.

user80551

Posted 2013-11-22T16:00:35.080

Reputation: 2 520

print 'a'*5+'\x20'+'a'*19 – gnibbler – 2013-11-25T10:22:12.700

for python3 you can do print('a'*13) – Cruncher – 2013-12-06T13:48:20.973

that would require a from __future__ import print_function – user80551 – 2013-12-06T17:16:26.273

3

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 {{} \{ \}} }

psxls

Posted 2013-11-22T16:00:35.080

Reputation: 311

Right. This is a more lax condition on quines – Cruncher – 2013-12-05T15:30:31.560

1

or if you are picky

5 ⍴ 1 + 1

outputs

2 2 2 2 2

protist

Posted 2013-11-22T16:00:35.080

Reputation: 570

What language is it? – Timtech – 2013-12-02T00:08:00.210

@Timtech, it's APL. – epidemian – 2013-12-03T01:28:21.897

1

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

psxls

Posted 2013-11-22T16:00:35.080

Reputation: 311

You need longer code to make an interesting picture :) – Cruncher – 2013-12-05T15:32:30.837

1

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

Doorknob

Posted 2013-11-22T16:00:35.080

Reputation: 68 138

1

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   }

F. Hauri

Posted 2013-11-22T16:00:35.080

Reputation: 2 654

0

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

user80551

Posted 2013-11-22T16:00:35.080

Reputation: 2 520

0

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

AMK

Posted 2013-11-22T16:00:35.080

Reputation: 506

3print-1e5 and print(-1e7) are shorter :P – Reinstate Monica – 2013-11-24T23:53:16.690

1For Py3K works also print(1e7) – AMK – 2013-11-26T15:43:33.280

0

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

F. Hauri

Posted 2013-11-22T16:00:35.080

Reputation: 2 654

You can drop the space before the <; being an operator character means it's forced to delimit the word token before it. – FireFly – 2013-12-01T10:52:18.093

@FireFly Thanks, I've missed this! – F. Hauri – 2013-12-01T11:56:24.763