7
\$\begingroup\$

I posted this to The Daily WTF coder challenge forum a few years ago but only got one response. I figure CodeGolf stack exchange might generate more interest!

Description

Write a program that runs when the rows and columns of the source code are transposed.

In other words, imagine each character on each line is a cell in a matrix (pad out shorter lines as necessary to make the matrix rectangular). Write a functioning program that still functions even if its "matrix" is transposed.

This is just a creative puzzle, not a golf.

Details

  1. The program must execute without error and do something interesting*
  2. The transpose of the program must execute without error and do something interesting, although not necessarily the same thing as the original.
  3. Any language.
  4. No shebang required.
  5. Single row/single column entries disallowed.

*Interesting is the subjective opinion of the CodeGolf community.

Deadline April 15 2011

Trivial Example

Here's a really stupid example, but it illustrates the rules pretty well. Of course, any "symmetric" program will meet this challenge, but I encourage you to try non-symmetric programs.

# #####################
 print "hello,world\n";
#r
#i
#n
#t
#
#"
#h
#e
#l
#l
#o
#,
#w
#o
#r
#l
#d
#\
#n
#"
#;

Transposer Reference Implementation

Here's a perl snippet that does the type of transposing that I am talking about. This is not the objective of the puzzle, it is just a helper tool.

#!/usr/bin/perl
my @matrix;
open('SOURCE', $ARGV[0]);
my $matrixwidth = -1;
my $matrixheight = 0;
foreach (<SOURCE>) {
  chomp;
  if ($matrixwidth < length $_) {$matrixwidth = length $_}
  my @row = split(//, $_);
  push @matrix, \@row;
  $matrixheight++;
}
foreach my $x (0 .. ($matrixwidth-1)) {
  foreach my $y (0 .. ($matrixheight-1)) {
    print(defined $matrix[$y]->[$x] ? $matrix[$y]->[$x] : ' ');
  }
  print "\n";
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Uninteresting set of solutions: Any program in a whitespace-insensitive language with single character operators and keywords (like APL, J, GoldScript, Jack Chrenshaw's TINY...) can qualify simply my writing it on one line. \$\endgroup\$ – dmckee --- ex-moderator kitten Mar 25 '11 at 17:36
  • 1
    \$\begingroup\$ I can just put 1 into a source file and have a quine that still works when transposing the lines ... \$\endgroup\$ – Joey Mar 25 '11 at 17:40
  • \$\begingroup\$ Correction: any such program that only involves single character literals. \$\endgroup\$ – dmckee --- ex-moderator kitten Mar 25 '11 at 17:42
  • 1
    \$\begingroup\$ @Joey, I don't think that would meet the first requirement (interesting behavior). \$\endgroup\$ – Mark E. Haase Mar 25 '11 at 17:43
  • 1
    \$\begingroup\$ @Joey I added a rule that submissions need to be more than 1 row or 1 column in size. @dmckee I am not familiar with those languages. Can you clarify your issue? Assuming that the operators have operands, then transposing the program will modify the order of the operator and operands, potentially separating the two in a way that's not syntactically correct. Hence the challenge. Unless I'm being naive? \$\endgroup\$ – Mark E. Haase Mar 25 '11 at 17:52
11
\$\begingroup\$

c

Not-quite-trivial, not-quite-symmetric solution that prints it's command line arguments, one per line. As shown it prints them in order, as transposed in reverse order.

/*******ipcc;m(acaw-{aa) 
********nuohiairhrh-prr; 
********ttnaningagiaugg} 
******** ssrtntcrvlrtvc} 
******** (t*   ,*)egs[]  
********   s    *{(c(    
********   )       )     
*******/                 
int                      
puts(                    
const                    
char*s)                  
;int                     
main                     
(int                     
argc,                    
char**                   
argv){                   
int i;                   
for(i=1;                 
i<argc;                  
i++)                     
{puts(                   
argv[i]                  
);}}                     
\$\endgroup\$
  • \$\begingroup\$ Haha! Very interesting approach! +1 \$\endgroup\$ – Mark E. Haase Mar 27 '11 at 4:09
  • \$\begingroup\$ I like the starry blob in the upper-left corner ;) \$\endgroup\$ – Joey Mar 27 '11 at 10:42
  • \$\begingroup\$ @mehaase: Frankly I didn't think it was philosophically different than your "trivial" example. I just thought the trick with the block comment was cute. In principle it is extensible to any two complete programs. \$\endgroup\$ – dmckee --- ex-moderator kitten Mar 27 '11 at 22:30
  • \$\begingroup\$ Well, april 15 came and went with no other submissions :( But congrats, you win the mini-contest! \$\endgroup\$ – Mark E. Haase Apr 18 '11 at 14:53
16
\$\begingroup\$

Here's one I came up with when I originally wrote the challenge a few years back. It's a perl script that can transpose other scripts. You can run it on itself, save the output as a new script, then run that new script on itself to get the original.

#######       p           $f f   ]'
#      ofm    u        $  ho o   -
#      poy    s        w  +r r  p>'
#      er ####h########=##+ #   r[}
    open   'M' , $ARGV[ 0]  ; # i$p
  for(;   <M>  ; ){         #   nxr
      #((@             $  }( (  t]i
      #';r             #   $ $   ?n
      #M<=    @        r   x y   $t
      #'M     m        +   = =   m
      # >     ,        1   0 0   ["
my @r=   $_ =~ /./g;#  >   ; ;   $\
      #  _ /  \                # yn
push @m,     \@r;              # ]"
      # ;=    r            $ $   -}
      #  ~    ;        $   x y   >
$w=($#r   + 1>        $w)      # [
?$#r+1:     $w ;$h++}# ?   < <   $
for (     $x=0 ;$x<$w;         # x
          $x++ ){              # ]
for (     $y=0 ;$y<$h;
          $y++ ){           #$
  print               #$   $ h  d:
defined                #   w ;  e
      #$)/    #        r   ; $  f
      #A{.             +   $ y  i
      #R#/             1   x +  n
      #G g             :   + +  e
      #V ;                 + )  d
     $m[  $y]->[$x]
      #0 #                 ) {  $
 ?$m[$y]->[$x]:' '}          #  m
      #)               $   {    [
      #;               w        $
  print "\n"} #        ;        y
\$\endgroup\$

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