Skip to content

2023-10: Partition with a Twist

Splitting delimited data into sub-arrays using partitioning on a delimiter character (or characters) is a fairly common operation in APL. For instance, if you partition the character vector 'this is an example' on each occurrence of the space character, there would be 4 sub-arrays: 'this' 'is' 'an' 'example'. This problem adds a slight twist to the operation in that the left argument indicates how many sub-arrays to return.

Write a function that:

  • takes a non-negative integer left argument, N
  • takes a space-delimited character vector right argument, string
  • returns an array of length N where:
    • if N is less than or equal to the number of sub-arrays in string, the first N-1 elements of the result are the first N-1 space-delimited partitions in string.
      The Nth element of the result is the remaining portion of string.
    • if N is greater than the number of sub-arrays, pad the result with as many empty arrays as necessary to achieve length N.

Note: Each space in string be considered as a delimiter. This means that leading, trailing, or contiguous spaces are potentially significant.

Hint: The partitioned enclose function could be helpful in solving this problem.

Examples:

      1 (your_function) 'this is a sample'
┌────────────────┐
│this is a sample│
└────────────────┘

      2 (your_function) 'this is a sample'
┌────┬───────────┐
│this│is a sample│
└────┴───────────┘

      4 (your_function) 'this is a sample'
┌────┬──┬─┬──────┐
│this│is│a│sample│
└────┴──┴─┴──────┘

      ⍴¨4 (your_function) 'this is a sample' ⍝ each sub-array is a vector
┌─┬─┬─┬─┐
│4│2│1│6│
└─┴─┴─┴─┘

      13 (your_function) '  this  is  a sample  ' ⍝ note the leading, trailing, and multiple interior spaces 
┌┬┬────┬┬──┬┬─┬──────┬┬┬┬┬┐
│││this││is││a│sample││││││
└┴┴────┴┴──┴┴─┴──────┴┴┴┴┴┘

      0 (your_function) 'this is a sample' ⍝ returns an empty vector


      4 (your_function) ''
┌┬┬┬┐
│││││
└┴┴┴┘
your_function ←

Solutions

Video Thumbnail YouTube

Chat transcript Code on GitHub