The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Program that runs when rows and columns are transposed

Last post 06-14-2009 9:48 PM by Faxmachinen. 13 replies.
Page 1 of 1 (14 items)
Sort Posts: Previous Next
  • 12-16-2008 3:21 PM

    Program that runs when rows and columns are transposed

    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 program that can run when that matrix is transposed.

    I have a cheap perl solution:

    perl:

    # #####################
     print "hello,world\n";
    #r
    #i
    #n
    #t
    #
    #"
    #h
    #e
    #l
    #l
    #o
    #,
    #w
    #o
    #r              b
    #l               y
    #d
    #\                 m
    #n                  a
    #"                   r
    #;                    k

    Doesn't really *do* anything because all the lines are commented out except 1. I have a PHP version that is a little less cheap, but I will wait to post it.

  • 12-16-2008 4:48 PM In reply to

    Re: Program that runs when rows and columns are transposed

    A perl script that does the kind of transposing I'm talking about:

    transpose.pl:

    #!/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";
    }


  • 12-18-2008 4:20 PM In reply to

    Re: Program that runs when rows and columns are transposed

    Damn, I was hoping somebody would jump in here... I guess the challenge forum is dead. Or nobody likes my challenge.

    Anyway, here's my transposable PHP script. It prints out the square of the first parameter, and its transpose prints out the square root.

    php:

    <?php         /*  e
    ?                 c
    p                 h
    h                 o
    p
                      p
                      o
    /                 w
    *                 (
                      $
                      a
                      r
                      g
                      v
                      [
                      1
                      ]
                      ,
                      1/
                      /*
    */
    echo pow($argv[1],2)."\n";
                   /**)
                     /.      *
                      "      /
                      \
                      n
                      "
                      ; */

    As an added challenge: I dare anybody to try this with syntax highlighting turned off... Yikes

    In all seriousness, I think the next level of challenge would be to create a transposeable program that has several executable lines (instead of just one).

  • 01-06-2009 9:06 AM In reply to

    • jyc
    • Not Ranked
    • Joined on 01-06-2009
    • Posts 1

    Re: Program that runs when rows and columns are transposed

    can you create a program that can transpose a square matrix?

  • 01-06-2009 2:22 PM In reply to

    Re: Program that runs when rows and columns are transposed

    jyc:

    can you create a program that can transpose a square matrix?

     

    No, this problem is NP-hard and there is no known algorithm that can evaluate this problem in a finite timeframe (e.g. < 100 billion years).

  • 01-06-2009 3:48 PM In reply to

    Re: Program that runs when rows and columns are transposed

    seriousJoker:
    No, this problem is NP-hard and there is no known algorithm that can evaluate this problem in a finite timeframe (e.g. < 100 billion years).
    Just use Java.

  • 01-06-2009 4:28 PM In reply to

    Re: Program that runs when rows and columns are transposed

    seriousJoker:

    jyc:

    can you create a program that can transpose a square matrix?

     

    No, this problem is NP-hard and there is no known algorithm that can evaluate this problem in a finite timeframe (e.g. < 100 billion years).

     

    At first I was like "WTF!?"

    Then I was like "ohhhh."

  • 01-07-2009 10:56 AM In reply to

    Re: Program that runs when rows and columns are transposed

    Well, I dunno. Last time I checked the transpose was computed by

    for(int i = 0; i < n; i++)
      for(int j = 0; j < n; j++)
        transposed_matrix(i,j) = the_matrix(j,i);

    Maybe it's just me but I think that question was just a little more elaborate than "How do you compute 1 + 1 in a program". And by reveiling the solution I'm sure I just made someone elses homework.

  • 01-15-2009 2:09 PM In reply to

    Re: Program that runs when rows and columns are transposed

     

    seriousJoker:

    Well, I dunno. Last time I checked the transpose was computed by

    for(int i = 0; i < n; i++)
      for(int j = 0; j < n; j++)
        transposed_matrix(i,j) = the_matrix(j,i);

    Maybe it's just me but I think that question was just a little more elaborate than "How do you compute 1 + 1 in a program". And by reveiling the solution I'm sure I just made someone elses homework.

    I'm pretty sure he meant a transposeable program that transposes a square matrix... given the nature of the thread.

  • 01-15-2009 4:30 PM In reply to

    Re: Program that runs when rows and columns are transposed

    Here's the answer. This perl script can transpose itself and still run correctly. (eg "perl transpose.pl matrix.txt; perl transpose.pl transpose.pl > transpose.pl; perl transpose.pl matrix.pl")

    transpose.pl:

    #######       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
  • 05-07-2009 5:25 AM In reply to

    Re: Program that runs when rows and columns are transposed

    savar:

    Here's the answer. This perl script can transpose itself and still run correctly. (eg "perl transpose.pl matrix.txt; perl transpose.pl transpose.pl > transpose.pl; perl transpose.pl matrix.pl")

     

    "perl transpose.pl transpose.pl > transpose.pl" will truncate the program to 0 bytes because the output file, "transpose.pl" will be first opened for writing and truncated. Then it will be executed (and obviously do nothing)


  • Wed, Dec 15 2010 6:34 AM

    • Username
    • Top 10 Contributor
    • Joined on Mon, Jan -1 3141.5926535
    • Posts -70 trillion
    Hey PJH, make your own signature guy instead of copying mine (complete with bugs)
  • 06-10-2009 3:05 PM In reply to

    Re: Program that runs when rows and columns are transposed

    Dammit. I should have tested that. Well at least 1 person read my thread.
  • 06-14-2009 11:44 AM In reply to

    Re: Program that runs when rows and columns are transposed

    I commend you, sir. That's just brilliant.

    Begging the question since 2007.
  • 06-14-2009 9:48 PM In reply to

    Re: Program that runs when rows and columns are transposed

    A Brainfuck program that prints "Hello!":
    [                                ++ []] 
     +  +                            +  <>  
      [>+              >               <    
     + + [           >       -       - ++   
           -+             +          + +    
           +[>                          + + 
              <-  ]                  --++   
                <-                    -+    
               ]  <-]>                - +   
          >         >                 . ><  
                      <<     +          -   
      >               <-+             ++    
                       +[>           ++     
           +              [>+        +      
                           +<-]        +  - 
                              <-            
                                        ]   
                                 >      <>  
                                  >+ +      
                                    .+ +    
    ++     +    -        +-+       ++.+++  -
    +      -  --     . + +           +  .   
      <-   +  ++   +   +  +    +    ++      
    [< +   + +  +            > -]<   +.  [[>
    ]>                       <   >      [-  
    ]        +     -                    [ ] 
                                     -  >  ]
    
    I also wrote a Brainfuck interpreter in Perl, but it proved hard to splice it into the Brainfuck program due to the commas (I could have used the "=>" alias, but it's getting late and I should go to bed). That's why it's such a horrible abomination.
    local $/;@d=split //, <>;
    %s=qw/[ $q++ ] $q--/;
    %a=qw/+ $m[$i]++ - $m[$i]-- < $i-- > $i++ . print(chr($m[$i])) , $m[$i]=ord(getc) [ $m[$i]?push(@l,$p):&skip() ] @l?$p=pop(@l)-1:0/;
    while($p<@d){eval $a{$d[$p]};$p++}
    sub skip {$q=1;while($q&&$p<@d){$p++;eval $s{$d[$p]}}}
    
    Begging the question since 2007.
Page 1 of 1 (14 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems