Skip to content

2021-10: On the Right Side

Write a function that:

  • has a right argument T that is a character scalar, vector or a vector of character vectors/scalars.
  • has a left argument W that is a positive integer specifying the width of the result.
  • returns a right-aligned character array R of shape ((2=|≡T)/≢T),W meaning R is one of the following:
    • a W-wide vector if T is a simple vector or scalar.
    • a W-wide matrix with the same number rows as elements of T if T is a vector of vectors/scalars.
  • if an element of T has length greater than W, truncate it after W characters.

💡 Hint: Your solution might make use of take X ↑ Y.

Examples:

In these examples, ⍴⎕← is inserted to display first the result and then its shape.


      ⍴⎕←6 (your_function) '⍒'
     ⍒
6

      ⍴⎕←8 (your_function) 'K' 'E' 'Iverson'
       K
       E
 Iverson
3 8

      ⍴⎕←10 (your_function) 'Parade' 
    Parade
10

      ⍴⎕←8 (your_function) 'Longer Phrase' 'APL' 'Parade' 
r Phrase
     APL
  Parade
3 8

      starsForSpaces←'*'@(=∘' ')
      starsForSpaces 6 (your_function) '⍒'
*****⍒

your_function ←

Solutions

Video Thumbnail YouTube

Chat transcript Code on GitHub