Skip to content

2015-10: Blaise'ing a Trail

The APL expression to compute Pascal's triangle of order n is fairly simple.

      {{⍉⍵∘.!⍵}0,⍳⍵} 10
1  0  0   0   0   0   0   0  0  0 0
1  1  0   0   0   0   0   0  0  0 0
1  2  1   0   0   0   0   0  0  0 0
1  3  3   1   0   0   0   0  0  0 0
1  4  6   4   1   0   0   0  0  0 0
1  5 10  10   5   1   0   0  0  0 0
1  6 15  20  15   6   1   0  0  0 0
1  7 21  35  35  21   7   1  0  0 0
1  8 28  56  70  56  28   8  1  0 0
1  9 36  84 126 126  84  36  9  1 0
1 10 45 120 210 252 210 120 45 10 1

However, the output leaves something to be desired. Write a function that takes an integer right argument representing the order of the Pascal's triangle to be created and returns a "nicely" formatted character matrix. Because we're using a mono-spaced font and can only format whole character positions, lines do not have to be perfectly centered and may vary by a character position.

Examples:

     (your_function) 10 
                 1                     
                1 1           
               1 2 1                   
              1 3 3 1        
             1 4 6 4 1    
           1 5 10 10 5 1        
         1 6 15 20 15 6 1               
        1 7 21 35 35 21 7 1          
      1 8 28 56 70 56 28 8 1        
    1 9 36 84 126 126 84 36 9 1     
1 10 45 120 210 252 210 120 45 10 1
     (your_function) 1
 1 
1 1
     (your_function) 0 
1 
your_function ←

Solutions

Video Thumbnail YouTube

Chat transcript Code on GitHub