Skip to content

2019-1: Chunky Monkey

Write a function that, given a scalar or vector as the right argument and a positive (>0) integer chunk size n as the left argument, breaks the array's items up into chunks of size n. If the number of elements in the array is not evenly divisible by n, then the last chunk will have fewer than n elements.

πŸ’‘ Hint: The partitioned enclose function XβŠ‚Y could be helpful for this problem.

Examples:

      3 (your_function) ⍳9   ⍝ ]Box on is used to display the result
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚1 2 3β”‚4 5 6β”‚7 8 9β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
      3 (your_function) ⍳11
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚1 2 3β”‚4 5 6β”‚7 8 9β”‚10 11β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
      10 (your_function) 'Dyalog'
β”Œβ”€β”€β”€β”€β”€β”€β”
β”‚Dyalogβ”‚
β””β”€β”€β”€β”€β”€β”€β”˜
      2 (your_function) 'The' 'cat' 'in' 'the' 'hat' 'sat' 'pat'
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚β”Œβ”€β”€β”€β”¬β”€β”€β”€β”β”‚β”Œβ”€β”€β”¬β”€β”€β”€β”β”‚β”Œβ”€β”€β”€β”¬β”€β”€β”€β”β”‚β”Œβ”€β”€β”€β”β”‚
β”‚β”‚Theβ”‚catβ”‚β”‚β”‚inβ”‚theβ”‚β”‚β”‚hatβ”‚satβ”‚β”‚β”‚patβ”‚β”‚
β”‚β””β”€β”€β”€β”΄β”€β”€β”€β”˜β”‚β””β”€β”€β”΄β”€β”€β”€β”˜β”‚β””β”€β”€β”€β”΄β”€β”€β”€β”˜β”‚β””β”€β”€β”€β”˜β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
      5 (your_function) ''   ⍝ result is 0-element vector of text vectors
      4 (your_function) 5
β”Œβ”€β”
β”‚5β”‚
β””β”€β”˜
your_function ←

Solutions

Video Thumbnail YouTube

Chat transcript Code on GitHub