I really like this puzzle.  Here is another implementation, with the core idea 
stolen from John Randall, who wrote:

>Here's another suggestion, using the binomial coefficients mod 2. 

What's nice about this implementation is the core logic is so "loopless".  Not 
even the loops implicit in   ^: " &.> /\  the powering functions of  & &:  etc. 
 Ok, you've got  !/~  but that doesn't "look loopy" to me.

           tri       =.  [: |: 2 | !/~@:i.
           tri_txt   =.  i.&.- |."_1 ":@:tri
           clean     =.  ([: ~:/\ [: </\ '1' = ])`(' '&,:) }  
           triangle  =:  clean@:tri_txt f.
        
           triangle 10
                 1         
                1 1        
               1 0 1       
              1 1 1 1      
             1 0 0 0 1     
            1 1 0 0 1 1    
           1 0 1 0 1 0 1   
          1 1 1 1 1 1 1 1  
         1 0 0 0 0 0 0 0 1 
        1 1 0 0 0 0 0 0 1 1
           
I'm interested in other ways to transform  tri N  into  triangle N  .  If, in  
tri_txt  , we substitute rotate (|.) for shift (|.!.) we can get another 
formulation:

           tri_txt   =:  i.&.- |.!.' '"_1 ":@:tri
           clean     =.  ([: +./\."_1 '1' = ])`(' '&,:) }  
        NB.clean     =.  ([: +./\.    '1' = ])`(' '&,:) }&.|:  NB.  Equivalent 
to preceding line.

But that's not as symmetric, and the speedup is negligible.  What are some 
other, faster, leaner, cooler or more elegant ways to transform the output of  
tri  into the output of  triangle  ?

-Dan

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to