Skip to content

10. Pyramid Scheme

In this problem, you’re asked to enter your own APL function, without giving it any arguments. Your function will be checked by the APL Challenge system using several tests to see whether it is correct.

There’s a trick to change the yes/no-answers of comparisons (1 and 0) into other values. Add 1 and then use square brackets from problem 3 to select from a list of values. For example:

      40 435[1 + 3 1 4 ≥ 2 1 7]
435 435 40

This gives 435 where numbers in 3 1 4 are higher than the corresponding numbers in 2 1 7, 40 otherwise.

In the same way, we can get letters by thinking of a text as a list of letters. For example:

      1 + 3 1 4 ≥ 2 1 7
2 2 1

      'NY'[1 + 3 1 4 ≥ 2 1 7]
YYN

Using everything that you’ve learnt so far, write a function that takes number and gives a text picture of a triangle. The triangle must be constructed as a table of the given number of rows. The first row has a single letter A with one or more spaces (  ) before and after the A. The next row has AAA, the third AAAAA and so on, with the As centred below each other. For example:

      {answer} 4
   A   
  AAA  
 AAAAA 
AAAAAAA

      {answer} 2
 A 
AAA

      {answer} 5
    A    
   AAA   
  AAAAA  
 AAAAAAA 
AAAAAAAAA